Skip to content

Commit bfdb564

Browse files
feat: allow to specify options for the service worker in svelte.config.js (#13578)
* feat: allow to specify options for the service worker in `svelte.config.js` * fix: better validation of `options` config * fix: use easier types * chore: add test for service worker `options` * fix: `object` is already a function --------- Co-authored-by: Rich Harris <[email protected]>
1 parent aa0ceda commit bfdb564

File tree

9 files changed

+88
-15
lines changed

9 files changed

+88
-15
lines changed

.changeset/fuzzy-guests-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: allow to specify options for the service worker in `svelte.config.js`

packages/kit/src/core/config/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ const options = object(
271271

272272
serviceWorker: object({
273273
register: boolean(true),
274+
// options could be undefined but if it is defined we only validate that
275+
// it's an object since the type comes from the browser itself
276+
options: validate(undefined, object({}, true)),
274277
files: fun((filename) => !/\.DS_Store/.test(filename))
275278
}),
276279

packages/kit/src/core/sync/write_server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const options = {
4646
preload_strategy: ${s(config.kit.output.preloadStrategy)},
4747
root,
4848
service_worker: ${has_service_worker},
49+
service_worker_options: ${config.kit.serviceWorker.register ? s(config.kit.serviceWorker.options) : 'null'},
4950
templates: {
5051
app: ({ head, body, assets, nonce, env }) => ${s(template)
5152
.replace('%sveltekit.head%', '" + head + "')

packages/kit/src/exports/public.d.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,17 +710,31 @@ export interface KitConfig {
710710
resolution?: 'client' | 'server';
711711
};
712712
serviceWorker?: {
713-
/**
714-
* Whether to automatically register the service worker, if it exists.
715-
* @default true
716-
*/
717-
register?: boolean;
718713
/**
719714
* Determine which files in your `static` directory will be available in `$service-worker.files`.
720715
* @default (filename) => !/\.DS_Store/.test(filename)
721716
*/
722-
files?(filepath: string): boolean;
723-
};
717+
files?: (file: string) => boolean;
718+
} & (
719+
| {
720+
/**
721+
* Whether to automatically register the service worker, if it exists.
722+
* @default true
723+
*/
724+
register: true;
725+
/**
726+
* Options for serviceWorker.register("...", options);
727+
*/
728+
options?: RegistrationOptions;
729+
}
730+
| {
731+
/**
732+
* Whether to automatically register the service worker, if it exists.
733+
* @default true
734+
*/
735+
register?: false;
736+
}
737+
);
724738
typescript?: {
725739
/**
726740
* A function that allows you to edit the generated `tsconfig.json`. You can mutate the config (recommended) or return a new one.

packages/kit/src/runtime/server/page/render.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,14 @@ export async function render_response({
478478
}
479479

480480
if (options.service_worker) {
481-
const opts = __SVELTEKIT_DEV__ ? ", { type: 'module' }" : '';
481+
let opts = __SVELTEKIT_DEV__ ? ", { type: 'module' }" : '';
482+
if (options.service_worker_options != null) {
483+
const service_worker_options = { ...options.service_worker_options };
484+
if (__SVELTEKIT_DEV__) {
485+
service_worker_options.type = 'module';
486+
}
487+
opts = `, ${s(service_worker_options)}`;
488+
}
482489

483490
// we use an anonymous function instead of an arrow function to support
484491
// older browsers (https://github.com/sveltejs/kit/pull/5417)

packages/kit/src/types/internal.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ export interface SSROptions {
450450
preload_strategy: ValidatedConfig['kit']['output']['preloadStrategy'];
451451
root: SSRComponent['default'];
452452
service_worker: boolean;
453+
service_worker_options: RegistrationOptions;
453454
templates: {
454455
app(values: {
455456
head: string;

packages/kit/test/apps/basics/svelte.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ const config = {
3737
console.warn(message);
3838
}
3939
},
40+
serviceWorker: {
41+
register: true,
42+
options: {
43+
updateViaCache: 'imports'
44+
}
45+
},
4046

4147
version: {
4248
name: 'TEST_VERSION'

packages/kit/test/apps/basics/test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,3 +1635,25 @@ test.describe('params prop', () => {
16351635
await expect(page.locator('p')).toHaveText('x: 456');
16361636
});
16371637
});
1638+
1639+
test.describe('service worker option', () => {
1640+
test('pass the options to the service worker', async ({ page }) => {
1641+
await page.goto('/');
1642+
const content = await page.content();
1643+
const matching = content.match(/navigator\.serviceWorker\.register\(.+?, (?<options>{.+?})\)/);
1644+
let options = {};
1645+
if (matching && matching.groups) {
1646+
options = JSON.parse(matching.groups.options);
1647+
}
1648+
if (process.env.DEV) {
1649+
expect(options).toMatchObject({
1650+
type: 'module',
1651+
updateViaCache: 'imports'
1652+
});
1653+
} else {
1654+
expect(options).toMatchObject({
1655+
updateViaCache: 'imports'
1656+
});
1657+
}
1658+
});
1659+
});

packages/kit/types/index.d.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,17 +687,31 @@ declare module '@sveltejs/kit' {
687687
resolution?: 'client' | 'server';
688688
};
689689
serviceWorker?: {
690-
/**
691-
* Whether to automatically register the service worker, if it exists.
692-
* @default true
693-
*/
694-
register?: boolean;
695690
/**
696691
* Determine which files in your `static` directory will be available in `$service-worker.files`.
697692
* @default (filename) => !/\.DS_Store/.test(filename)
698693
*/
699-
files?(filepath: string): boolean;
700-
};
694+
files?: (file: string) => boolean;
695+
} & (
696+
| {
697+
/**
698+
* Whether to automatically register the service worker, if it exists.
699+
* @default true
700+
*/
701+
register: true;
702+
/**
703+
* Options for serviceWorker.register("...", options);
704+
*/
705+
options?: RegistrationOptions;
706+
}
707+
| {
708+
/**
709+
* Whether to automatically register the service worker, if it exists.
710+
* @default true
711+
*/
712+
register?: false;
713+
}
714+
);
701715
typescript?: {
702716
/**
703717
* A function that allows you to edit the generated `tsconfig.json`. You can mutate the config (recommended) or return a new one.

0 commit comments

Comments
 (0)