Skip to content

feat: allow to specify options for the service worker in svelte.config.js #13578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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/fuzzy-guests-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: allow to specify options for the service worker in `svelte.config.js`
3 changes: 3 additions & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ const options = object(

serviceWorker: object({
register: boolean(true),
// options could be undefined but if it is defined we only validate that
// it's an object since the type comes from the browser itself
options: validate(undefined, object({}, true)),
Comment on lines +270 to +272
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd still want to error early, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's undefined we just don't add the options...if they pass some unknown values it's also fine, this way in case they update the signature we don't need to fix it on our end...or do you mean something else?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, yeah i guess since we're not using it internally it doesn't really matter — types are sufficient. fair enough

files: fun((filename) => !/\.DS_Store/.test(filename))
}),

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/sync/write_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const options = {
preload_strategy: ${s(config.kit.output.preloadStrategy)},
root,
service_worker: ${has_service_worker},
service_worker_options: ${config.kit.serviceWorker.register ? s(config.kit.serviceWorker.options) : 'null'},
templates: {
app: ({ head, body, assets, nonce, env }) => ${s(template)
.replace('%sveltekit.head%', '" + head + "')
Expand Down
28 changes: 21 additions & 7 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,17 +683,31 @@ export interface KitConfig {
resolution?: 'client' | 'server';
};
serviceWorker?: {
/**
* Whether to automatically register the service worker, if it exists.
* @default true
*/
register?: boolean;
/**
* Determine which files in your `static` directory will be available in `$service-worker.files`.
* @default (filename) => !/\.DS_Store/.test(filename)
*/
files?(filepath: string): boolean;
};
files?: (file: string) => boolean;
} & (
| {
/**
* Whether to automatically register the service worker, if it exists.
* @default true
*/
register: true;
/**
* Options for serviceWorker.register("...", options);
*/
options?: RegistrationOptions;
}
| {
/**
* Whether to automatically register the service worker, if it exists.
* @default true
*/
register?: false;
}
);
typescript?: {
/**
* A function that allows you to edit the generated `tsconfig.json`. You can mutate the config (recommended) or return a new one.
Expand Down
9 changes: 8 additions & 1 deletion packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,14 @@ export async function render_response({
}

if (options.service_worker) {
const opts = __SVELTEKIT_DEV__ ? ", { type: 'module' }" : '';
let opts = __SVELTEKIT_DEV__ ? ", { type: 'module' }" : '';
if (options.service_worker_options != null) {
const service_worker_options = { ...options.service_worker_options };
if (__SVELTEKIT_DEV__) {
service_worker_options.type = 'module';
}
opts = `, ${s(service_worker_options)}`;
}

// we use an anonymous function instead of an arrow function to support
// older browsers (https://github.com/sveltejs/kit/pull/5417)
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export interface SSROptions {
preload_strategy: ValidatedConfig['kit']['output']['preloadStrategy'];
root: SSRComponent['default'];
service_worker: boolean;
service_worker_options: RegistrationOptions;
templates: {
app(values: {
head: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const config = {
],
handleHttpError: 'warn'
},
serviceWorker: {
register: true,
options: {
updateViaCache: 'imports'
}
},

version: {
name: 'TEST_VERSION'
Expand Down
22 changes: 22 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,3 +1532,25 @@ test.describe('Serialization', () => {
expect(await page.textContent('h1')).toBe('It works!');
});
});

test.describe('service worker option', () => {
test('pass the options to the service worker', async ({ page }) => {
await page.goto('/');
const content = await page.content();
const matching = content.match(/navigator\.serviceWorker\.register\(.+?, (?<options>{.+?})\)/);
let options = {};
if (matching && matching.groups) {
options = JSON.parse(matching.groups.options);
}
if (process.env.DEV) {
expect(options).toMatchObject({
type: 'module',
updateViaCache: 'imports'
});
} else {
expect(options).toMatchObject({
updateViaCache: 'imports'
});
}
});
});
28 changes: 21 additions & 7 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,17 +665,31 @@ declare module '@sveltejs/kit' {
resolution?: 'client' | 'server';
};
serviceWorker?: {
/**
* Whether to automatically register the service worker, if it exists.
* @default true
*/
register?: boolean;
/**
* Determine which files in your `static` directory will be available in `$service-worker.files`.
* @default (filename) => !/\.DS_Store/.test(filename)
*/
files?(filepath: string): boolean;
};
files?: (file: string) => boolean;
} & (
| {
/**
* Whether to automatically register the service worker, if it exists.
* @default true
*/
register: true;
/**
* Options for serviceWorker.register("...", options);
*/
options?: RegistrationOptions;
}
| {
/**
* Whether to automatically register the service worker, if it exists.
* @default true
*/
register?: false;
}
);
typescript?: {
/**
* A function that allows you to edit the generated `tsconfig.json`. You can mutate the config (recommended) or return a new one.
Expand Down
Loading