Skip to content

Commit a8dd72d

Browse files
Add service workers guide (#1043)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 3fd0d91 commit a8dd72d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"title": "Guides",
3-
"pages": ["data-fetching.mdx", "data-mutation.mdx"]
3+
"pages": ["data-fetching.mdx", "data-mutation.mdx", "service-workers.mdx"]
44
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: "Service workers"
3+
---
4+
5+
To register a service worker:
6+
7+
1. Place your service-worker file in the `public` directory (e.g., `public/sw.js`), making it available at the root URL (`/sw.js`).
8+
2. Add registration logic to the `client.entry.tsx` file.
9+
10+
```tsx {6-11} title="src/client.entry.tsx"
11+
// @refresh reload
12+
import { mount, StartClient } from "@solidjs/start/client";
13+
14+
mount(() => <StartClient />, document.getElementById("app")!);
15+
16+
if ("serviceWorker" in navigator && import.meta.env.PROD) {
17+
window.addEventListener("load", () => {
18+
navigator.serviceWorker.register("/sw.js");
19+
});
20+
}
21+
```

0 commit comments

Comments
 (0)