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:

Screenshot zig webui

Then, right-click on the screen to inspect the JS console:

Screenshot 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:

Screenshot JS compiled

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:

Screenshot webui websocket

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

Most upvoted comments

My pleasure. I’ve decided to go with webui instead of Tauri.

Thank you.

Thank you for the test. Can you please build webui in debug mode? make debug or mingw32-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:

#allowNavigation = true;

and I stopped binding events in my zig project,

   // _ = nwin.bind("", events);

And the app runs without any infinite reload but still has the weird, single 404 in the SvelteKit code at app load:

image

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:

  1. Download latest nightly build GCC (Because you need the new JavaScript API if(webui.isConnected()))
  2. Update your Go webui module manually (replace libwebui-2-static.a)
  3. Update your Go script like this PR (Add all events bind ui.Bind(w, "", events))

@hassandraga can you advise me how to find out what webui.js line 177 is doing?

The webui.js initialization routine is trying to connect to the WS server at ws://localhost:xxxx/_webui_ws_connect, but before that, for some reason, a WS close event is called (globalThis.close()), which is line 177. Like closing a connection before opening… I will investigate this to fix the 177 warning, but the app closing issue is another thing.