|
| 1 | +# Assets and Browser Modules |
| 2 | + |
| 3 | +## What This Covers |
| 4 | + |
| 5 | +How to serve browser scripts and styles from source. Read this when the task |
| 6 | +involves: |
| 7 | + |
| 8 | +- Configuring `createAssetServer` (`fileMap`, `allow`, `deny`, fingerprinting, |
| 9 | + compiler options) |
| 10 | +- Choosing between `staticFiles()` for already-built files and |
| 11 | + `createAssetServer()` for source assets that need import rewriting, preloads, |
| 12 | + or fingerprinted URLs |
| 13 | +- Generating script URLs or `<link rel="modulepreload">` tags for a client entry |
| 14 | +- Keeping server-only files out of the browser via `deny` rules |
| 15 | + |
| 16 | +For routing the URL namespace itself, see `routing-and-controllers.md`. For |
| 17 | +client entry hydration, see `hydration-frames-navigation.md`. |
| 18 | + |
| 19 | +## When To Reach For It |
| 20 | + |
| 21 | +Use `remix/assets` when the app serves browser JavaScript, TypeScript, or CSS |
| 22 | +from source files. This is the right tool for client entrypoints, browser-only |
| 23 | +helpers, styles under `app/assets/`, and monorepo code that should be compiled |
| 24 | +and served under a public URL namespace. |
| 25 | + |
| 26 | +Use `staticFiles()` for files that already exist on disk exactly as they should |
| 27 | +be served. Use `createAssetServer()` for source scripts or styles that need |
| 28 | +rewriting, dependency scanning, preloads, sourcemaps, or fingerprinted URLs. |
| 29 | + |
| 30 | +## Default Pattern |
| 31 | + |
| 32 | +```typescript |
| 33 | +import * as path from 'node:path' |
| 34 | + |
| 35 | +import { createAssetServer } from 'remix/assets' |
| 36 | +import { createRouter } from 'remix/fetch-router' |
| 37 | + |
| 38 | +let assetServer = createAssetServer({ |
| 39 | + rootDir: path.resolve(import.meta.dirname, '..'), |
| 40 | + fileMap: { |
| 41 | + '/assets/app/*path': 'app/*path', |
| 42 | + '/assets/packages/*path': '../packages/*path', |
| 43 | + }, |
| 44 | + allow: ['app/assets/**', '../packages/**'], |
| 45 | + deny: ['app/**/*.server.*'], |
| 46 | + target: { es: '2020', chrome: '109', safari: '16.4' }, |
| 47 | + sourceMaps: process.env.NODE_ENV === 'development' ? 'external' : undefined, |
| 48 | + minify: process.env.NODE_ENV === 'production', |
| 49 | + scripts: { |
| 50 | + define: { |
| 51 | + 'process.env.NODE_ENV': JSON.stringify( |
| 52 | + process.env.NODE_ENV ?? 'development', |
| 53 | + ), |
| 54 | + }, |
| 55 | + }, |
| 56 | +}) |
| 57 | + |
| 58 | +let router = createRouter() |
| 59 | + |
| 60 | +router.get('/assets/*path', ({ request }) => { |
| 61 | + return assetServer.fetch(request) |
| 62 | +}) |
| 63 | +``` |
| 64 | + |
| 65 | +## Rules |
| 66 | + |
| 67 | +- Treat `allow` and `deny` as the security boundary for browser-reachable source |
| 68 | + files. |
| 69 | +- Add a `deny` list for server-only modules such as `*.server.*`, private |
| 70 | + config, or other files that should never be exposed. |
| 71 | +- Set `rootDir` explicitly in monorepos so relative paths resolve from the |
| 72 | + intended project root. |
| 73 | +- `fileMap` keys are public URL patterns and values are root-relative file path |
| 74 | + patterns. They use `route-pattern` syntax on both sides. |
| 75 | +- Keep the same wildcard params on both sides of a `fileMap` entry so import |
| 76 | + rewriting can map source files back to public URLs. |
| 77 | +- CSS files are compiled and served alongside scripts. Local CSS `@import` rules |
| 78 | + are rewritten and fingerprinted with the same asset server routing rules. |
| 79 | + |
| 80 | +## Rendering HTML |
| 81 | + |
| 82 | +Use `getHref()` when you need the public URL for one module, and `getPreloads()` |
| 83 | +when you want `<link rel="modulepreload">` tags or `Link` headers for one or |
| 84 | +more entrypoints and their dependencies. |
| 85 | + |
| 86 | +```typescript |
| 87 | +let entryHref = await assetServer.getHref('app/assets/entry.ts') |
| 88 | +let preloads = await assetServer.getPreloads(['app/assets/entry.ts']) |
| 89 | +``` |
| 90 | + |
| 91 | +Use this when rendering documents or layouts that boot browser behavior with a |
| 92 | +known client entry. |
| 93 | + |
| 94 | +When resolving hydrated client entries during server rendering, pass the source |
| 95 | +entry ID from `clientEntry(import.meta.url, ...)` to `getHref()` inside |
| 96 | +`resolveClientEntry`. Keep export-name resolution in that render helper, and |
| 97 | +avoid hard-coding public asset URLs in source-owned component modules. |
| 98 | + |
| 99 | +## Development vs Deployment |
| 100 | + |
| 101 | +In development: |
| 102 | + |
| 103 | +- Keep `watch` enabled so source changes are picked up without restarting the |
| 104 | + server |
| 105 | +- Prefer stable URLs with normal revalidation |
| 106 | +- Enable source maps when debugging browser code |
| 107 | + |
| 108 | +In deployment: |
| 109 | + |
| 110 | +- Set `watch: false` |
| 111 | +- Use `fingerprint: { buildId }` for long-lived immutable caching |
| 112 | +- Make sure `buildId` changes for each deploy |
| 113 | + |
| 114 | +Fingerprinting assumes files on disk are stable and requires `watch: false`. |
| 115 | + |
| 116 | +## Useful Compiler Options |
| 117 | + |
| 118 | +- `minify` for production minification of scripts and styles |
| 119 | +- `sourceMaps` for `'external'` or `'inline'` source maps for scripts and styles |
| 120 | +- `sourceMapSourcePaths` for `'url'` or `'absolute'` source map paths |
| 121 | +- `target` as an object for shared browser targets and script-only ECMAScript |
| 122 | + output, such as `{ es: '2020', chrome: '109', safari: '16.4' }` |
| 123 | +- `scripts.define` to replace globals such as `process.env.NODE_ENV` |
| 124 | +- `scripts.external` to leave specific script imports untouched |
| 125 | + |
| 126 | +Do not nest shared compiler options under `scripts`. Use top-level `minify`, |
| 127 | +`sourceMaps`, `sourceMapSourcePaths`, and `target` so they apply to styles as |
| 128 | +well as scripts. |
| 129 | + |
| 130 | +## Lifecycle |
| 131 | + |
| 132 | +If the asset server is long-lived and watching the file system, call |
| 133 | +`await assetServer.close()` when shutting down dev servers or disposing tests. |
0 commit comments