Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-horses-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Delay automatic service worker registration until load event
2 changes: 1 addition & 1 deletion documentation/docs/07-service-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Service workers act as proxy servers that handle network requests inside your ap

In SvelteKit, if you have a `src/service-worker.js` file (or `src/service-worker.ts`, or `src/service-worker/index.js`, etc) it will be built with Vite and automatically registered. You can disable automatic registration if you need to register the service worker with your own logic (e.g. prompt user for update, configure periodic updates, use `workbox`, etc).

> You can change the location of your service worker and disable automatic registration in your [project configuration](/docs/configuration#files).
> You can change the [location of your service worker](/docs/configuration#files) and [disable automatic registration](/docs/configuration#serviceworker) in your project configuration.

Inside the service worker you have access to the [`$service-worker` module](/docs/modules#$service-worker).

Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ export async function render_response({

const init_service_worker = `
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('${options.service_worker}');
addEventListener('load', () => {
navigator.serviceWorker.register('${options.service_worker}');
});
}
`;

Expand Down