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
-
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: false
in 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
onMount
on the root+layout.svelte
.I've tried registering with this code:
In this case I'm not able to use the
load
event on thewindow
object viaaddEventListener
because the page loads too fast, and once SvelteKit takes control theload
event has passed. So I'm forced to use, in this instance, theonMount
life cycle hook.Besides that, the main question is (a two-part question):
Where do I position the
service-worker.js
file? 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.js
seems to work, but I see these errors:Meaning I wouldn't be able to use the
$service-worker
package 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-worker
doesn't work. Calling it like../lib/service-worker
doesn't work either. This all returns something similar to:Register it on the
app.html
using thewindow
load
eventAt the bottom of the head in the
app.html
file 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 thedev
variable from$app/environment
does.Now the same question applies here. Where do I position the
service-worker.js
file? 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.js
seems to work, but I see these errors (again):Meaning I wouldn't be able to use the
$service-worker
package (same problem with TypeScript).If I position it at the root of the
/lib/
folder calling it like this$lib/service-worker
doesn't work. Calling it like./lib/service-worker
doesn'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.html
easily unless I start doing some other post processing.Main goal
If I could use TypeScript, the
$service-worker
package and guarantee that the service worker is registered via theload
window
event, 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