remix 3.0.0-beta.0 → 3.0.0-beta.2
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/fetch-router.d.ts +7 -0
- package/dist/fetch-router.d.ts.map +1 -1
- package/dist/node-tsx/load-module.d.ts +2 -0
- package/dist/node-tsx/load-module.d.ts.map +1 -0
- package/dist/node-tsx/load-module.js +2 -0
- package/dist/node-tsx.d.ts +3 -0
- package/dist/node-tsx.d.ts.map +1 -0
- package/{src/node-serve.ts → dist/node-tsx.js} +2 -1
- package/dist/render-middleware.d.ts +2 -0
- package/dist/render-middleware.d.ts.map +1 -0
- package/dist/render-middleware.js +2 -0
- package/dist/route-pattern/href.d.ts +2 -0
- package/dist/route-pattern/href.d.ts.map +1 -0
- package/dist/route-pattern/href.js +2 -0
- package/dist/route-pattern/join.d.ts +2 -0
- package/dist/route-pattern/join.d.ts.map +1 -0
- package/dist/route-pattern/join.js +2 -0
- package/dist/route-pattern/match.d.ts +2 -0
- package/dist/route-pattern/match.d.ts.map +1 -0
- package/dist/route-pattern/match.js +2 -0
- package/package.json +158 -44
- package/src/assert/README.md +109 -0
- package/src/assets/README.md +539 -0
- package/src/async-context-middleware/README.md +100 -0
- package/src/auth/README.md +445 -0
- package/src/auth-middleware/README.md +246 -0
- package/src/cli/README.md +78 -0
- package/src/compression-middleware/README.md +176 -0
- package/src/cookie/README.md +106 -0
- package/src/cop-middleware/README.md +117 -0
- package/src/cors-middleware/README.md +174 -0
- package/src/csrf-middleware/README.md +99 -0
- package/src/data-schema/README.md +422 -0
- package/src/data-table/README.md +552 -0
- package/src/data-table-mysql/README.md +97 -0
- package/src/data-table-postgres/README.md +74 -0
- package/src/data-table-sqlite/README.md +84 -0
- package/src/fetch-proxy/README.md +46 -0
- package/src/fetch-router/README.md +902 -0
- package/src/fetch-router.ts +7 -0
- package/src/file-storage/README.md +57 -0
- package/src/file-storage-s3/README.md +47 -0
- package/src/form-data-middleware/README.md +109 -0
- package/src/form-data-parser/README.md +160 -0
- package/src/fs/README.md +60 -0
- package/src/headers/README.md +629 -0
- package/src/html-template/README.md +101 -0
- package/src/lazy-file/README.md +109 -0
- package/src/logger-middleware/README.md +132 -0
- package/src/method-override-middleware/README.md +71 -0
- package/src/mime/README.md +110 -0
- package/src/multipart-parser/README.md +241 -0
- package/src/node-fetch-server/README.md +352 -0
- package/src/node-tsx/README.md +79 -0
- package/src/node-tsx/load-module.ts +2 -0
- package/{dist/node-serve.js → src/node-tsx.ts} +2 -1
- package/src/render-middleware/README.md +99 -0
- package/src/render-middleware.ts +2 -0
- package/src/route-pattern/README.md +291 -0
- package/src/route-pattern/href.ts +2 -0
- package/src/route-pattern/join.ts +2 -0
- package/src/route-pattern/match.ts +2 -0
- package/src/session/README.md +171 -0
- package/src/session-middleware/README.md +109 -0
- package/src/session-storage-memcache/README.md +37 -0
- package/src/session-storage-redis/README.md +37 -0
- package/src/static-middleware/README.md +89 -0
- package/src/tar-parser/README.md +74 -0
- package/src/terminal/README.md +92 -0
- package/src/test/README.md +430 -0
- package/src/ui/README.md +219 -0
- package/src/ui/accordion/README.md +166 -0
- package/src/ui/anchor/README.md +153 -0
- package/src/ui/animation/README.md +316 -0
- package/src/ui/breadcrumbs/README.md +55 -0
- package/src/ui/button/README.md +44 -0
- package/src/ui/combobox/README.md +145 -0
- package/src/ui/glyph/README.md +72 -0
- package/src/ui/listbox/README.md +115 -0
- package/src/ui/menu/README.md +96 -0
- package/src/ui/popover/README.md +122 -0
- package/src/ui/scroll-lock/README.md +33 -0
- package/src/ui/select/README.md +107 -0
- package/src/ui/server/README.md +90 -0
- package/src/ui/test/README.md +107 -0
- package/src/ui/theme/README.md +103 -0
- package/dist/node-serve.d.ts +0 -2
- package/dist/node-serve.d.ts.map +0 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
# assets
|
|
2
|
+
|
|
3
|
+
Fetch-based server for compiling browser assets on demand.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **On-Demand Compilation** - Compile browser scripts and styles on demand
|
|
8
|
+
- **File Serving** - Serve configured file assets like images and fonts with optional transforms
|
|
9
|
+
- **Custom File Mapping** - Define patterns for mapping public URLs to file paths on disk
|
|
10
|
+
- **Access Control** - Control exactly which files can be served with allow and deny rules
|
|
11
|
+
- **Preloads** - Generate preload URLs for scripts and styles based on imports
|
|
12
|
+
- **Caching** - Conservative caching by default with stable URLs, ETags, and revalidation
|
|
13
|
+
- **Optional Fingerprinting** - Source-based fingerprinted URLs for long-lived browser caching
|
|
14
|
+
- **Source Maps** - Serve inline or external sourcemaps
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm i remix
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Use `createAssetServer` to serve browser assets from a URL namespace in your app.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { createRouter } from 'remix/router'
|
|
28
|
+
import { createAssetServer } from 'remix/assets'
|
|
29
|
+
|
|
30
|
+
let assetServer = createAssetServer({
|
|
31
|
+
basePath: '/assets',
|
|
32
|
+
fileMap: {
|
|
33
|
+
'/app/*path': 'app/*path',
|
|
34
|
+
'/npm/*path': 'node_modules/*path',
|
|
35
|
+
},
|
|
36
|
+
allow: ['app/assets/**', 'node_modules/**'],
|
|
37
|
+
files: {
|
|
38
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
let router = createRouter()
|
|
43
|
+
|
|
44
|
+
router.get('/assets/*', ({ request }) => {
|
|
45
|
+
return assetServer.fetch(request)
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This example gives you an `/assets/*` endpoint that serves compiled browser assets from `app/assets` and `node_modules`.
|
|
50
|
+
|
|
51
|
+
## Root Directory
|
|
52
|
+
|
|
53
|
+
Use `rootDir` to specify the root directory of the asset server, which is used to resolve relative file paths. Defaults to `process.cwd()`.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import * as path from 'node:path'
|
|
57
|
+
import { createAssetServer } from 'remix/assets'
|
|
58
|
+
|
|
59
|
+
let assetServer = createAssetServer({
|
|
60
|
+
rootDir: path.resolve(import.meta.dirname, '..'),
|
|
61
|
+
basePath: '/assets',
|
|
62
|
+
fileMap: {
|
|
63
|
+
'/app/*path': 'app/*path',
|
|
64
|
+
'/npm/*path': 'node_modules/*path',
|
|
65
|
+
},
|
|
66
|
+
allow: ['app/assets/**', 'node_modules/**'],
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Access Control
|
|
71
|
+
|
|
72
|
+
You must provide an `allow` list to specify which files are allowed to be served. `deny` is optional and takes precedence over `allow`.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { createAssetServer } from 'remix/assets'
|
|
76
|
+
|
|
77
|
+
let assetServer = createAssetServer({
|
|
78
|
+
basePath: '/assets',
|
|
79
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
80
|
+
allow: ['app/assets/**'],
|
|
81
|
+
deny: ['app/**/*.server.*'],
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Rules for `allow` and `deny` are file paths or globs. Relative values are resolved from `rootDir`. Absolute file paths match exactly, and absolute directory paths also match their descendants.
|
|
86
|
+
|
|
87
|
+
## File Map
|
|
88
|
+
|
|
89
|
+
Use `fileMap` to map public URLs to file paths on disk. `basePath` defines the shared public mount point, and the `fileMap` keys are URL patterns relative to that base path. The values are root-relative file path patterns.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { createAssetServer } from 'remix/assets'
|
|
93
|
+
|
|
94
|
+
let assetServer = createAssetServer({
|
|
95
|
+
basePath: '/assets',
|
|
96
|
+
fileMap: {
|
|
97
|
+
'/app/*path': 'app/*path',
|
|
98
|
+
'/packages/*path': '../packages/*path',
|
|
99
|
+
},
|
|
100
|
+
allow: ['app/assets/**', '../packages/**'],
|
|
101
|
+
})
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`fileMap` entries use [`route-pattern`](https://github.com/remix-run/remix/tree/main/packages/route-pattern) syntax for both URL and file patterns. Wildcards must be named, and the same params must appear in both patterns so imports can be rewritten back to public URLs. For example, with `basePath: '/assets'`, a `fileMap` key of `'/app/*path'` is served at `/assets/app/*path`.
|
|
105
|
+
|
|
106
|
+
### File watching
|
|
107
|
+
|
|
108
|
+
The file system is watched by default so source changes are picked up without requiring a server restart.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { createAssetServer } from 'remix/assets'
|
|
112
|
+
|
|
113
|
+
let assetServer = createAssetServer({
|
|
114
|
+
basePath: '/assets',
|
|
115
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
116
|
+
allow: ['app/assets/**', 'app/node_modules/**'],
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
When finished with the asset server, call `await assetServer.close()` to clean up the file watcher.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
await assetServer.close()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
You can disable file watching if the files on disk won't change, or if watching is managed at a higher level (e.g. Node's `--watch` flag).
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import { createAssetServer } from 'remix/assets'
|
|
130
|
+
|
|
131
|
+
let assetServer = createAssetServer({
|
|
132
|
+
basePath: '/assets',
|
|
133
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
134
|
+
allow: ['app/assets/**', 'app/node_modules/**'],
|
|
135
|
+
watch: false,
|
|
136
|
+
})
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
You can optionally provide an array of glob patterns to the `watch.ignore` option:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { createAssetServer } from 'remix/assets'
|
|
143
|
+
|
|
144
|
+
let assetServer = createAssetServer({
|
|
145
|
+
basePath: '/assets',
|
|
146
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
147
|
+
allow: ['app/assets/**', 'app/node_modules/**'],
|
|
148
|
+
watch: {
|
|
149
|
+
ignore: ['**/node_modules/**'],
|
|
150
|
+
},
|
|
151
|
+
})
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Hrefs
|
|
155
|
+
|
|
156
|
+
Use `assetServer.getHref()` when you need the public URL for a served asset. You can provide a root-relative or absolute file path, or a `file://` URL.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
let src = await assetServer.getHref('app/assets/entry.tsx')
|
|
160
|
+
// '/assets/app/assets/entry.tsx'
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
For configured `files` assets, you can also pass a `transform` pipeline to build a request URL with custom file transforms. Basic transforms are written as strings, while dynamic transforms use `[name, param]` tuples.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
let src = await assetServer.getHref('app/assets/image.png', {
|
|
167
|
+
transform: [['resize', '100x100'], 'webp'],
|
|
168
|
+
})
|
|
169
|
+
// '/assets/app/assets/image.png?transform=resize%3A100x100&transform=webp'
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Preloads
|
|
173
|
+
|
|
174
|
+
Use `assetServer.getPreloads()` when rendering HTML so you can turn the returned URLs into `<link rel="modulepreload">`, stylesheet preload tags, or `Link` headers for one or more assets and their dependencies. You can provide root-relative or absolute file paths, or `file://` URLs.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
let preloads = await assetServer.getPreloads(['app/assets/entry.tsx', 'app/assets/search.tsx'])
|
|
178
|
+
// [
|
|
179
|
+
// '/assets/app/assets/entry.tsx',
|
|
180
|
+
// '/assets/app/assets/search.tsx',
|
|
181
|
+
// '/assets/app/assets/utils.ts',
|
|
182
|
+
// '/assets/npm/remix/ui/index.js',
|
|
183
|
+
// ...etc
|
|
184
|
+
// ]
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Fingerprinting
|
|
188
|
+
|
|
189
|
+
By default, assets are served at stable URLs with ETags and `Cache-Control: no-cache`.
|
|
190
|
+
|
|
191
|
+
If you want clients to cache assets aggressively without revalidation, you can opt into source-based fingerprinting.
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
import { createAssetServer } from 'remix/assets'
|
|
195
|
+
|
|
196
|
+
let assetServer = createAssetServer({
|
|
197
|
+
basePath: '/assets',
|
|
198
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
199
|
+
allow: ['app/assets/**'],
|
|
200
|
+
watch: false,
|
|
201
|
+
fingerprint: {
|
|
202
|
+
buildId: process.env.GITHUB_SHA,
|
|
203
|
+
},
|
|
204
|
+
})
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
When fingerprinting is enabled, assets use a `.@<fingerprint>` segment before the file extension and are served with `Cache-Control: public, max-age=31536000, immutable`.
|
|
208
|
+
|
|
209
|
+
Source fingerprints are based on the original file contents and the build ID. The build ID must change for each deployment so that fingerprinted assets are invalidated together. This fingerprinting strategy assumes that files on disk won't change, so fingerprinting requires `watch: false`.
|
|
210
|
+
|
|
211
|
+
## Target
|
|
212
|
+
|
|
213
|
+
Use `target` to lower emitted syntax to a specific browser support policy and/or ECMAScript version.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { createAssetServer } from 'remix/assets'
|
|
217
|
+
|
|
218
|
+
let assetServer = createAssetServer({
|
|
219
|
+
basePath: '/assets',
|
|
220
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
221
|
+
allow: ['app/assets/**'],
|
|
222
|
+
target: {
|
|
223
|
+
chrome: '109',
|
|
224
|
+
ios: '15.6',
|
|
225
|
+
es: '2020',
|
|
226
|
+
},
|
|
227
|
+
})
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Supported target options are `chrome`, `firefox`, `safari`, `edge`, `opera`, `ios`, `samsung`, and `es` (ECMAScript version).
|
|
231
|
+
|
|
232
|
+
### Source Maps
|
|
233
|
+
|
|
234
|
+
Enable sourcemaps with either `'external'` or `'inline'` using `sourceMaps`:
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import { createAssetServer } from 'remix/assets'
|
|
238
|
+
|
|
239
|
+
let assetServer = createAssetServer({
|
|
240
|
+
basePath: '/assets',
|
|
241
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
242
|
+
allow: ['app/assets/**'],
|
|
243
|
+
sourceMaps: 'external',
|
|
244
|
+
})
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
By default, sourcemap `sources` use URLs so they're presented alongside the compiled output in your browser's developer tools. You can also use file system paths instead with `sourceMapSourcePaths`:
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
import { createAssetServer } from 'remix/assets'
|
|
251
|
+
|
|
252
|
+
let assetServer = createAssetServer({
|
|
253
|
+
basePath: '/assets',
|
|
254
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
255
|
+
allow: ['app/assets/**'],
|
|
256
|
+
sourceMaps: 'inline',
|
|
257
|
+
sourceMapSourcePaths: 'absolute',
|
|
258
|
+
})
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Minification
|
|
262
|
+
|
|
263
|
+
Enable minification with `minify`:
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { createAssetServer } from 'remix/assets'
|
|
267
|
+
|
|
268
|
+
let assetServer = createAssetServer({
|
|
269
|
+
basePath: '/assets',
|
|
270
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
271
|
+
allow: ['app/assets/**'],
|
|
272
|
+
minify: true,
|
|
273
|
+
})
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Script Options
|
|
277
|
+
|
|
278
|
+
### Define
|
|
279
|
+
|
|
280
|
+
Use `scripts.define` to replace global identifiers with constant expressions.
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
import { createAssetServer } from 'remix/assets'
|
|
284
|
+
|
|
285
|
+
let assetServer = createAssetServer({
|
|
286
|
+
basePath: '/assets',
|
|
287
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
288
|
+
allow: ['app/assets/**', 'app/node_modules/**'],
|
|
289
|
+
scripts: {
|
|
290
|
+
define: {
|
|
291
|
+
'process.env.NODE_ENV': '"production"',
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Values are injected exactly as defined, so string literals must include their own quotes, e.g. `process.env.NODE_ENV` must be `"production"` rather than `production`.
|
|
298
|
+
|
|
299
|
+
### External Imports
|
|
300
|
+
|
|
301
|
+
Use `scripts.external` to leave specific import specifiers unchanged by providing an array of specifiers.
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { createAssetServer } from 'remix/assets'
|
|
305
|
+
|
|
306
|
+
let assetServer = createAssetServer({
|
|
307
|
+
basePath: '/assets',
|
|
308
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
309
|
+
allow: ['app/assets/**'],
|
|
310
|
+
scripts: {
|
|
311
|
+
external: ['my-external-import'],
|
|
312
|
+
},
|
|
313
|
+
})
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## File Options
|
|
317
|
+
|
|
318
|
+
Use `files` to serve additional leaf assets like images and fonts. File extensions must include the leading dot and are only served when explicitly configured.
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
import { createAssetServer } from 'remix/assets'
|
|
322
|
+
|
|
323
|
+
let assetServer = createAssetServer({
|
|
324
|
+
basePath: '/assets',
|
|
325
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
326
|
+
allow: ['app/assets/**'],
|
|
327
|
+
files: {
|
|
328
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
329
|
+
},
|
|
330
|
+
})
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
JavaScript/TypeScript and CSS extensions not supported in `files.extensions` as they are not leaf assets and have their own module systems.
|
|
334
|
+
|
|
335
|
+
### File transforms
|
|
336
|
+
|
|
337
|
+
Files can optionally be transformed before serving.
|
|
338
|
+
|
|
339
|
+
Use `files.transforms` for named transforms that callers can opt into per request, provided via the `transform` option when calling `assetServer.getHref()`.
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
import { createAssetServer, defineFileTransform } from 'remix/assets'
|
|
343
|
+
import sharp from 'sharp'
|
|
344
|
+
|
|
345
|
+
let assetServer = createAssetServer({
|
|
346
|
+
basePath: '/assets',
|
|
347
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
348
|
+
allow: ['app/assets/**'],
|
|
349
|
+
files: {
|
|
350
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
351
|
+
transforms: {
|
|
352
|
+
webp: defineFileTransform({
|
|
353
|
+
extensions: ['.png', '.jpg', '.jpeg'],
|
|
354
|
+
async transform(bytes) {
|
|
355
|
+
return {
|
|
356
|
+
content: await sharp(bytes).webp({ quality: 80 }).toBuffer(),
|
|
357
|
+
extension: '.webp',
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
}),
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
let imageUrl = await assetServer.getHref('app/assets/photo.jpg', {
|
|
366
|
+
transform: ['webp'],
|
|
367
|
+
})
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Transforms can also accept a single string param value, provided as a `[name, param]` tuple in the `transform` array when calling `assetServer.getHref()`.
|
|
371
|
+
|
|
372
|
+
```ts
|
|
373
|
+
import { createAssetServer, defineFileTransform } from 'remix/assets'
|
|
374
|
+
|
|
375
|
+
let assetServer = createAssetServer({
|
|
376
|
+
basePath: '/assets',
|
|
377
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
378
|
+
allow: ['app/assets/**'],
|
|
379
|
+
files: {
|
|
380
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
381
|
+
transforms: {
|
|
382
|
+
recolor: defineFileTransform({
|
|
383
|
+
extensions: ['.svg'],
|
|
384
|
+
param: true,
|
|
385
|
+
async transform(bytes, { param }) {
|
|
386
|
+
if (!/^#?(?:[\da-f]{3,4}|[\da-f]{6}(?:[\da-f]{2})?)$/i.test(param)) {
|
|
387
|
+
throw new TypeError('Expected a hex color, with or without a leading #')
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
let svg = new TextDecoder().decode(bytes)
|
|
391
|
+
return svg.replaceAll('currentColor', `${!param.startsWith('#') ? '#' : ''}${param}`)
|
|
392
|
+
},
|
|
393
|
+
}),
|
|
394
|
+
},
|
|
395
|
+
},
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
let imageUrl = await assetServer.getHref('app/assets/logo.svg', {
|
|
399
|
+
transform: [['recolor', '0000ff']],
|
|
400
|
+
})
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
Hand-authored URLs use repeated `transform` search params with `name` or `name:param` values:
|
|
404
|
+
|
|
405
|
+
```css
|
|
406
|
+
.selector {
|
|
407
|
+
background-image: url('/assets/app/assets/image.png?transform=resize:100x100&transform=webp');
|
|
408
|
+
}
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
#### Global file transforms
|
|
412
|
+
|
|
413
|
+
Use `files.globalTransforms` to define transforms that should always happen before a file is served. These transforms are run after any request-level transforms for all configured file extensions, and can return `null` to skip themselves for a given input.
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
import { createAssetServer } from 'remix/assets'
|
|
417
|
+
import { optimize as optimizeSvg } from 'svgo'
|
|
418
|
+
|
|
419
|
+
let assetServer = createAssetServer({
|
|
420
|
+
basePath: '/assets',
|
|
421
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
422
|
+
allow: ['app/assets/**'],
|
|
423
|
+
files: {
|
|
424
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
425
|
+
globalTransforms: [
|
|
426
|
+
{
|
|
427
|
+
extensions: ['.svg'],
|
|
428
|
+
async transform(bytes) {
|
|
429
|
+
let svg = new TextDecoder().decode(bytes)
|
|
430
|
+
return optimizeSvg(svg, { multipass: true }).data
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
},
|
|
435
|
+
})
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
#### File transform caching
|
|
439
|
+
|
|
440
|
+
Use `files.cache` to store transformed file outputs via a [`file-storage`](https://github.com/remix-run/remix/tree/main/packages/file-storage) backend.
|
|
441
|
+
|
|
442
|
+
Without `files.cache`, transformed file outputs are recomputed per request.
|
|
443
|
+
|
|
444
|
+
If `fingerprint.buildId` is set, the file cache can be reused across server restarts for the same build.
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
import * as path from 'node:path'
|
|
448
|
+
import { createAssetServer } from 'remix/assets'
|
|
449
|
+
import { createFsFileStorage } from 'remix/file-storage/fs'
|
|
450
|
+
|
|
451
|
+
let assetServer = createAssetServer({
|
|
452
|
+
basePath: '/assets',
|
|
453
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
454
|
+
allow: ['app/assets/**'],
|
|
455
|
+
files: {
|
|
456
|
+
cache: createFsFileStorage(path.resolve('.tmp/assets-cache')),
|
|
457
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
458
|
+
transforms: {
|
|
459
|
+
/*...*/
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
})
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
#### Request transform limits
|
|
466
|
+
|
|
467
|
+
Use `files.maxRequestTransforms` to cap request transform pipelines. It defaults to `5`.
|
|
468
|
+
|
|
469
|
+
```ts
|
|
470
|
+
import { createAssetServer } from 'remix/assets'
|
|
471
|
+
|
|
472
|
+
let assetServer = createAssetServer({
|
|
473
|
+
basePath: '/assets',
|
|
474
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
475
|
+
allow: ['app/assets/**'],
|
|
476
|
+
files: {
|
|
477
|
+
maxRequestTransforms: 5,
|
|
478
|
+
extensions: ['.svg', '.png', '.jpg', '.jpeg', '.woff2'],
|
|
479
|
+
transforms: {
|
|
480
|
+
/*...*/
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
})
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
## CSS Imports
|
|
487
|
+
|
|
488
|
+
Relative CSS `@import` rules and `url()` references are rewritten to asset server URLs.
|
|
489
|
+
|
|
490
|
+
```css
|
|
491
|
+
/* Rewritten to asset server URLs: */
|
|
492
|
+
@import './reset.css';
|
|
493
|
+
.selector {
|
|
494
|
+
background-image: url('./image.png');
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/* External URLs: */
|
|
498
|
+
@import 'https://fonts.googleapis.com/css2?family=Inter';
|
|
499
|
+
.selector {
|
|
500
|
+
background-image: url('https://example.com/logo.svg');
|
|
501
|
+
}
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
File transforms can also be applied to relative CSS `url()` references:
|
|
505
|
+
|
|
506
|
+
```css
|
|
507
|
+
.selector {
|
|
508
|
+
background-image: url('./image.png?transform=resize:100x100&transform=webp');
|
|
509
|
+
}
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
## Error Handling
|
|
513
|
+
|
|
514
|
+
Use `onError` to report unexpected compilation failures and/or return a custom response.
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
import { createAssetServer } from 'remix/assets'
|
|
518
|
+
|
|
519
|
+
let assetServer = createAssetServer({
|
|
520
|
+
basePath: '/assets',
|
|
521
|
+
fileMap: { '/app/*path': 'app/*path' },
|
|
522
|
+
allow: ['app/assets/**'],
|
|
523
|
+
onError(error) {
|
|
524
|
+
console.error('Failed to build client assets', error)
|
|
525
|
+
return new Response('Client asset build failed', { status: 500 })
|
|
526
|
+
},
|
|
527
|
+
})
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
If `onError` returns nothing, the asset server responds with the default `500 Internal Server Error` response.
|
|
531
|
+
|
|
532
|
+
## Related Packages
|
|
533
|
+
|
|
534
|
+
- [`fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) - A Fetch-based router that pairs naturally with `assets`
|
|
535
|
+
- [`route-pattern`](https://github.com/remix-run/remix/tree/main/packages/route-pattern) - Route-pattern syntax for URL and route file matching
|
|
536
|
+
|
|
537
|
+
## License
|
|
538
|
+
|
|
539
|
+
See [LICENSE](https://github.com/remix-run/remix/blob/main/LICENSE)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# async-context-middleware
|
|
2
|
+
|
|
3
|
+
Request-scoped async context middleware for Remix. It stores each request context in [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage) so utilities can access it anywhere in the same async call stack.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Request context access** - Read the current `RequestContext` from anywhere in the same async execution flow
|
|
8
|
+
- **App-typed `getContext()`** - Reuses your fetch-router `RouterTypes.context` by default
|
|
9
|
+
- **Simple router integration** - Add a single middleware at the router level
|
|
10
|
+
- **Node async hooks** - Built on `node:async_hooks` `AsyncLocalStorage`
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm i remix
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Use `asyncContext()` at the router level to make the current request context available to helpers deeper in the same async call stack.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createRouter } from 'remix/router'
|
|
24
|
+
import { asyncContext, getContext } from 'remix/middleware/async-context'
|
|
25
|
+
|
|
26
|
+
let router = createRouter({
|
|
27
|
+
middleware: [asyncContext()],
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
async function loadCurrentUser() {
|
|
31
|
+
let context = getContext()
|
|
32
|
+
let userId = context.params.id
|
|
33
|
+
|
|
34
|
+
return users.getById(userId)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
router.get('/users/:id', async () => {
|
|
38
|
+
let user = await loadCurrentUser()
|
|
39
|
+
return Response.json(user)
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This middleware requires support for `node:async_hooks`, so it is intended for Node.js runtimes.
|
|
44
|
+
|
|
45
|
+
## Typed `getContext()`
|
|
46
|
+
|
|
47
|
+
`getContext()` is global and out-of-band, so it reuses your fetch-router `RouterTypes.context` by default.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { requireAuth } from 'remix/middleware/auth'
|
|
51
|
+
import type { AnyParams, ContextWithParams, MiddlewareContext } from 'remix/router'
|
|
52
|
+
import { loadAuth } from './middleware/auth.ts'
|
|
53
|
+
import { loadSession } from './middleware/session.ts'
|
|
54
|
+
|
|
55
|
+
export type RootMiddleware = [ReturnType<typeof loadSession>, ReturnType<typeof loadAuth>]
|
|
56
|
+
export const authenticatedMiddleware = [requireAuth<{ id: string }>()] as const
|
|
57
|
+
|
|
58
|
+
export type AppContext<params extends AnyParams = {}> = ContextWithParams<
|
|
59
|
+
MiddlewareContext<RootMiddleware>,
|
|
60
|
+
params
|
|
61
|
+
>
|
|
62
|
+
|
|
63
|
+
export type AuthenticatedAppContext<params extends AnyParams = {}> = ContextWithParams<
|
|
64
|
+
MiddlewareContext<typeof authenticatedMiddleware, AppContext>,
|
|
65
|
+
params
|
|
66
|
+
>
|
|
67
|
+
|
|
68
|
+
declare module 'remix/router' {
|
|
69
|
+
interface RouterTypes {
|
|
70
|
+
context: AppContext
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
After that augmentation, `getContext()` returns your app context values everywhere in the app, with route params typed broadly as `AnyParams`.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { Auth } from 'remix/middleware/auth'
|
|
79
|
+
import { getContext } from 'remix/middleware/async-context'
|
|
80
|
+
|
|
81
|
+
function getCurrentAuth() {
|
|
82
|
+
return getContext().get(Auth)
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Route handlers themselves can still use more precise route-specific params in their own `RequestContext` types.
|
|
87
|
+
|
|
88
|
+
## Related Packages
|
|
89
|
+
|
|
90
|
+
- [`fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) - Router and request context contracts for Remix
|
|
91
|
+
- [`auth-middleware`](https://github.com/remix-run/remix/tree/main/packages/auth-middleware) - Request-time auth state and protected route middleware
|
|
92
|
+
- [`session-middleware`](https://github.com/remix-run/remix/tree/main/packages/session-middleware) - Session loading middleware often paired with async request context
|
|
93
|
+
|
|
94
|
+
## Related Work
|
|
95
|
+
|
|
96
|
+
- [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage)
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
See [LICENSE](https://github.com/remix-run/remix/blob/main/LICENSE)
|