Customize output name of service worker
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the problem
I'm trying to migrate my old React project to SvelteKit.
In my old project, I have registered a service worker in /sw.js, but in SvelteKit, the service worker is located in /service-worker.js and cannot be changed.
https://github.com/sveltejs/kit/blob/main/packages/kit/src/runtime/server/page/render.js#L401-L405
// #L401-L405
blocks.push(`if ('serviceWorker' in navigator) {
addEventListener('load', function () {
navigator.serviceWorker.register('${prefixed('service-worker.js')}'${opts});
// ^ hard-coded here
});
}`);
As a workaround, I have to add a pre-build step to build sw.js isolatedly, then copy it into /static folder, but I feel it's not a good solution.
Describe the proposed solution
It would be great if we can customize the location in svelte.config.js likes this:
// svelte.config.js
const config = {
kit: {
serviceWorker: {
register: true,
// `scriptURL` comes from
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#syntax
scriptURL: '/sw.js',
},
},
}
export default config
Alternatives considered
No response
Importance
nice to have
Additional Information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with packages/kit/src/runtime/server/page/render.js around lines 401-405, where the service-worker.js registration URL is hard-coded. Then trace how options from svelte.config.js reach this rendering code. Done means a configured scriptURL such as /sw.js is used for service-worker registration instead of the fixed path.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend, web-dev
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100