@rsc-kit/mcp 0.41.0 → 0.42.0
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 +12 -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 +251 -218
- 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/feature-flags.md
CHANGED
|
@@ -2,52 +2,97 @@
|
|
|
2
2
|
|
|
3
3
|
> Vercel's Flags SDK runs unchanged — flags/next, without Next.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Next: `next/headers`, for `headers()` and `cookies()`. Those exist here
|
|
9
|
-
under the same names and with the same shapes, one object per request,
|
|
10
|
-
which is what the SDK's per-request dedupe keys on. The build answers
|
|
11
|
-
`next/headers` with them, so the SDK runs as written:
|
|
5
|
+
Use the [Flags SDK](https://flags-sdk.dev) as its docs show it. Its Next
|
|
6
|
+
integration, `flags/next`, works here without Next, so there is nothing to
|
|
7
|
+
install from rsc-kit:
|
|
12
8
|
|
|
13
9
|
```sh
|
|
14
10
|
bun add flags
|
|
15
11
|
```
|
|
16
12
|
|
|
13
|
+
```ts title="src/flags.ts"
|
|
14
|
+
import { flag } from 'flags/next'
|
|
15
|
+
|
|
16
|
+
export const showBanner = flag<boolean>({
|
|
17
|
+
key: 'show-banner',
|
|
18
|
+
decide: () => false,
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```tsx title="src/app/page.tsx"
|
|
23
|
+
import { showBanner } from '../flags'
|
|
24
|
+
|
|
25
|
+
export default async function Home() {
|
|
26
|
+
return (await showBanner()) ? <Banner /> : null
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Deciding per visitor
|
|
31
|
+
|
|
32
|
+
A flag usually depends on who is asking. `identify` reads the request, and
|
|
33
|
+
`dedupe` makes it run once per request however many flags use it:
|
|
34
|
+
|
|
17
35
|
```ts title="src/flags.ts"
|
|
18
36
|
import { flag, dedupe } from 'flags/next'
|
|
19
37
|
|
|
20
|
-
|
|
38
|
+
type Visitor = { id: string; country: string }
|
|
39
|
+
|
|
40
|
+
const visitor = dedupe(async ({ cookies, headers }): Promise<Visitor> => ({
|
|
21
41
|
id: cookies.get('visitor')?.value ?? 'anonymous',
|
|
22
42
|
country: headers.get('x-vercel-ip-country') ?? headers.get('cf-ipcountry') ?? '',
|
|
23
43
|
}))
|
|
24
44
|
|
|
25
|
-
export const showBanner = flag<boolean,
|
|
45
|
+
export const showBanner = flag<boolean, Visitor>({
|
|
26
46
|
key: 'show-banner',
|
|
27
47
|
identify: visitor,
|
|
28
48
|
decide: ({ entities }) => entities?.country === 'GB',
|
|
29
49
|
})
|
|
50
|
+
|
|
51
|
+
export const newCheckout = flag<boolean, Visitor>({
|
|
52
|
+
key: 'new-checkout',
|
|
53
|
+
identify: visitor,
|
|
54
|
+
decide: ({ entities }) => entities?.id.endsWith('7') ?? false,
|
|
55
|
+
})
|
|
30
56
|
```
|
|
31
57
|
|
|
58
|
+
An adapter, such as Statsig, LaunchDarkly or Vercel's own, belongs to the SDK.
|
|
59
|
+
Configure it the way its docs say.
|
|
60
|
+
|
|
61
|
+
## Reading a flag in a page
|
|
62
|
+
|
|
63
|
+
A flag reads the request, so the part of the page that awaits it renders per
|
|
64
|
+
visitor. Put it under `<Suspense>`, and the build stores the rest of the page
|
|
65
|
+
as a shell:
|
|
66
|
+
|
|
32
67
|
```tsx title="src/app/page.tsx"
|
|
68
|
+
import { Suspense } from 'react'
|
|
33
69
|
import { showBanner } from '../flags'
|
|
34
70
|
|
|
35
|
-
export default
|
|
36
|
-
|
|
71
|
+
export default function Home() {
|
|
72
|
+
return (
|
|
73
|
+
<>
|
|
74
|
+
<Suspense fallback={null}>
|
|
75
|
+
<MaybeBanner />
|
|
76
|
+
</Suspense>
|
|
77
|
+
<h1>Welcome</h1>
|
|
78
|
+
</>
|
|
79
|
+
)
|
|
80
|
+
}
|
|
37
81
|
|
|
38
|
-
|
|
82
|
+
async function MaybeBanner() {
|
|
83
|
+
return (await showBanner()) ? <Banner /> : null
|
|
39
84
|
}
|
|
40
85
|
```
|
|
41
86
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
87
|
+
The build's table says `headers() in run, cookies() in run stream per request`
|
|
88
|
+
for that page. That is the same line any `headers()` call earns; see
|
|
89
|
+
[Static generation](/guides/static-generation). A flag read under a boundary
|
|
90
|
+
costs one streamed hole, not the page.
|
|
91
|
+
|
|
92
|
+
## The discovery endpoint
|
|
48
93
|
|
|
49
|
-
The
|
|
50
|
-
has:
|
|
94
|
+
The Flags Explorer finds your flags through a route handler, and a `route.ts`
|
|
95
|
+
already has that shape:
|
|
51
96
|
|
|
52
97
|
```ts title="src/app/.well-known/vercel/flags/route.ts"
|
|
53
98
|
import { createFlagsDiscoveryEndpoint, getProviderData } from 'flags/next'
|
|
@@ -57,19 +102,44 @@ export const GET = createFlagsDiscoveryEndpoint(async () => getProviderData(flag
|
|
|
57
102
|
export const openapi = false
|
|
58
103
|
```
|
|
59
104
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
105
|
+
## Testing a flag
|
|
106
|
+
|
|
107
|
+
A test isn't built, so it needs the alias the build adds. The scaffold's test
|
|
108
|
+
preload ships it:
|
|
64
109
|
|
|
65
110
|
```ts title="tests/preload.ts"
|
|
66
111
|
mock.module('next/headers', () => import('@rsc-kit/core/request'))
|
|
67
112
|
```
|
|
68
113
|
|
|
69
|
-
|
|
70
|
-
|
|
114
|
+
Without it, `flags/next` finds Next's own `next/headers`, installed by some
|
|
115
|
+
other app in a monorepo, and throws that `headers` was called outside a
|
|
116
|
+
request scope.
|
|
117
|
+
|
|
118
|
+
Then evaluate the flag inside `withRequest()`, the way anything that reads the
|
|
119
|
+
request is tested:
|
|
120
|
+
|
|
121
|
+
```ts title="tests/flags.test.ts"
|
|
122
|
+
import { withRequest } from '@rsc-kit/core/request'
|
|
123
|
+
import { showBanner } from '../src/flags'
|
|
124
|
+
|
|
125
|
+
test('visitors from the UK see the banner', async () => {
|
|
126
|
+
const request = new Request('https://app.test/', { headers: { 'cf-ipcountry': 'GB' } })
|
|
127
|
+
|
|
128
|
+
expect(await withRequest(request, () => showBanner())).toBe(true)
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
See [Testing](/guides/testing).
|
|
133
|
+
|
|
134
|
+
## What doesn't carry over
|
|
135
|
+
|
|
136
|
+
`precompute()` doesn't. It is built on a Next middleware rewriting the URL to
|
|
137
|
+
a permutation, which is Next's routing. Read the flag in the page instead,
|
|
138
|
+
under a boundary.
|
|
139
|
+
|
|
140
|
+
## How it works
|
|
71
141
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
142
|
+
`flags/next` needs one thing from Next: `next/headers`, for `headers()` and
|
|
143
|
+
`cookies()`. Those exist here under the same names and with the same shapes,
|
|
144
|
+
one object per request, which is what the SDK's per-request dedupe keys on.
|
|
145
|
+
The build answers `next/headers` with them, so the SDK runs as written.
|
package/guides/file-uploads.md
CHANGED
|
@@ -2,11 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
> Sending files through a server action without encoding them.
|
|
4
4
|
|
|
5
|
-
A
|
|
6
|
-
|
|
7
|
-
on the way in.
|
|
5
|
+
A server action can take a `File` as an argument. It travels as binary, so
|
|
6
|
+
there is nothing to encode in the browser and nothing to decode on the server:
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
```ts title="src/actions.ts"
|
|
9
|
+
'use server'
|
|
10
|
+
|
|
11
|
+
export async function uploadAvatar(file: File) {
|
|
12
|
+
console.log(file.name, file.type, file.size)
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
It is the same call and the same round trip as [any other server
|
|
17
|
+
action](/guides/server-actions).
|
|
18
|
+
|
|
19
|
+
## Uploading one file
|
|
20
|
+
|
|
21
|
+
Read the file from the form in a client component and pass it to the action:
|
|
10
22
|
|
|
11
23
|
```tsx title="src/components/AvatarUpload.tsx"
|
|
12
24
|
"use client";
|
|
@@ -35,7 +47,7 @@ export function AvatarUpload() {
|
|
|
35
47
|
}
|
|
36
48
|
```
|
|
37
49
|
|
|
38
|
-
The action receives the `File` itself
|
|
50
|
+
The action receives the `File` itself, with its name, type and size:
|
|
39
51
|
|
|
40
52
|
```ts title="src/actions.ts"
|
|
41
53
|
'use server'
|
|
@@ -43,22 +55,21 @@ The action receives the `File` itself — name, type, size and all:
|
|
|
43
55
|
import { writeFile } from 'node:fs/promises';
|
|
44
56
|
|
|
45
57
|
export async function uploadAvatar(file: File): Promise<string> {
|
|
46
|
-
const
|
|
58
|
+
const name = `${crypto.randomUUID()}.${file.type.split('/')[1]}`;
|
|
47
59
|
|
|
48
|
-
await writeFile(`./public/avatars/${
|
|
60
|
+
await writeFile(`./public/avatars/${name}`, new Uint8Array(await file.arrayBuffer()));
|
|
49
61
|
|
|
50
|
-
return `/avatars/${
|
|
62
|
+
return `/avatars/${name}`;
|
|
51
63
|
}
|
|
52
64
|
```
|
|
53
65
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
action](/guides/server-actions).
|
|
66
|
+
Name the stored file yourself. `file.name` comes from the visitor's computer and
|
|
67
|
+
can be anything, `../` included.
|
|
57
68
|
|
|
58
|
-
##
|
|
69
|
+
## Sending the whole form
|
|
59
70
|
|
|
60
|
-
An action can take the `FormData` instead
|
|
61
|
-
|
|
71
|
+
An action can take the `FormData` instead. That is simpler when the form mixes
|
|
72
|
+
files and fields:
|
|
62
73
|
|
|
63
74
|
```tsx
|
|
64
75
|
"use client";
|
|
@@ -88,10 +99,14 @@ export async function createPost(formData: FormData) {
|
|
|
88
99
|
}
|
|
89
100
|
```
|
|
90
101
|
|
|
91
|
-
##
|
|
102
|
+
## Uploading several files
|
|
103
|
+
|
|
104
|
+
`formData.getAll` returns every file from a `multiple` input, and an array of
|
|
105
|
+
`File` travels as easily as one:
|
|
92
106
|
|
|
93
|
-
|
|
94
|
-
|
|
107
|
+
```tsx
|
|
108
|
+
<input type="file" name="photos" accept="image/*" multiple />
|
|
109
|
+
```
|
|
95
110
|
|
|
96
111
|
```tsx
|
|
97
112
|
const files = formData.getAll('photos') as File[];
|
|
@@ -109,19 +124,79 @@ export async function uploadPhotos(files: File[]) {
|
|
|
109
124
|
}
|
|
110
125
|
```
|
|
111
126
|
|
|
112
|
-
##
|
|
127
|
+
## Checking the file
|
|
128
|
+
|
|
129
|
+
`accept` only filters the browser's file picker. Check the type and size in the
|
|
130
|
+
action, where it can't be skipped:
|
|
131
|
+
|
|
132
|
+
```ts title="src/actions.ts"
|
|
133
|
+
'use server'
|
|
134
|
+
|
|
135
|
+
const allowed = ['image/png', 'image/jpeg', 'image/webp'];
|
|
113
136
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
137
|
+
export async function uploadAvatar(file: File) {
|
|
138
|
+
if (!allowed.includes(file.type)) {
|
|
139
|
+
return { error: 'Choose a PNG, JPEG or WebP image.' };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (file.size > 2 * 1024 * 1024) {
|
|
143
|
+
return { error: 'The image must be under 2 MB.' };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// store it …
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
[Validation](/guides/validation) covers showing errors like these beside the
|
|
151
|
+
field.
|
|
152
|
+
|
|
153
|
+
## Raising the size limit
|
|
154
|
+
|
|
155
|
+
The server refuses a body over **8 MB** with a 413, before it holds a byte of
|
|
156
|
+
it, and the form reports a failed submission. Raise the limit when the app
|
|
157
|
+
uploads more:
|
|
119
158
|
|
|
120
159
|
```ts title="vite.config.ts"
|
|
121
160
|
rscKit({ maxActionBody: 32 * 1024 * 1024 })
|
|
122
161
|
```
|
|
123
162
|
|
|
124
|
-
|
|
163
|
+
The whole body is held in memory before the action runs: it is one `POST`, not
|
|
164
|
+
a stream. Keep the limit, and any limit a proxy in front sets, as low as the
|
|
165
|
+
app needs.
|
|
166
|
+
|
|
167
|
+
## Uploading large files to storage
|
|
168
|
+
|
|
169
|
+
For large files, skip the action for the bytes. The action hands out a
|
|
170
|
+
pre-signed URL, and the browser uploads straight to storage:
|
|
171
|
+
|
|
172
|
+
```ts title="src/actions.ts"
|
|
173
|
+
'use server'
|
|
174
|
+
|
|
175
|
+
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
|
|
176
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
177
|
+
|
|
178
|
+
const s3 = new S3Client({});
|
|
179
|
+
|
|
180
|
+
export async function uploadUrl(type: string) {
|
|
181
|
+
const key = `videos/${crypto.randomUUID()}`;
|
|
182
|
+
const url = await getSignedUrl(s3, new PutObjectCommand({ Bucket: 'uploads', Key: key, ContentType: type }), { expiresIn: 300 });
|
|
183
|
+
|
|
184
|
+
return { key, url };
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
```tsx
|
|
189
|
+
"use client";
|
|
190
|
+
|
|
191
|
+
import { uploadUrl } from '../actions';
|
|
192
|
+
|
|
193
|
+
async function upload(file: File) {
|
|
194
|
+
const { key, url } = await uploadUrl(file.type);
|
|
195
|
+
|
|
196
|
+
await fetch(url, { method: 'PUT', body: file, headers: { 'Content-Type': file.type } });
|
|
197
|
+
|
|
198
|
+
return key;
|
|
199
|
+
}
|
|
200
|
+
```
|
|
125
201
|
|
|
126
|
-
|
|
127
|
-
- For genuinely large files, do not send them through an action at all. Have the action mint a pre-signed URL and let the browser upload straight to storage; the bytes never touch your server.
|
|
202
|
+
The bytes never touch your server, so the size limit doesn't apply.
|
package/guides/fonts.md
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
1
1
|
# Fonts
|
|
2
2
|
|
|
3
|
-
> Self-hosted fonts from npm, and
|
|
3
|
+
> Self-hosted fonts from npm, preloading the first one, and porting from next/font.
|
|
4
4
|
|
|
5
|
-
Install the font, import its CSS, name it in a variable. Vite does
|
|
5
|
+
Install the font from npm, import its CSS, and name it in a variable. Vite does
|
|
6
|
+
the rest:
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
bun add @fontsource-variable/geist
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```css title="src/app/styles.css"
|
|
13
|
+
@import '@fontsource-variable/geist';
|
|
14
|
+
|
|
15
|
+
:root {
|
|
16
|
+
--font-sans: 'Geist Variable', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
|
|
17
|
+
'Helvetica Neue', Arial, sans-serif,
|
|
18
|
+
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
[Fontsource](https://fontsource.org) packages every Google font, and many
|
|
23
|
+
others, as woff2 files with the `@font-face` rules already written. Vite hashes
|
|
24
|
+
the files and serves them beside your other assets. **Nothing is fetched from
|
|
25
|
+
Google at runtime**, and nothing is downloaded at build time either: the files
|
|
26
|
+
are in `node_modules`.
|
|
27
|
+
|
|
28
|
+
## Using more than one font
|
|
29
|
+
|
|
30
|
+
Import each one and give each its own variable:
|
|
6
31
|
|
|
7
32
|
```sh
|
|
8
33
|
bun add @fontsource-variable/fraunces @fontsource-variable/geist
|
|
@@ -19,29 +44,72 @@ bun add @fontsource-variable/fraunces @fontsource-variable/geist
|
|
|
19
44
|
'Helvetica Neue', Arial, sans-serif,
|
|
20
45
|
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
|
|
21
46
|
}
|
|
47
|
+
|
|
48
|
+
h1 {
|
|
49
|
+
font-family: var(--font-display);
|
|
50
|
+
}
|
|
22
51
|
```
|
|
23
52
|
|
|
24
|
-
|
|
25
|
-
|
|
53
|
+
`full.css` carries every axis of a variable font, and `standard.css` is weight
|
|
54
|
+
alone. `full-italic.css` adds the italics.
|
|
55
|
+
|
|
56
|
+
## Keeping the fallback stack
|
|
57
|
+
|
|
58
|
+
Put your font in front of a full stack, not in place of one. The rest of the
|
|
59
|
+
stack does two things a bare `sans-serif` doesn't:
|
|
26
60
|
|
|
27
|
-
- **Emoji.** Geist has no emoji glyphs, so an emoji falls through to whatever
|
|
28
|
-
named after it. With nothing there, some Linux and older Android setups
|
|
29
|
-
monochrome glyph or a box. The four emoji fonts at the end are
|
|
30
|
-
for characters the main font
|
|
61
|
+
- **Emoji.** Geist has no emoji glyphs, so an emoji falls through to whatever
|
|
62
|
+
is named after it. With nothing there, some Linux and older Android setups
|
|
63
|
+
draw a monochrome glyph or a box. The four emoji fonts at the end are used
|
|
64
|
+
only for characters the main font can't draw, so they cost nothing.
|
|
31
65
|
- **A closer fallback.** During the `swap` window the browser shows the next
|
|
32
|
-
font. `-apple-system` and `Segoe UI` are
|
|
33
|
-
than Helvetica or Arial, so the
|
|
66
|
+
font. `-apple-system` and `Segoe UI` are closer to Geist in width and
|
|
67
|
+
x-height than Helvetica or Arial, so the swap is a smaller jolt.
|
|
68
|
+
|
|
69
|
+
These are Tailwind's own defaults. shadcn's generated `--font-sans:
|
|
70
|
+
'Geist Variable', sans-serif` drops both, so keep the stack.
|
|
71
|
+
|
|
72
|
+
## Preloading the first font
|
|
73
|
+
|
|
74
|
+
Import the file with `?url` and add a preload link. React hoists it into the
|
|
75
|
+
`<head>`:
|
|
76
|
+
|
|
77
|
+
```tsx title="src/app/layout.tsx"
|
|
78
|
+
import fraunces from '@fontsource-variable/fraunces/files/fraunces-latin-full-normal.woff2?url'
|
|
34
79
|
|
|
35
|
-
|
|
36
|
-
|
|
80
|
+
export default function RootLayout({ children }) {
|
|
81
|
+
return (
|
|
82
|
+
<html>
|
|
83
|
+
<head>
|
|
84
|
+
<link rel="preload" href={fraunces} as="font" type="font/woff2" crossOrigin="anonymous" />
|
|
85
|
+
</head>
|
|
86
|
+
…
|
|
87
|
+
```
|
|
37
88
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
fetched from Google at runtime**, and nothing is downloaded at build time
|
|
42
|
-
either. The files are in `node_modules`.
|
|
89
|
+
Without a preload, the browser finds the font only when it parses the CSS, and
|
|
90
|
+
the text shows in the fallback a moment longer. `?url` is Vite's: it returns
|
|
91
|
+
the hashed path the build serves.
|
|
43
92
|
|
|
44
|
-
|
|
93
|
+
Preload the one file the first paint needs, usually the Latin regular.
|
|
94
|
+
Preloading all of them defeats the subsetting.
|
|
95
|
+
|
|
96
|
+
## A font that isn't on npm
|
|
97
|
+
|
|
98
|
+
Put the file in `public/` and write its `@font-face` yourself:
|
|
99
|
+
|
|
100
|
+
```css title="src/app/styles.css"
|
|
101
|
+
@font-face {
|
|
102
|
+
font-family: 'Brand Sans';
|
|
103
|
+
font-weight: 400;
|
|
104
|
+
font-display: swap;
|
|
105
|
+
src: url('/fonts/brand-sans.woff2') format('woff2');
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Or put it in `src/` and import it with `?url`, as in the preload above, so the
|
|
110
|
+
build hashes it.
|
|
111
|
+
|
|
112
|
+
## Coming from next/font
|
|
45
113
|
|
|
46
114
|
```ts
|
|
47
115
|
// before
|
|
@@ -54,18 +122,18 @@ const fraunces = Fraunces({
|
|
|
54
122
|
})
|
|
55
123
|
```
|
|
56
124
|
|
|
57
|
-
|
|
125
|
+
Each option is something Fontsource already does:
|
|
58
126
|
|
|
59
127
|
| `next/font` | Fontsource |
|
|
60
128
|
| --- | --- |
|
|
61
|
-
| `variable: '--font-display'` | the `:root` rule above
|
|
129
|
+
| `variable: '--font-display'` | the `:root` rule above: you write the variable, once |
|
|
62
130
|
| `subsets: ['latin']` | every subset ships, each with a `unicode-range`; the browser fetches only the ones the page uses |
|
|
63
131
|
| `style: ['normal', 'italic']` | `full.css` and `full-italic.css` |
|
|
64
|
-
| `axes: ['SOFT', 'opsz']` | `full.css
|
|
132
|
+
| `axes: ['SOFT', 'opsz']` | `full.css`, every axis of the variable font; `standard.css` is weight alone |
|
|
65
133
|
| `display: 'swap'` | already in every rule |
|
|
66
|
-
| `className={fraunces.variable}` | nothing
|
|
134
|
+
| `className={fraunces.variable}` | nothing: the variable is on `:root` |
|
|
67
135
|
|
|
68
|
-
|
|
136
|
+
The layout loses its font imports, and the `className` gains nothing:
|
|
69
137
|
|
|
70
138
|
```tsx title="src/app/layout.tsx"
|
|
71
139
|
import './styles.css'
|
|
@@ -79,88 +147,21 @@ export default function RootLayout({ children }) {
|
|
|
79
147
|
}
|
|
80
148
|
```
|
|
81
149
|
|
|
82
|
-
The subsetting is
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
fetches one file whatever else is installed.
|
|
87
|
-
|
|
88
|
-
## Preloading
|
|
150
|
+
The subsetting is better than before. `next/font` ships the subsets you name at
|
|
151
|
+
build time. Fontsource ships all of them, each behind a `unicode-range`, and the
|
|
152
|
+
browser downloads only the ones the page's text needs. A Latin-only page
|
|
153
|
+
fetches one file, whatever else is installed.
|
|
89
154
|
|
|
90
|
-
The one thing `next/font` did that needs a line here
|
|
91
|
-
|
|
92
|
-
does. Without one, the font is discovered when the CSS is parsed, and the text
|
|
93
|
-
shows in the fallback face for a moment longer.
|
|
94
|
-
|
|
95
|
-
Import the file for its url, and React hoists the link:
|
|
96
|
-
|
|
97
|
-
```tsx title="src/app/layout.tsx"
|
|
98
|
-
import fraunces from '@fontsource-variable/fraunces/files/fraunces-latin-full-normal.woff2?url'
|
|
99
|
-
|
|
100
|
-
export default function RootLayout({ children }) {
|
|
101
|
-
return (
|
|
102
|
-
<html>
|
|
103
|
-
<head>
|
|
104
|
-
<link rel="preload" href={fraunces} as="font" type="font/woff2" crossOrigin="anonymous" />
|
|
105
|
-
</head>
|
|
106
|
-
…
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
`?url` is Vite's — it hands back the hashed path the build will serve. Preload
|
|
110
|
-
the one file the first paint needs, usually the Latin regular; preloading all
|
|
111
|
-
of them defeats the subsetting.
|
|
112
|
-
|
|
113
|
-
## A font never blocks the page
|
|
114
|
-
|
|
115
|
-
Text paints before the web font arrives, always. That is not a tuning
|
|
116
|
-
choice; it is the rule everything below serves, and a font can break it in
|
|
117
|
-
three ways, each of which the setup above has already closed:
|
|
118
|
-
|
|
119
|
-
- **A stylesheet from another origin.** `<link href="https://fonts.googleapis.com/…">`
|
|
120
|
-
is render-blocking CSS from a host the browser has not connected to: a DNS
|
|
121
|
-
lookup, a TLS handshake and a round trip before the first paint, on every
|
|
122
|
-
cold load. Self-hosting through Fontsource keeps the `@font-face` rules in
|
|
123
|
-
your own stylesheet, which is inlined into the document.
|
|
124
|
-
- **`font-display: block`, or none at all.** Without a `font-display`, the
|
|
125
|
-
browser is free to hide text for up to three seconds while it waits — the
|
|
126
|
-
invisible-text flash. Every rule the setup writes says `swap` (paint the
|
|
127
|
-
fallback now, swap when the font lands) or `optional` (paint once, in
|
|
128
|
-
whichever is ready). Never `block`, and never leave it unset in a rule you
|
|
129
|
-
own.
|
|
130
|
-
- **Preloading everything.** A `<link rel="preload">` per file puts every
|
|
131
|
-
weight and subset ahead of the page in the network queue. Preload the one
|
|
132
|
-
file the first paint needs; the rest arrive with the stylesheet.
|
|
133
|
-
|
|
134
|
-
A font that is slow — a cold cache, a throttled connection — then costs a
|
|
135
|
-
swap or a fallback, never a blank page. The section below is how to make
|
|
136
|
-
that swap invisible.
|
|
155
|
+
The one thing `next/font` did that needs a line here is the preload. See
|
|
156
|
+
[Preloading the first font](#preloading-the-first-font).
|
|
137
157
|
|
|
138
158
|
## Getting to 100 on a phone
|
|
139
159
|
|
|
140
|
-
Fontsource's stylesheet
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
[rsc-kit.dev](https://rsc-kit.dev) from 99 to a steady 100, and they are
|
|
146
|
-
what any page can do once the design is settled:
|
|
147
|
-
|
|
148
|
-
**Own the `@font-face` rules, latin only.** Import the files you use with
|
|
149
|
-
`?url` and declare them yourself, with the latin `unicode-range`. The
|
|
150
|
-
declarations shrink from every script to the one you set, and the inlined
|
|
151
|
-
stylesheet with them.
|
|
152
|
-
|
|
153
|
-
**`font-display: optional` for the body faces.** The text then paints once —
|
|
154
|
-
in the fallback on a cold load over a slow network, in the web font when it
|
|
155
|
-
is cached or quick — and never re-lays out for a font. That is the honest
|
|
156
|
-
trade for a page whose first paint is the product: a first-time visitor on
|
|
157
|
-
slow 4G reads the fallback. The headline face stays `swap`, because it is
|
|
158
|
-
the brand and it is preloaded, so it arrives with the document.
|
|
159
|
-
|
|
160
|
-
**One weight per family, variable only where you use the axes.** A variable
|
|
161
|
-
font carries every weight; if the page uses one, a static file is a third
|
|
162
|
-
the size. Declare it to cover the range you ask for (`font-weight: 400 500`)
|
|
163
|
-
so nothing requests a file that is not there.
|
|
160
|
+
Fontsource's stylesheet declares every subset of every weight with
|
|
161
|
+
`font-display: swap`. On a throttled phone audit, the largest text paints in
|
|
162
|
+
the fallback and repaints when the font lands, and Lighthouse reads that as a
|
|
163
|
+
late LCP. Three changes took [rsc-kit.dev](https://rsc-kit.dev) from 99 to a
|
|
164
|
+
steady 100:
|
|
164
165
|
|
|
165
166
|
```tsx title="src/app/layout.tsx"
|
|
166
167
|
import fraunces from '@fontsource-variable/fraunces/files/fraunces-latin-wght-normal.woff2?url'
|
|
@@ -179,13 +180,41 @@ const FACES = `
|
|
|
179
180
|
<style>{FACES}</style>
|
|
180
181
|
```
|
|
181
182
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
183
|
+
- **Write the `@font-face` rules yourself, Latin only.** Import the files you
|
|
184
|
+
use with `?url` and give them the Latin `unicode-range`. The declarations
|
|
185
|
+
shrink from every script to one, and the inlined stylesheet with them.
|
|
186
|
+
- **Use `font-display: optional` for body text.** The text paints once: in the
|
|
187
|
+
fallback on a cold load over a slow network, in the web font when it's
|
|
188
|
+
cached or quick. It never re-lays out for a font, and a first-time visitor
|
|
189
|
+
on slow 4G reads the fallback. The headline face stays `swap`, because it is
|
|
190
|
+
the brand and it is preloaded, so it arrives with the document.
|
|
191
|
+
- **Use one weight per family, and a variable font only where you use its
|
|
192
|
+
axes.** A variable font carries every weight. If the page uses one, a static
|
|
193
|
+
file is a third of the size. Declare it to cover the range you ask for
|
|
194
|
+
(`font-weight: 400 500`), so nothing requests a file that isn't there.
|
|
195
|
+
|
|
196
|
+
On a throttled Moto G against the same page, Speed Index went from 1.7 s to
|
|
197
|
+
0.9 s, three runs scored 100 where there had been 99s, and fonts went from
|
|
198
|
+
114 kB to 74 kB. Do this last: it is tuning, and the design has to be settled
|
|
199
|
+
before the weights are.
|
|
200
|
+
|
|
201
|
+
## Why a font never blocks the page
|
|
202
|
+
|
|
203
|
+
Text paints before the web font arrives. A font can break that in three ways,
|
|
204
|
+
and the setup above avoids all three:
|
|
186
205
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
206
|
+
- **A stylesheet from another origin.** `<link href="https://fonts.googleapis.com/…">`
|
|
207
|
+
is render-blocking CSS from a host the browser hasn't connected to: a DNS
|
|
208
|
+
lookup, a TLS handshake and a round trip before the first paint, on every
|
|
209
|
+
cold load. Fontsource keeps the `@font-face` rules in your own stylesheet,
|
|
210
|
+
which is inlined into the document.
|
|
211
|
+
- **`font-display: block`, or none at all.** Without a `font-display`, the
|
|
212
|
+
browser may hide text for up to three seconds while it waits. Every rule
|
|
213
|
+
above says `swap` (paint the fallback now, swap when the font lands) or
|
|
214
|
+
`optional` (paint once, in whichever is ready). Never use `block`, and never
|
|
215
|
+
leave it unset in a rule you write.
|
|
216
|
+
- **Preloading everything.** A preload per file puts every weight and subset
|
|
217
|
+
ahead of the page in the network queue. Preload the one file the first
|
|
218
|
+
paint needs; the rest arrive with the stylesheet.
|
|
219
|
+
|
|
220
|
+
A slow font then costs a swap or a fallback, never a blank page.
|