Skip to content

Commit 0badf86

Browse files
committed
fix: exclude service worker from tsconfig
Also document how to get types working inside the file closes #8127
1 parent 926040b commit 0badf86

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

.changeset/tidy-tools-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: exclude service worker from tsconfig

documentation/docs/30-advanced/40-service-workers.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ if ('serviceWorker' in navigator) {
1616
}
1717
```
1818

19+
## Inside the service worker
20+
1921
Inside the service worker you have access to the [`$service-worker` module](/docs/modules#$service-worker), which provides you with the paths to all static assets, build files and prerendered pages. You're also provided with an app version string which you can use for creating a unique cache name. If your Vite config specifies `define` (used for global variable replacements), this will be applied to service workers as well as your server/client builds.
2022

2123
The following example caches the built app and any files in `static` eagerly, and caches all other requests as they happen. This would make each page work offline once visited.
@@ -87,6 +89,8 @@ self.addEventListener('fetch', (event) => {
8789

8890
> Be careful when caching! In some cases, stale data might be worse than data that's unavailable while offline. Since browsers will empty caches if they get too full, you should also be careful about caching large assets like video files.
8991
92+
## During development
93+
9094
The service worker is bundled for production, but not during development. For that reason, only browsers that support [modules in service workers](https://web.dev/es-modules-in-sw) will be able to use them at dev time. If you are manually registering your service worker, you will need to pass the `{ type: 'module' }` option in development:
9195

9296
```js
@@ -99,4 +103,27 @@ navigator.serviceWorker.register('/service-worker.js', {
99103

100104
> `build` and `prerendered` are empty arrays during development
101105
106+
## Type safety
107+
108+
Setting up proper types for service workers requires some manual setup. Inside your `service-worker.js`, add the following to the top of your file:
109+
110+
```original-js
111+
/// <reference no-default-lib="true"/>
112+
/// <reference lib="esnext" />
113+
/// <reference lib="webworker" />
114+
115+
const ws = /** @type {ServiceWorkerGlobalScope} */ (/** @type {unknown} */ (self));
116+
```
117+
```generated-ts
118+
/// <reference no-default-lib="true"/>
119+
/// <reference lib="esnext" />
120+
/// <reference lib="webworker" />
121+
122+
const ws = self as unknown as ServiceWorkerGlobalScope;
123+
```
124+
125+
This disables access to DOM typings like `HTMLElement` which are not available inside a service worker and instantiates the correct globals. The reassignment of `self` to `ws` allows you to type cast it in the process (there are a couple of ways to do this, but the easiest that requires no additional files). Use `ws` instead of `self` in the rest of the file.
126+
127+
## Other solutions
128+
102129
SvelteKit's service worker implementation is deliberately low-level. If you need a more full-flegded but also more opinionated solution, we recommend looking at solutions like [Vite PWA plugin](https://vite-pwa-org.netlify.app/frameworks/sveltekit.html), which uses [Workbox](https://web.dev/learn/pwa/workbox). For more general information on service workers, we recommend [the MDN web docs](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers).

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ export function write_tsconfig(config, cwd = process.cwd()) {
6060
include.push(config_relative(`${test_folder}/**/*.ts`));
6161
include.push(config_relative(`${test_folder}/**/*.svelte`));
6262

63+
const exclude = [config_relative('node_modules/**'), './[!ambient.d.ts]**'];
64+
if (path.extname(config.files.serviceWorker)) {
65+
exclude.push(config_relative(config.files.serviceWorker));
66+
} else {
67+
exclude.push(config_relative(`${config.files.serviceWorker}.js`));
68+
exclude.push(config_relative(`${config.files.serviceWorker}.ts`));
69+
exclude.push(config_relative(`${config.files.serviceWorker}.d.ts`));
70+
}
71+
6372
write_if_changed(
6473
out,
6574
JSON.stringify(
@@ -88,7 +97,7 @@ export function write_tsconfig(config, cwd = process.cwd()) {
8897
target: 'esnext'
8998
},
9099
include,
91-
exclude: [config_relative('node_modules/**'), './[!ambient.d.ts]**']
100+
exclude
92101
},
93102
null,
94103
'\t'

0 commit comments

Comments
 (0)