-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Precaching bug with adapter-static
and load()
functions
#98
Comments
I'm going to update the function buildGlobPatterns(globPatterns?: string[]) {
if (globPatterns) {
if (!globPatterns.some(g => g.startsWith('prerendered/')))
globPatterns.push('prerendered/**/*.{html,json,js}')
if (!globPatterns.some(g => g.startsWith('client/')))
globPatterns.push('client/**/*.{js,css,ico,png,svg,webp,webmanifest}')
if (!globPatterns.some(g => g.includes('webmanifest')))
globPatterns.push('client/*.webmanifest')
return globPatterns
}
return ['client/**/*.{js,css,ico,png,svg,webp,webmanifest}', 'prerendered/**/*.{html,json,js}']
} |
Yep, that should fix it |
Also, I had another doubt. When using this strategy, the Am I misunderstanding something? How would I know if all the assets have been precached on the frontend? |
Uhmmm, looks like the |
sure, I'll let you know |
@WhyAsh5114 are you using SPA https://svelte.dev/docs/kit/single-page-apps? |
not really, I'm using adapter-static, with export const prerender = true in root layout.ts |
You need to check it in the browser devtools: Application > Cache Storage
Can you provide a minimal reproduction? |
Also, the env.js file isn't really of interest, the Ohkk, I'll make a minimal repro and link it here. |
Since you're using static adapter (SPA requires it), maybe moving the I have no idea why kit not applying adapter logic like normal build, looks like |
Minimal repro: https://github.com/WhyAsh5114/prerendered-pwa See the |
Thx, looks like we need to move the kit pwa build plugin from 'pre' to 'post' (register it after kit), this way we've now |
There is a problem, now the service worker being generated in the kit output folder, but the adapter will not copy the service worker since it is being generated after the adapter runs (the adapter will copy the client folder without the sw). |
hmm, but again, why would we need the Or are those files also generated after the PWA plugin builds? |
I'm not even sure it contains anything useful, in my minimal repro it's just this:
|
Yes, my globPattern doesn't include JS from the
|
ok, the fix will work for |
Yep, perfect. I expected this by default, maybe we should also update the docs for this, let me know if I can help! |
I'm going to release a patch version, in the meantime you can check it in your local with this dependency: https://pkg.pr.new/@vite-pwa/sveltekit@a7c0605 (I guess once PR merged the pkg.pr.new is removed) |
Checking the docs... |
I will do that once I get back, out rn |
Umm, I installed this and remove my custom URL manipulation. It doesn't seem to work, did you push the patch? And the docs change looks good. |
Remove the storage including the service worker and reload the page, you may have old sw (you should use a virtual, vanillajs or sveltekit for page reload): check if the old service worker is in skip waiting state (yellow dot in devtools) |
Yes, I did try that, clear storage, empty cache and hard reload. I was still getting this error. Same minimal repro with following changes:
|
Try adding |
You are getting that after removing the url manipulation from the service worker?
|
wait, I'm using local tgz file, checking changes... |
In the meantime add the pwa icons... |
Use this link https://pkg.pr.new/@vite-pwa/sveltekit@97 |
Ohk, need to head out for a bit, will get back in an hour or two and add the icons |
Oh, we need font files (ttf,woff*), don't remove first globPattern. |
@WhyAsh5114 kit pwa v0.6.7 released |
Hey, it works! I forgot to use Once I added that, with v0.6.7, it works properly. Thank you very much! |
For some reason, hovering (and clicking) on the link element, the request doesn't go through the service-worker. |
Check https://github.com/WhyAsh5114/prerendered-pwa/pull/1 (about prefetching when hovering the links no idea what's happening, I get some errors but now it should be fixed in the PR => read my comment in the PR). The PR including pwa icons (from kit pwa example) and removed manifest.webamanifest from static folder addding the content to pwa manifest option). |
@userquin, I did merge it and tried to run it again. It still errors out when doing the steps mentioned above. But yeah, this issue has been fixed, its something I need to figure out on my own with workbox docs and all. |
@WhyAsh5114 can you replace |
yep, tried that, still issues I'll look into the mentioned issue tomorrow, a bit tired rn |
@userquin Okay, I finally fixed it. All we just needed to do was: self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
console.log('Service worker activated');
}); The service worker wasn't taking control on the initial page load, and therefore unable to serve cache resulting in errors as the requests were not being passed to it anyways. There was no need to modify the registration logic in the root |
|
Happy new year 🎉 You're using auto update, the code should be: self.skipWaiting()
self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
console.log('Service worker activated');
});
/* THIS IS USED FOR PROMPT FOR UPDATE: CLIENTS WILL NOT EMIT THIS EVENT
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') self.skipWaiting();
});
*/ |
About the logic in the layout, you should avoid calling |
Happy new year to you too! Thank you for the fixes :) |
The main problem
There's a specific bug when using
load()
functions along with prerendering and the static-adapter that I spenthours trying to fix, it occurs primarily due to
@vite-pwa/sveltekit
not being able to figure out how to cachethe returned data from the
load()
, typically stored in:This causes the app to break when offline and navigating to a page that depends on that data.
The hacky fix
vite.config.ts
and the customservice-worker
file.After setting up the basic PWA, I added the following to your
vite.config.ts
:prerendered/**/*.{html,json}
in theglobPatterns
array, this tellsvite-plugin-pwa
to cache theprerendered data (typically in the form of
__data.json
files) so that the app can work offline. This should havebeen enough, but for some reason you need this weird hack in your
service-worker.ts
:This hack is necessary because the
__data.json
files are stored in theprerendered/dependencies/
folder, and theservice worker's default behaviour incorrectly configures their URLs. This hack manually alters the URLs so that the
service worker can cache them correctly.
The text was updated successfully, but these errors were encountered: