Replies: 1 comment
-
|
so for me it looks like the root is actually the root of your project, for example i left my service-worker file in the "src" folder, so the path to it looks like this './src/service-worker.ts' hope it helps someone |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there,
This is most probably a newbie question, but I'm really confused as to how to manually register a service worker. I've set
kit.serviceWorker.register: falsein mysvelte.config.js. I want to manually register it to create custom logic for it.Here's how the service worker looks like at the moment (but more logic will be added/modified once I'm able to register it without problems):
Here's all the different ways I've tried:
Register it using
onMounton the root+layout.svelte.I've tried registering with this code:
In this case I'm not able to use the
loadevent on thewindowobject viaaddEventListenerbecause the page loads too fast, and once SvelteKit takes control theloadevent has passed. So I'm forced to use, in this instance, theonMountlife cycle hook.Besides that, the main question is (a two-part question):
Where do I position the
service-worker.jsfile? In the/lib/folder? In the/static/folder? Or where else? And how do I call it withinnavigator.serviceWorker.register?If I position it at the root of
/static/folder, calling it like this/service-worker.jsseems to work, but I see these errors:Meaning I wouldn't be able to use the
$service-workerpackage which would be needed to access all the goodies that come with it (files,prerendered, etc.). And given it's in the static folder I wouldn't be able to use TypeScript in the future unless I start to play around with Vite's config.If I position it at the root of the
/lib/folder calling it like this$lib/service-workerdoesn't work. Calling it like../lib/service-workerdoesn't work either. This all returns something similar to:Register it on the
app.htmlusing thewindowloadeventAt the bottom of the head in the
app.htmlfile I added something like:Notice two things, I'm able to use
window.addEventListener('load', ...), and the need to use a environment variable (in this case calledPUBLIC_DEV) to simulate what thedevvariable from$app/environmentdoes.Now the same question applies here. Where do I position the
service-worker.jsfile? In the/lib/folder? In the/static/folder? And how do I call it withinnavigator.serviceWorker.register?If I position it at the root of
/static/folder, calling it like this%sveltekit.assets%/service-worker.jsseems to work, but I see these errors (again):Meaning I wouldn't be able to use the
$service-workerpackage (same problem with TypeScript).If I position it at the root of the
/lib/folder calling it like this$lib/service-workerdoesn't work. Calling it like./lib/service-workerdoesn't work either. This all returns something similar to:Another caveat this approach has is that I can't minify the
<script>tag in theapp.htmleasily unless I start doing some other post processing.Main goal
If I could use TypeScript, the
$service-workerpackage and guarantee that the service worker is registered via theloadwindowevent, while being able to have custom registration and logic, that would be the perfect solution. But I don't know how to get there.I'm sure I'm missing something very obvious, but I just can't see it yet. Any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions