antony / sveltekit-adapter-browser-extension

Build browser extensions with Svelte (early prototype)
160 stars 19 forks source link

Script detection doesn't work in current versions of SvelteKit #24

Open Rich-Harris opened 1 year ago

Rich-Harris commented 1 year ago

See https://github.com/sveltejs/kit/issues/9277 — since https://github.com/sveltejs/kit/pull/8901, this logic...

https://github.com/antony/sveltekit-adapter-browser-extension/blob/dc45063ed2e79464de86ea0cd6c8a70b981e7838/adapter-browser-extension.mjs#L63-L73

...no longer works, because the <script> no longer has type="module" (as this would defeat streaming). The 'proper' solution might well be to finally implement https://github.com/sveltejs/kit/issues/3555

bogacg commented 1 year ago

@Rich-Harris I did manage to fix that part of the problem in my local repo, however now I get this error when the extension is executed:

Uncaught TypeError: Cannot read properties of null (reading 'parentElement')

from generated (and externalised script)

     __sveltekit_1yt2rwz = {
        env: {},
        base: new URL(".", location).pathname.slice(0, -1),
        element: document.currentScript.parentElement
    };

    const data = [null,null];

    Promise.all([
        import("./app/immutable/entry/start.e005700e.js"),
        import("./app/immutable/entry/app.ae8f820f.js")
    ]).then(([kit, app]) => {
        kit.start(app, __sveltekit_1yt2rwz.element, {
            node_ids: [0, 2],
            data,
            form: null,
            error: null
        });
    });

I do get document object, but currentScript is null, as well as __sveltekit_1yt2rwz.

Do you have any useful pointers to fix that?

Reading that JavaScript I think we need to do more than externalized module generation from inline script.

bogacg commented 1 year ago

Digging further into this, I believe fix needs to come from SvelteKit side. As mentioned by @Rich-Harris sveltejs/kit#3555 needs to be implemented because modules have no currentScript reach yet; therefore turning code in the example above into an external module is useless.

There is an ongoing discussion at WHATWG started years ago. 🤦🏻‍♂️