@rsc-kit/mcp 0.41.0 → 0.42.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/recipes.js +18 -2
- package/dist/recipes.js.map +1 -1
- package/guides/admin-plugins.md +206 -6
- package/guides/api-routes.md +159 -215
- package/guides/authorization.md +103 -114
- package/guides/backend-answered-pages.md +301 -252
- package/guides/bap-vs-inertia.md +67 -25
- package/guides/blueprints.md +300 -169
- package/guides/bun.md +42 -25
- package/guides/caching.md +46 -49
- package/guides/coming-from-inertia.md +2 -2
- package/guides/commands.md +251 -200
- package/guides/connection.md +50 -54
- package/guides/content-security-policy.md +133 -84
- package/guides/domains.md +190 -149
- package/guides/edge-caching.md +242 -268
- package/guides/emails.md +96 -45
- package/guides/errors.md +116 -137
- package/guides/feature-flags.md +100 -30
- package/guides/file-uploads.md +101 -26
- package/guides/fonts.md +139 -110
- package/guides/forms.md +520 -542
- package/guides/images.md +87 -28
- package/guides/index.json +13 -33
- package/guides/instrumentation.md +134 -95
- package/guides/isr.md +3 -3
- package/guides/live-data-in-production.md +695 -0
- package/guides/live-data.md +170 -839
- package/guides/mcp.md +83 -59
- package/guides/metadata.md +124 -179
- package/guides/navigation.md +118 -147
- package/guides/no-javascript.md +49 -57
- package/guides/offline.md +118 -153
- package/guides/openapi.md +100 -35
- package/guides/pages.md +160 -50
- package/guides/ppr.md +178 -256
- package/guides/pwa.md +270 -216
- package/guides/queries.md +171 -300
- package/guides/react-compiler.md +90 -50
- package/guides/redirects.md +141 -177
- package/guides/response-headers.md +76 -74
- package/guides/route-interception.md +112 -111
- package/guides/routing.md +216 -322
- package/guides/sections.md +63 -66
- package/guides/seo-files.md +127 -65
- package/guides/server-actions.md +285 -357
- package/guides/static-generation.md +131 -199
- package/guides/testing.md +269 -147
- package/guides/third-party-scripts.md +94 -47
- package/guides/typed-routes.md +115 -113
- package/guides/url-validation.md +113 -61
- package/guides/validation.md +53 -62
- package/guides/view-transitions.md +159 -132
- package/guides/where-it-runs.md +226 -162
- package/package.json +2 -2
- package/guides/admin.md +0 -520
- package/guides/deployment.md +0 -598
- package/guides/go.md +0 -539
- package/guides/laravel.md +0 -787
- package/guides/your-own-backend.md +0 -602
package/guides/pwa.md
CHANGED
|
@@ -2,9 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
> Making the app installable, and adding push notifications and background sync.
|
|
4
4
|
|
|
5
|
-
An installable app is a manifest
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
An installable app is a manifest and a service worker. The manifest is a file
|
|
6
|
+
beside your routes:
|
|
7
|
+
|
|
8
|
+
```ts title="src/app/manifest.ts"
|
|
9
|
+
import type { WebManifest } from '@rsc-kit/core/manifest-file'
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
id: '/',
|
|
13
|
+
name: 'Orders',
|
|
14
|
+
shortName: 'Orders',
|
|
15
|
+
themeColor: '#0b0b0c',
|
|
16
|
+
backgroundColor: '#ffffff',
|
|
17
|
+
} satisfies WebManifest
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Put `icon-192.png` and `icon-512.png` next to it, turn on `offline`, and the
|
|
21
|
+
browser offers to install the app. [Offline](/guides/offline/) covers the
|
|
22
|
+
service worker. This page covers the rest: the manifest, an install button,
|
|
23
|
+
splash screens, push notifications and background sync.
|
|
8
24
|
|
|
9
25
|
## Starting one installable
|
|
10
26
|
|
|
@@ -12,24 +28,26 @@ that makes the app feel like one the operating system knows about.
|
|
|
12
28
|
bun create rsc-kit@latest my-app --pwa
|
|
13
29
|
```
|
|
14
30
|
|
|
15
|
-
Or answer yes to *Installable app* when it asks. It turns on `offline
|
|
16
|
-
writes
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
Or answer yes to *Installable app* when it asks. It turns on `offline` and
|
|
32
|
+
writes:
|
|
33
|
+
|
|
34
|
+
- `src/app/manifest.ts`
|
|
35
|
+
- a static `/offline` page
|
|
36
|
+
- four starter icons: `icon-192.png`, `icon-512.png`, `icon-maskable-512.png`
|
|
37
|
+
and `apple-icon.png`
|
|
38
|
+
|
|
39
|
+
The app is installable as soon as it exists. Chrome's installability check
|
|
40
|
+
finds nothing missing, and with the network cut the home page still loads and
|
|
41
|
+
a page never visited lands on `/offline`. Replace the icons with your own and
|
|
42
|
+
keep the names.
|
|
23
43
|
|
|
24
|
-
It is off by default
|
|
25
|
-
|
|
26
|
-
a template takes for you.
|
|
44
|
+
It is off by default. A service worker outlives the code that installed it,
|
|
45
|
+
so take it on deliberately.
|
|
27
46
|
|
|
28
|
-
##
|
|
47
|
+
## Declaring the manifest
|
|
29
48
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
is a file beside your routes:
|
|
49
|
+
`src/app/manifest.ts` exports an object literal. The build reads it, writes
|
|
50
|
+
`manifest.webmanifest`, and links it from every page:
|
|
33
51
|
|
|
34
52
|
```ts title="src/app/manifest.ts"
|
|
35
53
|
import type { WebManifest } from '@rsc-kit/core/manifest-file'
|
|
@@ -38,85 +56,92 @@ export default {
|
|
|
38
56
|
id: '/',
|
|
39
57
|
name: 'Orders',
|
|
40
58
|
shortName: 'Orders',
|
|
59
|
+
description: 'Track and ship orders.',
|
|
60
|
+
startUrl: '/orders',
|
|
41
61
|
themeColor: '#0b0b0c',
|
|
42
62
|
backgroundColor: '#ffffff',
|
|
43
|
-
|
|
63
|
+
shortcuts: [
|
|
64
|
+
{ name: 'New order', shortName: 'New', url: '/orders/new', icons: ['icon-96.png'] },
|
|
65
|
+
],
|
|
44
66
|
} satisfies WebManifest
|
|
45
67
|
```
|
|
46
68
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
installed is left with an app their device no longer recognises as yours. `'/'`
|
|
50
|
-
is right for almost everyone; the build warns when it is missing.
|
|
51
|
-
|
|
52
|
-
The rest of the spec is typed as well, in its own names: `scope`, `orientation`,
|
|
53
|
-
`categories`, and `shortcuts` — the jumps a long press on the icon offers:
|
|
69
|
+
There is no layout to edit. React hoists the `<link>` and the `theme-color`
|
|
70
|
+
meta into `<head>`, so this works in an app with its own root layout.
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
72
|
+
**Set `id` once and never change it.** It is the app's identity. Without it a
|
|
73
|
+
browser derives one from `startUrl`, and changing that later leaves everyone
|
|
74
|
+
who installed with an app their device no longer recognises as yours. `'/'` is
|
|
75
|
+
right for almost everyone.
|
|
58
76
|
|
|
59
|
-
|
|
77
|
+
The file must be an object literal. It is read before there is a module graph,
|
|
78
|
+
so a computed or imported value is a build error rather than an app that is
|
|
79
|
+
quietly not installable.
|
|
60
80
|
|
|
61
|
-
|
|
62
|
-
layout. It is not build configuration — it is one more thing the app declares
|
|
63
|
-
about itself, so it lives where the app is, next to `layout.tsx` and
|
|
64
|
-
`not-found.tsx`.
|
|
81
|
+
### The fields
|
|
65
82
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
| Field | What it does |
|
|
84
|
+
| --- | --- |
|
|
85
|
+
| `id` | The app's identity. Set it to `'/'`. |
|
|
86
|
+
| `name`, `shortName`, `description` | What the install sheet and the home screen show. |
|
|
87
|
+
| `startUrl` | Where a launch lands. Defaults to `/`. |
|
|
88
|
+
| `scope` | The URLs that are the app. A link outside it opens in the browser. |
|
|
89
|
+
| `display` | `standalone` (the default), `fullscreen`, `minimal-ui` or `browser`. |
|
|
90
|
+
| `orientation` | Lock the installed app to portrait or landscape. |
|
|
91
|
+
| `categories` | What a store files it under, such as `['productivity']`. |
|
|
92
|
+
| `shortcuts` | The jumps a long press on the icon offers. |
|
|
93
|
+
| `themeColor` | The colour of the browser's chrome. Also written as a meta tag. |
|
|
94
|
+
| `backgroundColor` | The colour behind the app while it starts. |
|
|
95
|
+
| `icons`, `screenshots` | Usually left out: they are found by name. |
|
|
96
|
+
| `other` | Anything else the spec allows, passed through as written. |
|
|
97
|
+
|
|
98
|
+
### Icons
|
|
99
|
+
|
|
100
|
+
Put the icons next to `manifest.ts`. They are found by name, and their sizes
|
|
101
|
+
are read from the name:
|
|
71
102
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
103
|
+
```text
|
|
104
|
+
src/app/
|
|
105
|
+
manifest.ts
|
|
106
|
+
icon-192.png any, 192px
|
|
107
|
+
icon-512.png any, 512px
|
|
108
|
+
icon-maskable-512.png maskable, 512px
|
|
109
|
+
apple-icon.png the iOS home screen icon
|
|
110
|
+
```
|
|
76
111
|
|
|
77
|
-
|
|
78
|
-
it must be an object literal — not computed, not imported from elsewhere. A
|
|
79
|
-
file that is not gets a build error rather than an app that is quietly not
|
|
80
|
-
installable.
|
|
112
|
+
`icon-192.png` and `icon-192x192.png` both mean 192.
|
|
81
113
|
|
|
82
|
-
|
|
83
|
-
the
|
|
84
|
-
|
|
85
|
-
|
|
114
|
+
A file named `icon-maskable-*.png` is `maskable`. Draw it with a safe zone,
|
|
115
|
+
since a launcher only promises to show the middle 80%, and Android can mask it
|
|
116
|
+
to its own shape without cutting anything off. It goes in the manifest and
|
|
117
|
+
never in `<head>`, where the padding would make it the small icon in the tab.
|
|
86
118
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
own shape without cutting anything off. It goes in the manifest and never in
|
|
91
|
-
`<head>`, where the padding would make it the small icon in the tab. One file
|
|
92
|
-
marked both, which is what this used to do, is either shrunk or cropped, and
|
|
93
|
-
the Android splash screen drew the shrunk one.
|
|
119
|
+
To keep icons in `public/` instead, list them in `icons` as paths there:
|
|
120
|
+
`icons: ['icon-192.png', 'icon-512.png']`. A listed set replaces the files
|
|
121
|
+
found by name.
|
|
94
122
|
|
|
95
|
-
###
|
|
123
|
+
### What the build warns about
|
|
96
124
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
install it. Nothing is wrong and nothing works.
|
|
125
|
+
A manifest with no icons is valid. It parses, it links, and no browser ever
|
|
126
|
+
offers to install it. So the build says what is missing:
|
|
100
127
|
|
|
101
128
|
```text
|
|
102
129
|
[rsc-kit] manifest: no icons, so no browser will offer to install this. Add a 192px and a 512px png.
|
|
130
|
+
[rsc-kit] manifest: no 512px icon, so a splash screen will be upscaled from a smaller one.
|
|
131
|
+
[rsc-kit] manifest: no backgroundColor, so the Android splash screen is white.
|
|
103
132
|
[rsc-kit] manifest: no id, so the app's identity is its startUrl - change that and everyone who installed has a different app.
|
|
104
133
|
[rsc-kit] manifest: Orders is installable
|
|
105
134
|
```
|
|
106
135
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
installable and would otherwise find out months later.
|
|
136
|
+
These are warnings, not a failed build. The manifest still sets the theme
|
|
137
|
+
colour and the name.
|
|
110
138
|
|
|
111
|
-
##
|
|
139
|
+
## Adding an install button
|
|
112
140
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
effect has run, and an app listening in `useEffect` could miss it for good,
|
|
116
|
-
with no way to offer *Install* until a reload. So the page catches it in its
|
|
117
|
-
inline bootstrap, ahead of the runtime, and `useInstall()` hands it over:
|
|
141
|
+
`useInstall()` says whether the browser has offered to install the app, and
|
|
142
|
+
shows its dialog:
|
|
118
143
|
|
|
119
|
-
```tsx
|
|
144
|
+
```tsx title="src/components/InstallButton.tsx"
|
|
120
145
|
'use client'
|
|
121
146
|
import { useInstall } from '@rsc-kit/core/install'
|
|
122
147
|
|
|
@@ -131,26 +156,32 @@ export function InstallButton() {
|
|
|
131
156
|
}
|
|
132
157
|
```
|
|
133
158
|
|
|
134
|
-
- **`canInstall
|
|
159
|
+
- **`canInstall`**: the browser has offered. `install()` shows its dialog and
|
|
135
160
|
resolves with `accepted` or `dismissed`. An offer can be shown once, so after
|
|
136
|
-
that it
|
|
137
|
-
- **`ios
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
161
|
+
that it resolves with `unavailable` until the browser makes another.
|
|
162
|
+
- **`ios`**: Safari on an iPhone or iPad. iOS makes no offer, and the visitor
|
|
163
|
+
installs from the Share menu. The most the app can do is say so.
|
|
164
|
+
- **`installed`**: running as the installed app, or installed while this page
|
|
165
|
+
was open. Use it to hide the button, or for anything that should differ in
|
|
166
|
+
the app's own window.
|
|
167
|
+
|
|
168
|
+
To act on what the visitor chose:
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
async function onInstall() {
|
|
172
|
+
const outcome = await install()
|
|
143
173
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
174
|
+
if (outcome === 'accepted') track('installed')
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The browser's own install UI is left alone, so an app with no button loses
|
|
179
|
+
nothing.
|
|
147
180
|
|
|
148
|
-
##
|
|
181
|
+
## Showing screenshots in the install sheet
|
|
149
182
|
|
|
150
|
-
Chrome shows a plain install dialog by default, and a
|
|
151
|
-
|
|
152
|
-
bargain as the icons: name the files by their size, and the build reads the
|
|
153
|
-
rest.
|
|
183
|
+
Chrome shows a plain install dialog by default, and a fuller one, like an app
|
|
184
|
+
store listing, when the manifest has screenshots. Name the files by their size:
|
|
154
185
|
|
|
155
186
|
```text
|
|
156
187
|
src/app/
|
|
@@ -158,50 +189,41 @@ src/app/
|
|
|
158
189
|
screenshot-narrow-750x1334.png a phone
|
|
159
190
|
```
|
|
160
191
|
|
|
161
|
-
Each becomes a manifest `screenshots` entry with its `sizes` and a
|
|
162
|
-
of `wide` or `narrow`.
|
|
163
|
-
|
|
164
|
-
|
|
192
|
+
Each becomes a manifest `screenshots` entry with its `sizes` and a
|
|
193
|
+
`form_factor` of `wide` or `narrow`. Chrome wants at least one wide screenshot
|
|
194
|
+
for the desktop sheet and one narrow one for a phone. The build mentions them
|
|
195
|
+
when an otherwise installable app has none.
|
|
165
196
|
|
|
166
197
|
## Splash screens
|
|
167
198
|
|
|
168
|
-
What shows while an installed app starts.
|
|
169
|
-
and neither needs anything run over your images
|
|
199
|
+
What shows while an installed app starts. Android and iOS do it differently,
|
|
200
|
+
and neither needs anything run over your images. You make them, and the build
|
|
170
201
|
links them.
|
|
171
202
|
|
|
172
|
-
### Android
|
|
203
|
+
### Android
|
|
173
204
|
|
|
174
|
-
Chrome and Edge draw the splash
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
either missing:
|
|
178
|
-
|
|
179
|
-
```text
|
|
180
|
-
[rsc-kit] manifest: no 512px icon, so a splash screen will be upscaled from a smaller one.
|
|
181
|
-
[rsc-kit] manifest: no backgroundColor, so the Android splash screen is white.
|
|
182
|
-
```
|
|
205
|
+
Chrome and Edge draw the splash from three things the manifest already has:
|
|
206
|
+
the `name`, the largest icon and `backgroundColor`. Give it a 512px icon and a
|
|
207
|
+
background colour and it is done.
|
|
183
208
|
|
|
184
|
-
### iOS
|
|
209
|
+
### iOS
|
|
185
210
|
|
|
186
211
|
Safari ignores the manifest for this. It wants one image per device and
|
|
187
212
|
orientation, each behind a media query naming the device's size and pixel
|
|
188
|
-
ratio
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
So the image carries its size in its name, and the query comes from a table:
|
|
213
|
+
ratio, and with no match it shows a blank screen. Name each image by its size
|
|
214
|
+
and the build writes the query:
|
|
192
215
|
|
|
193
216
|
```text
|
|
194
217
|
src/app/
|
|
195
|
-
apple-splash-1179x2556.png iPhone 14 Pro, 15, 15 Pro, 16
|
|
196
|
-
apple-splash-2556x1179.png the same phones
|
|
197
|
-
apple-splash-1290x2796.png 14 Pro Max, 15 Plus
|
|
218
|
+
apple-splash-1179x2556.png iPhone 14 Pro, 15, 15 Pro, 16, portrait
|
|
219
|
+
apple-splash-2556x1179.png the same phones, landscape
|
|
220
|
+
apple-splash-1290x2796.png iPhone 14 Pro Max, 15 Plus, 15 Pro Max, 16 Plus
|
|
198
221
|
```
|
|
199
222
|
|
|
200
223
|
Each becomes `<link rel="apple-touch-startup-image" media="…">` with the right
|
|
201
|
-
query, and the page says it launches as an app, which is when Safari shows
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
every other device gets the background colour.
|
|
224
|
+
query, and the page says it launches as an app, which is when Safari shows
|
|
225
|
+
one. Make images for the devices you care about. Every other device gets the
|
|
226
|
+
background colour.
|
|
205
227
|
|
|
206
228
|
| Size (portrait) | Devices |
|
|
207
229
|
| --- | --- |
|
|
@@ -228,36 +250,15 @@ every other device gets the background colour.
|
|
|
228
250
|
| 1536×2048 | iPad mini, iPad Air, iPad 9.7" |
|
|
229
251
|
|
|
230
252
|
Swap the numbers for landscape. A size no device has is served but linked from
|
|
231
|
-
no page, and the build names it
|
|
253
|
+
no page, and the build names it:
|
|
232
254
|
|
|
233
255
|
```text
|
|
234
256
|
[rsc-kit] apple-splash: apple-splash-1000x2000.png matches no iPhone or iPad screen, so no page links to it.
|
|
235
257
|
```
|
|
236
258
|
|
|
237
|
-
|
|
238
|
-
before compression and well under 1 KB after, since the tags differ only in
|
|
239
|
-
numbers. Other browsers ignore them, and iOS fetches at most the one that
|
|
240
|
-
matches. An app with no `apple-splash-*` files gets no tags at all.
|
|
241
|
-
|
|
242
|
-
### One grey frame on a dark launch
|
|
243
|
-
|
|
244
|
-
In dark mode an installed app on iOS can show a single frame of grey (about
|
|
245
|
-
`#2C2C2E`) between the splash and the page. It is iOS's own: it starts
|
|
246
|
-
dismissing the splash one frame before the page has painted, and what shows
|
|
247
|
-
through for that frame is the empty web view's background — iOS's grey in
|
|
248
|
-
dark mode, white in light mode, which is why light mode looks clean.
|
|
249
|
-
|
|
250
|
-
Nothing in the page reaches it. `color-scheme`, an `html` background, the
|
|
251
|
-
cards and the placeholders are all drawn by the page, and this frame comes
|
|
252
|
-
before the page has drawn anything. It was chased through all of those on a
|
|
253
|
-
real phone before a frame-by-frame recording showed the whole screen lifting
|
|
254
|
-
evenly — the splash's pure-black icon plate included — which only a layer
|
|
255
|
-
behind the web view can do. It is 1/60 of a second; leave it.
|
|
259
|
+
### In metadata, the way Next writes it
|
|
256
260
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
A port from Next keeps its `appleWebApp` unchanged — the same shape, rendered
|
|
260
|
-
as the same tags:
|
|
261
|
+
A port from Next keeps its `appleWebApp` as it is, rendered as the same tags:
|
|
261
262
|
|
|
262
263
|
```ts title="src/app/layout.tsx"
|
|
263
264
|
export const metadata: Metadata = {
|
|
@@ -271,30 +272,15 @@ export const metadata: Metadata = {
|
|
|
271
272
|
```
|
|
272
273
|
|
|
273
274
|
`appleWebApp: true` is `{ capable: true }`. The files above are the easier way
|
|
274
|
-
to supply launch screens
|
|
275
|
+
to supply launch screens. This is for an app that already has its queries
|
|
275
276
|
written.
|
|
276
277
|
|
|
277
|
-
##
|
|
278
|
+
## Adding your own worker code
|
|
278
279
|
|
|
279
280
|
Push, notification clicks and background sync are events the generated worker
|
|
280
|
-
|
|
281
|
-
edit*. So it will import yours:
|
|
281
|
+
doesn't handle. Write them in `src/app/sw.js`:
|
|
282
282
|
|
|
283
283
|
```js title="src/app/sw.js"
|
|
284
|
-
// Plain javascript. The browser evaluates this in a worker scope with no build
|
|
285
|
-
// step in front of it, so what you write is what runs.
|
|
286
|
-
|
|
287
|
-
self.addEventListener('push', (event) => {
|
|
288
|
-
const payload = event.data ? event.data.json() : {}
|
|
289
|
-
|
|
290
|
-
event.waitUntil(
|
|
291
|
-
self.registration.showNotification(payload.title ?? 'Update', {
|
|
292
|
-
body: payload.body,
|
|
293
|
-
data: { url: payload.url ?? '/' },
|
|
294
|
-
}),
|
|
295
|
-
)
|
|
296
|
-
})
|
|
297
|
-
|
|
298
284
|
self.addEventListener('notificationclick', (event) => {
|
|
299
285
|
event.notification.close()
|
|
300
286
|
event.waitUntil(self.clients.openWindow(event.notification.data.url))
|
|
@@ -302,49 +288,55 @@ self.addEventListener('notificationclick', (event) => {
|
|
|
302
288
|
```
|
|
303
289
|
|
|
304
290
|
The build copies it beside the generated worker and imports it first, so your
|
|
305
|
-
listeners are registered before anything of ours
|
|
306
|
-
precached like everything else
|
|
307
|
-
then nothing is cached at all.
|
|
291
|
+
listeners are registered before anything of ours answers an event. It is
|
|
292
|
+
precached like everything else, and the build log says it was found:
|
|
308
293
|
|
|
309
294
|
```text
|
|
310
295
|
[rsc-kit] offline: 32 files precached as rsc-kit-3d9785e4, falling back to /offline, with app/sw.js
|
|
311
296
|
```
|
|
312
297
|
|
|
313
|
-
There is no option for
|
|
298
|
+
There is no option for it. The file is there or it isn't.
|
|
314
299
|
|
|
315
300
|
:::caution[`.js`, not `.ts`]
|
|
316
|
-
|
|
301
|
+
The browser evaluates it in a worker scope with no build step in front of it,
|
|
302
|
+
so what you write is what runs. TypeScript would not run.
|
|
317
303
|
:::
|
|
318
304
|
|
|
319
|
-
##
|
|
305
|
+
## Sending push notifications
|
|
320
306
|
|
|
321
|
-
|
|
307
|
+
Push takes five steps. Everything here is the web's own Push API and the
|
|
308
|
+
`web-push` package; the only part rsc-kit provides is `src/app/sw.js`.
|
|
322
309
|
|
|
323
|
-
### 1.
|
|
310
|
+
### 1. Generate keys
|
|
324
311
|
|
|
325
|
-
Push needs a VAPID key pair
|
|
326
|
-
browser, the private half signs what you send
|
|
312
|
+
Push needs a VAPID key pair. The public half identifies your server to the
|
|
313
|
+
browser, and the private half signs what you send:
|
|
327
314
|
|
|
328
315
|
```sh
|
|
329
316
|
bunx web-push generate-vapid-keys # or: npx web-push generate-vapid-keys
|
|
330
317
|
```
|
|
331
318
|
|
|
332
319
|
If either answers `web-push: command not found`, the runner found a copy of
|
|
333
|
-
`web-push` in the project
|
|
334
|
-
|
|
320
|
+
`web-push` in the project with no command inside it, usually a half-finished
|
|
321
|
+
install. Ask the library itself instead:
|
|
335
322
|
|
|
336
323
|
```sh
|
|
337
324
|
node -e "console.log(require('web-push').generateVAPIDKeys())"
|
|
338
325
|
```
|
|
339
326
|
|
|
340
|
-
Keep the private key
|
|
341
|
-
browser, so it goes in a `VITE_` variable
|
|
342
|
-
|
|
327
|
+
Keep the private key with your other secrets. The public one reaches the
|
|
328
|
+
browser, so it goes in a `VITE_` variable:
|
|
329
|
+
|
|
330
|
+
```sh title=".env"
|
|
331
|
+
VITE_VAPID_PUBLIC_KEY=BNc...
|
|
332
|
+
VAPID_PRIVATE_KEY=3Kq...
|
|
333
|
+
```
|
|
343
334
|
|
|
344
335
|
### 2. Ask, and subscribe
|
|
345
336
|
|
|
346
337
|
```tsx title="src/components/EnableNotifications.tsx"
|
|
347
338
|
'use client'
|
|
339
|
+
import { saveSubscription } from '../server/push'
|
|
348
340
|
|
|
349
341
|
export function EnableNotifications() {
|
|
350
342
|
async function enable() {
|
|
@@ -363,14 +355,13 @@ export function EnableNotifications() {
|
|
|
363
355
|
}
|
|
364
356
|
```
|
|
365
357
|
|
|
366
|
-
**Ask when they have a reason to say yes
|
|
367
|
-
how an app gets denied
|
|
368
|
-
|
|
358
|
+
**Ask when they have a reason to say yes**, such as right after they place an
|
|
359
|
+
order. A prompt on first load is how an app gets denied for good: the browser
|
|
360
|
+
remembers a refusal.
|
|
369
361
|
|
|
370
362
|
### 3. Store the subscription
|
|
371
363
|
|
|
372
|
-
An ordinary [server action](/guides/server-actions/)
|
|
373
|
-
no endpoint:
|
|
364
|
+
An ordinary [server action](/guides/server-actions/):
|
|
374
365
|
|
|
375
366
|
```ts title="src/server/push.ts"
|
|
376
367
|
'use server'
|
|
@@ -382,12 +373,12 @@ export const saveSubscription = client.input(subscriptionSchema).handler(
|
|
|
382
373
|
)
|
|
383
374
|
```
|
|
384
375
|
|
|
385
|
-
Store it against a **user**, not a session. A subscription outlives the
|
|
386
|
-
that created it
|
|
376
|
+
Store it against a **user**, not a session. A subscription outlives the
|
|
377
|
+
session that created it.
|
|
387
378
|
|
|
388
379
|
### 4. Send
|
|
389
380
|
|
|
390
|
-
From wherever the event happens
|
|
381
|
+
From wherever the event happens, such as a queue, a cron or a webhook:
|
|
391
382
|
|
|
392
383
|
```ts
|
|
393
384
|
import webpush from 'web-push'
|
|
@@ -398,11 +389,12 @@ for (const subscription of await db.pushSubscriptions.forUser(userId)) {
|
|
|
398
389
|
try {
|
|
399
390
|
await webpush.sendNotification(subscription, JSON.stringify({
|
|
400
391
|
title: 'Your order shipped',
|
|
392
|
+
body: 'Order #42 is on its way.',
|
|
401
393
|
url: '/orders/42',
|
|
402
394
|
}))
|
|
403
395
|
} catch (error) {
|
|
404
|
-
// 404 and 410 mean the subscription is dead
|
|
405
|
-
// the browser rotated it. Delete it rather than retrying forever.
|
|
396
|
+
// 404 and 410 mean the subscription is dead: the app was uninstalled,
|
|
397
|
+
// or the browser rotated it. Delete it rather than retrying forever.
|
|
406
398
|
if (error.statusCode === 404 || error.statusCode === 410) {
|
|
407
399
|
await db.pushSubscriptions.remove(subscription.endpoint)
|
|
408
400
|
}
|
|
@@ -410,20 +402,44 @@ for (const subscription of await db.pushSubscriptions.forUser(userId)) {
|
|
|
410
402
|
}
|
|
411
403
|
```
|
|
412
404
|
|
|
413
|
-
|
|
414
|
-
|
|
405
|
+
Don't skip the last branch. Without it, a sender's list fills with dead
|
|
406
|
+
endpoints.
|
|
415
407
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
`app/sw.js`. A wrapper would be a thin layer over a stable standard, and one
|
|
420
|
-
more thing to keep current.
|
|
421
|
-
:::
|
|
408
|
+
### 5. Show it
|
|
409
|
+
|
|
410
|
+
The worker shows the notification, and opens its URL when it is clicked:
|
|
422
411
|
|
|
423
|
-
|
|
412
|
+
```js title="src/app/sw.js"
|
|
413
|
+
self.addEventListener('push', (event) => {
|
|
414
|
+
const payload = event.data ? event.data.json() : {}
|
|
424
415
|
|
|
425
|
-
|
|
426
|
-
|
|
416
|
+
event.waitUntil(
|
|
417
|
+
self.registration.showNotification(payload.title ?? 'Update', {
|
|
418
|
+
body: payload.body,
|
|
419
|
+
data: { url: payload.url ?? '/' },
|
|
420
|
+
}),
|
|
421
|
+
)
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
self.addEventListener('notificationclick', (event) => {
|
|
425
|
+
event.notification.close()
|
|
426
|
+
event.waitUntil(self.clients.openWindow(event.notification.data.url))
|
|
427
|
+
})
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
## Syncing in the background
|
|
431
|
+
|
|
432
|
+
For work that must happen even if the visitor closes the tab, such as a
|
|
433
|
+
queued message or an offline edit. Register a sync after queueing the work:
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
const registration = await navigator.serviceWorker.ready
|
|
437
|
+
|
|
438
|
+
if ('sync' in registration) await registration.sync.register('outbox')
|
|
439
|
+
else await flushNow()
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
Then handle it in the worker:
|
|
427
443
|
|
|
428
444
|
```js title="src/app/sw.js"
|
|
429
445
|
self.addEventListener('sync', (event) => {
|
|
@@ -440,36 +456,74 @@ async function flushOutbox() {
|
|
|
440
456
|
body: JSON.stringify(item.body),
|
|
441
457
|
})
|
|
442
458
|
|
|
443
|
-
// Left in the queue on failure, so the browser retries the
|
|
444
|
-
// later. Removed only once the server has it.
|
|
459
|
+
// Left in the queue on failure, so the browser retries the sync later.
|
|
445
460
|
if (response.ok) await db.delete('outbox', item.id)
|
|
446
461
|
}
|
|
447
462
|
}
|
|
448
463
|
```
|
|
449
464
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
const registration = await navigator.serviceWorker.ready
|
|
454
|
-
await registration.sync.register('outbox')
|
|
455
|
-
```
|
|
465
|
+
Only Chromium supports it; Safari and Firefox don't. Treat it as an
|
|
466
|
+
optimisation over an ordinary retry, which is why the example falls back to
|
|
467
|
+
`flushNow()`.
|
|
456
468
|
|
|
457
469
|
:::danger[Make the endpoint idempotent]
|
|
458
|
-
The browser decides when a sync runs and may run it more than once
|
|
459
|
-
that succeeded on the server but whose response never arrived
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
Give each queued item an id the client generates, and have the endpoint
|
|
463
|
-
one it has already seen.
|
|
464
|
-
|
|
465
|
-
"already seen" means.
|
|
470
|
+
The browser decides when a sync runs and may run it more than once. A request
|
|
471
|
+
that succeeded on the server but whose response never arrived is retried, and
|
|
472
|
+
a message posted twice was sent twice.
|
|
473
|
+
|
|
474
|
+
Give each queued item an id the client generates, and have the endpoint
|
|
475
|
+
ignore one it has already seen. Only your data model knows what "already seen"
|
|
476
|
+
means, so no framework can do this for you.
|
|
466
477
|
:::
|
|
467
478
|
|
|
468
|
-
|
|
469
|
-
Firefox do not. Treat it as an optimisation over an ordinary retry, never as the
|
|
470
|
-
only path:
|
|
479
|
+
## Moving to another domain
|
|
471
480
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
481
|
+
Redirecting the old origin, such as `www.example.com` to `example.com`, needs one
|
|
482
|
+
exception: keep serving `/sw.js` on the old origin, without a redirect.
|
|
483
|
+
|
|
484
|
+
```txt
|
|
485
|
+
www.example.com/* → 301 https://example.com/:path
|
|
486
|
+
www.example.com/sw.js → served by the app, as before
|
|
475
487
|
```
|
|
488
|
+
|
|
489
|
+
A browser never updates a worker whose script redirects. A visitor's worker on the
|
|
490
|
+
old origin would keep answering stored pages from its cache, and never reach the
|
|
491
|
+
redirect. With `/sw.js` still served there, the browser picks up the new worker
|
|
492
|
+
on their next visit.
|
|
493
|
+
|
|
494
|
+
From 0.42.1 the worker notices on its own when a request fails because its
|
|
495
|
+
url now redirects. It clears its caches, unregisters, and reloads the page, so
|
|
496
|
+
the reload reaches the redirect. A visitor whose worker is older than that
|
|
497
|
+
needs the exception, and keep it for a few weeks so returning visitors pass
|
|
498
|
+
through.
|
|
499
|
+
|
|
500
|
+
## How it works
|
|
501
|
+
|
|
502
|
+
- **The manifest is a file, not config or metadata.** It isn't build
|
|
503
|
+
configuration; it is something the app declares about itself, so it lives
|
|
504
|
+
beside `layout.tsx`. It isn't `metadata` either: metadata is resolved per
|
|
505
|
+
route and can be computed per request, while a manifest is one file for the
|
|
506
|
+
whole app, so there is nothing to override per route.
|
|
507
|
+
- **Icon sizes come from the filename.** Declaring a size in the config as
|
|
508
|
+
well would be the same number written twice, and the copy that drifts is
|
|
509
|
+
the one nobody looks at. Nothing in rsc-kit draws, resizes or encodes an
|
|
510
|
+
image, at build, in dev or at runtime.
|
|
511
|
+
- **The install offer is caught before the runtime.** The
|
|
512
|
+
`beforeinstallprompt` event fires once, whenever the page qualifies, and
|
|
513
|
+
that can be before any component's effect has run. The page's inline
|
|
514
|
+
bootstrap catches it and `useInstall()` hands it over, so a listener in
|
|
515
|
+
`useEffect` can't miss it. Only an app that declares a manifest has the
|
|
516
|
+
capture.
|
|
517
|
+
- **Splash links are cheap.** A full set of `apple-splash-*` files adds one
|
|
518
|
+
`<link>` per image, about 4 KB before compression and well under 1 KB after.
|
|
519
|
+
Other browsers ignore them, and iOS fetches at most the one that matches. An
|
|
520
|
+
app with no splash files gets no tags.
|
|
521
|
+
- **One grey frame on a dark launch.** In dark mode an installed app on iOS
|
|
522
|
+
can show a single frame of grey (about `#2C2C2E`) between the splash and the
|
|
523
|
+
page. iOS starts dismissing the splash one frame before the page has
|
|
524
|
+
painted, and that frame shows the empty web view, grey in dark mode and
|
|
525
|
+
white in light mode. Nothing in the page can reach it. It is 1/60 of a
|
|
526
|
+
second; leave it.
|
|
527
|
+
- **There is no push helper.** Push is the web's Push API and `web-push`. A
|
|
528
|
+
wrapper would be a thin layer over a stable standard, so the only part
|
|
529
|
+
rsc-kit owns is the place to put the listener, `src/app/sw.js`.
|