webui: SvelteKit + webui problem
There may be a conflict between how a SvelteKit SPA boots itself and how zig-webui app loads its content. To see this, do the following.
Create a SvelteKit project with
npm create svelte@latest webui-sveltekit
Choose:
- “Skeleton” app option
- “No” do not use TypeScript, nor JSDoc
- Add “ESLint”
Follow instructions to test it, ending with npm run dev.
Next, set it up to generate an SPA:
npm install @sveltejs/adapter-static
Edit svelte.config.js to look like this:
// import adapter from '@sveltejs/adapter-auto';
import adapterStatic from '@sveltejs/adapter-static';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
// adapter: adapter()
adapter: adapterStatic({
pages: 'build',
assets: 'build',
fallback: 'app.html', // required to generate app.html in build folder
precompress: false,
strict: false
})
}
};
export default config;
In the /routes folder, add a file, +layout.js and add this:
export const ssr = false;
export const prerender = false;
Run npm run dev to test.
The above project is available at https://github.com/sensiblearts/SvelteKit-WebUI-SPA-test.git
To build the SPA:
npm run build
Copy the build folder (I renamed it spa_build) and all of its contents (app.html, compiled JS, etc) into a new zig-webui test project, at the same level as its src folder.
Edit main.zig to read as follows:
pub fn main() !void {
var nwin = webui.newWindow();
const path = "./spa_build";
const filename = "app.html";
_ = webui.setDefaultRootFolder(path);
_ = nwin.show(filename);
webui.wait();
}
Run the program with
zig build run and you will see this:
Then, right-click on the screen to inspect the JS console:
In the above screenshot, the line app.html:28.11 is pointing to this:
kit.start(app, element);
Thus, it’s the entry point to a SvelteKit app that is causing the problem. If you drill down on the first module listed in the Not found error, you see this:
Which, to me, looks like the first thing a SvelteKit app does is replace the client-side state – I think they call this “hydration” – and to do this it seems to be reloading itself – looking for app.html for a second load. But it cannot find it…?
Note: The above happens without including webui.js in the app. If we add <script src="webui.js"></script> to app.html, we get the same 404, but the app crashes and reloads repeatedly, with this cycling in the console:
I think that someone who knows the internals of WebUI should talk to someone who knows the internals of SvelteKit.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 23 (23 by maintainers)
Commits related to this issue
- Adding isConnected to the JavaScript bridge * More details https://github.com/webui-dev/webui/issues/392 — committed to webui-dev/webui by hassandraga 2 years ago
- Fix infinite reload * More details https://github.com/webui-dev/webui/issues/392 — committed to hassandraga/gsysmo by hassandraga 2 years ago
- Fix Bridge Navigation Event * If there is no bind all, then keep navigation events as default * More details (https://github.com/webui-dev/webui/issues/392) — committed to webui-dev/webui by hassandraga 2 years ago
My pleasure. I’ve decided to go with
webuiinstead of Tauri.Thank you.
Thank you for the test. Can you please build webui in debug mode?
make debugormingw32-make debug, then when you run the Zig app in terminal, webui will print logs. May be this 404 error can be easy to fix.I turned off server navigation in the bridge constructor:
and I stopped binding events in my zig project,
And the app runs without any infinite reload but still has the weird, single 404 in the SvelteKit code at app load:
The exact same compiled SvelteKit app runs fine when served with npm serve (when no webui.js is added).
To test this fix you can do this:
if(webui.isConnected()))libwebui-2-static.a)ui.Bind(w, "", events))The
webui.jsinitialization routine is trying to connect to the WS server atws://localhost:xxxx/_webui_ws_connect, but before that, for some reason, a WS close event is called (globalThis.close()), which is line177. Like closing a connection before opening… I will investigate this to fix the177warning, but the app closing issue is another thing.