Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sveltekit version and custom service worker injection point (js) #27

Merged
merged 1 commit into from
Oct 19, 2024

Conversation

userquin
Copy link
Member

@userquin userquin commented Oct 19, 2024

Description

Use / as navigate fallback in custom VanillaJS service workers.

This PR also includes:

Linked Issues

Additional Context

check #25


Tip

The author of this PR can publish a preview release by commenting /publish below.

@alexander-vidoni
Copy link

Thank you.

@userquin userquin changed the title fix: sveltekit custom service worker (js) fix: sveltekit version + custom service worker (js) Oct 19, 2024
@userquin userquin changed the title fix: sveltekit version + custom service worker (js) fix: sveltekit version and custom service worker injection point (js) Oct 19, 2024
@userquin
Copy link
Member Author

Thank you.

Svelte projects using Vite and so we'll have index.html entry point, there is no need to change the corresponding sw.

@userquin userquin merged commit dcbc521 into main Oct 19, 2024
1 check passed
@userquin userquin deleted the userquin/fix-sveltekit-sw-js branch October 19, 2024 01:20
@alexander-vidoni
Copy link

Svelte projects using Vite and so we'll have index.html entry point, there is no need to change the corresponding sw.

Hi,
i did a lot of testing today and i think we are both correct.

@userquin perspective:

Yes, there is an index.html if you call /index.html in the brower or cURL etc.
It ships the same content as if you request just /
But these index.html and / are only aliases of the applications entrypoint.

@alexander-vidoni perspective

The shipped content of index.html and / are no assets, they are entrypoints.

If you are building a sveltekit application with vite erverything is fine.

But if you scaffold your sveltekit application with create vite-pwa and the vite-pwa-plugin as its dependency you introduce workbox functionality and the precaching code to the service-worker.js file in the /src directory.

When using the injectManifest option, the build process replaces the self.__WB_MANIFEST placeholder with an array of urls and hashes to the parameters of the precacheAndRoute() function.

This array of urls in the service-worker was hardcoded by the build process and and was injected into the service-worker code.

The problem or better the bug

During build time there is no index.html file to glob, because index.html is not a physical file, it is an alias to the applications entrypoint.

The consequence is, that during runtime in the browser, the service-worker runs prechacheAndRoute() with an array which does not contain index.html.

If index.html is not cached, the route to index.html could not be registered

// to allow work offline
registerRoute(new NavigationRoute(
  createHandlerBoundToURL('/index.html'),
  { allowlist },
))

and the service-worker.js throws an exception which is not caught:

service-worker.js:2 Uncaught non-precached-url: non-precached-url :: [{"url":"index.html"}]
    at Q.createHandlerBoundToURL (http://localhost:4173/service-worker.js:2:1255)
    at re (http://localhost:4173/service-worker.js:2:5578)
    at http://localhost:4173/service-worker.js:2:7769

The uncaught exception leads to a #redundant state during service-worker.js installation in the browser, because faulty service-worker will not be activated by the browser.

The glob during build does fail (does not include index.html), due to the nonexistence of a physical index.html in the directories.
As user of create-pwa you alwas have a service-worker which fails, which leads to a non installable webapp, which leads to frustration, which leads to not using that fantasic build stack you maintain.

Thank you for your work.

@userquin
Copy link
Member Author

You cannot use vite-plugin-pwa with kit, you need to use kit pwa integration plugin: this plugin will replace the index.html with the / mapping (used by create-pwa when selecting Svelte + Kit).

If you're using offline support (precaching) with kit, then you need to prerender at least one page, otherwise you cannot use precaching.

Anyway, you can change everything in the pwa options and so there is no problem at all.

About workbox (workbox-build): it will be used if you enable it, that's, when using generateSW strategy or using injectManifest with offline and precaching support (injectionPoint), otherwise workbox-build won't be even loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants