brustjs 0.1.44-alpha → 0.1.46-alpha

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/README.md CHANGED
@@ -88,8 +88,9 @@ Click either one — both update. [See it live](https://brust.assetsart.com/) ·
88
88
 
89
89
  - **Native rendering** — pages compile ahead of time and are served as plain
90
90
  HTML: no hydration pass, no framework runtime in the response. One compiled
91
- page sustains **84,119 req/s** versus 28,938 for the same page through a
92
- JavaScript pipeline — about 2. ([bench/RESULTS.md](./bench/RESULTS.md)).
91
+ page sustains **106,374 req/s** versus 44,448 for the same page through a
92
+ JavaScript pipeline — about 2.4×, on an M1 Pro (10c)
93
+ ([bench/RESULTS.md](./bench/RESULTS.md)).
93
94
  → [Rendering](https://brust.assetsart.com/docs/rendering)
94
95
  - **React islands** — hydrate one component, not the page. React 19 streaming
95
96
  SSR with auto-Suspense; SSR islands can opt into **ISR caching**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brustjs",
3
- "version": "0.1.44-alpha",
3
+ "version": "0.1.46-alpha",
4
4
  "description": "Bun + Rust SSR framework — React on the server, Rust everywhere else (napi cdylib + per-worker SharedArrayBuffer).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,12 +41,12 @@
41
41
  "typescript": "^6.0.3"
42
42
  },
43
43
  "optionalDependencies": {
44
- "brustjs-darwin-x64": "0.1.44-alpha",
45
- "brustjs-darwin-arm64": "0.1.44-alpha",
46
- "brustjs-linux-x64-gnu": "0.1.44-alpha",
47
- "brustjs-linux-arm64-gnu": "0.1.44-alpha",
48
- "brustjs-linux-x64-musl": "0.1.44-alpha",
49
- "brustjs-linux-arm64-musl": "0.1.44-alpha"
44
+ "brustjs-darwin-x64": "0.1.46-alpha",
45
+ "brustjs-darwin-arm64": "0.1.46-alpha",
46
+ "brustjs-linux-x64-gnu": "0.1.46-alpha",
47
+ "brustjs-linux-arm64-gnu": "0.1.46-alpha",
48
+ "brustjs-linux-x64-musl": "0.1.46-alpha",
49
+ "brustjs-linux-arm64-musl": "0.1.46-alpha"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "react": "^19.2.6",
package/runtime/cache.ts CHANGED
@@ -5,14 +5,28 @@ import * as native from './index.js'
5
5
  export interface InvalidateArgs {
6
6
  key?: string
7
7
  tags?: string[]
8
+ /** L1 response-cache: evict all entries for this request path (any prefix /
9
+ * query variant). Independent of `tags`. */
10
+ path?: string
11
+ /** HTTP method for the `path` eviction (defaults to GET in Rust). */
12
+ method?: string
8
13
  }
9
14
 
10
15
  export const cache = {
11
- /** Evict by exact key and/or by tag group. Both optional; both may be given.
12
- * `invalidate({})` forwards `(undefined, undefined)` a deliberate no-op on
13
- * the Rust side (neither the key nor the tags branch fires). The `?.` guards
14
- * against a stale addon built before this export existed (degrades to no-op). */
16
+ /** Evict across all three caches: islands, L2 page cache, and L1 response
17
+ * cache. `key`/`tags` hit islands + L2; the L1 response cache is reached by
18
+ * `tags` (the ROUTE must declare static `cache.tags` for its L1 entries to
19
+ * carry them) and by `path` (evicts every L1 entry for that request path,
20
+ * any prefix/query variant). All fields optional; `invalidate({})` is a
21
+ * deliberate no-op. The `?.` guards against a stale addon built before a
22
+ * given binding existed (degrades to no-op). */
15
23
  invalidate(args: InvalidateArgs): void {
16
24
  ;(native as any).islandCacheInvalidate?.(args.key, args.tags)
25
+ // Fan out to the L2 page cache (same key/tag semantics). `?.` keeps a
26
+ // stale addon (built before page-cache bindings existed) a no-op.
27
+ ;(native as any).pageCacheInvalidate?.(args.key, args.tags)
28
+ // Fan out to the L1 response cache by tag (route must declare cache.tags)
29
+ // and/or by path. `?.` keeps a stale addon a no-op.
30
+ ;(native as any).responseCacheInvalidate?.(args.tags, args.path, args.method)
17
31
  },
18
32
  }
package/runtime/config.ts CHANGED
@@ -10,8 +10,10 @@ export interface BrustConfig {
10
10
  port: number
11
11
  /** Bun Worker count for render dispatch. Default `availableParallelism()`. */
12
12
  workers: number
13
- /** Cache capacity (entries). Undefined → Rust default of 1000. */
13
+ /** L1 response-cache capacity (entries). Undefined → Rust default of 1000. */
14
14
  cacheMaxEntries?: number
15
+ /** L2 page-cache capacity (entries). Undefined → Rust default of 1000. */
16
+ cachePageMaxEntries?: number
15
17
  }
16
18
 
17
19
  /** Caller-supplied fallbacks (e.g. `brust.run({ address, port })`) applied
@@ -77,7 +79,13 @@ export async function loadConfig(
77
79
  const port = fromEnv.port ?? fromToml.port ?? defaults.port ?? DEFAULT_PORT
78
80
  const workers = fromEnv.workers ?? fromToml.workers ?? defaultWorkers()
79
81
 
80
- return { host, port, workers, cacheMaxEntries: fromToml.cacheMaxEntries }
82
+ return {
83
+ host,
84
+ port,
85
+ workers,
86
+ cacheMaxEntries: fromToml.cacheMaxEntries,
87
+ cachePageMaxEntries: fromToml.cachePageMaxEntries,
88
+ }
81
89
  }
82
90
 
83
91
  function extractFromToml(parsed: unknown, file: string): Partial<BrustConfig> {
@@ -146,6 +154,20 @@ function extractFromToml(parsed: unknown, file: string): Partial<BrustConfig> {
146
154
  }
147
155
  out.cacheMaxEntries = maxEntries
148
156
  }
157
+ const pageMaxEntries = (cache as Record<string, unknown>).page_max_entries
158
+ if (pageMaxEntries !== undefined) {
159
+ if (
160
+ typeof pageMaxEntries !== 'number' ||
161
+ !Number.isInteger(pageMaxEntries) ||
162
+ pageMaxEntries < 1
163
+ ) {
164
+ throw new BrustConfigError(
165
+ `${file}: cache.page_max_entries must be a positive integer (got ${JSON.stringify(pageMaxEntries)})`,
166
+ file,
167
+ )
168
+ }
169
+ out.cachePageMaxEntries = pageMaxEntries
170
+ }
149
171
  }
150
172
 
151
173
  return out
@@ -19,7 +19,7 @@ export interface CachedIslandJs {
19
19
 
20
20
  export declare function compileJsx(source: string, path: string, componentSources?: Record<string, string> | undefined | null, lucideIcons?: Record<string, string> | undefined | null, directiveNames?: Record<string, string> | undefined | null): NapiCompiledJsx
21
21
 
22
- export declare function configureCache(maxEntries: number): NapiResult<undefined>
22
+ export declare function configureCache(maxEntries: number, pageMaxEntries: number): NapiResult<undefined>
23
23
 
24
24
  export declare function configureCssDir(path: string): NapiResult<undefined>
25
25
 
@@ -235,6 +235,14 @@ export declare function napiWsSend(connId: bigint, data: Buffer, isBinary: boole
235
235
  */
236
236
  export declare function napiWsSignalOpen(connId: bigint, status: number, body: Buffer, contentType: string, subprotocol: string): NapiResult<undefined>
237
237
 
238
+ export declare function pageCacheClear(): void
239
+
240
+ export declare function pageCacheGet(key: string): Buffer | null
241
+
242
+ export declare function pageCacheInvalidate(key?: string | undefined | null, tags?: Array<string> | undefined | null): void
243
+
244
+ export declare function pageCacheSet(key: string, tags: Array<string>, ttlMs: number | undefined | null, payload: Buffer): void
245
+
238
246
  /**
239
247
  * Register action endpoints. Replaces any previous router state.
240
248
  * Returns the number of endpoints registered.
@@ -256,6 +264,8 @@ export declare function registerRoutes(configs: Array<string>): NapiResult<numbe
256
264
  */
257
265
  export declare function resetWorkerPool(): number
258
266
 
267
+ export declare function responseCacheInvalidate(tags?: Array<string> | undefined | null, path?: string | undefined | null, method?: string | undefined | null): void
268
+
259
269
  export interface ServeOptions {
260
270
  /**
261
271
  * Host to bind on — a hostname (e.g. `localhost`, resolved here) or a
@@ -320,8 +330,9 @@ export interface ServeTuning {
320
330
  workerThreads?: number
321
331
  /**
322
332
  * Number of render slots per Bun worker (concurrent in-flight renders per
323
- * isolate). Default 1 (single in-flight render per worker byte-identical
324
- * to the pre-multi-slot behaviour). The COUNT reaches `register_renderer`
333
+ * isolate). Default `min(cores, 16)` (set `BRUST_RENDER_SLOTS` to exceed 16
334
+ * cores); slots > 1 only speed renders that await during render and are
335
+ * byte-identical to slots = 1. The COUNT reaches `register_renderer`
325
336
  * via the `BRUST_RENDER_SLOTS` worker env var set in `runtime/index.ts`; it
326
337
  * is NOT consumed by the Rust `Tuning` struct here (the pool learns it from
327
338
  * `dispatch.slot_count()`). Carried on `ServeTuning` only so the JS layer
package/runtime/index.js CHANGED
@@ -77,8 +77,8 @@ function requireNative() {
77
77
  try {
78
78
  const binding = require('brustjs-android-arm64')
79
79
  const bindingPackageVersion = require('brustjs-android-arm64/package.json').version
80
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
80
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
82
  }
83
83
  return binding
84
84
  } catch (e) {
@@ -93,8 +93,8 @@ function requireNative() {
93
93
  try {
94
94
  const binding = require('brustjs-android-arm-eabi')
95
95
  const bindingPackageVersion = require('brustjs-android-arm-eabi/package.json').version
96
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
96
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
98
  }
99
99
  return binding
100
100
  } catch (e) {
@@ -114,8 +114,8 @@ function requireNative() {
114
114
  try {
115
115
  const binding = require('brustjs-win32-x64-gnu')
116
116
  const bindingPackageVersion = require('brustjs-win32-x64-gnu/package.json').version
117
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
117
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
119
  }
120
120
  return binding
121
121
  } catch (e) {
@@ -130,8 +130,8 @@ function requireNative() {
130
130
  try {
131
131
  const binding = require('brustjs-win32-x64-msvc')
132
132
  const bindingPackageVersion = require('brustjs-win32-x64-msvc/package.json').version
133
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
133
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
135
  }
136
136
  return binding
137
137
  } catch (e) {
@@ -147,8 +147,8 @@ function requireNative() {
147
147
  try {
148
148
  const binding = require('brustjs-win32-ia32-msvc')
149
149
  const bindingPackageVersion = require('brustjs-win32-ia32-msvc/package.json').version
150
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
150
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
152
  }
153
153
  return binding
154
154
  } catch (e) {
@@ -163,8 +163,8 @@ function requireNative() {
163
163
  try {
164
164
  const binding = require('brustjs-win32-arm64-msvc')
165
165
  const bindingPackageVersion = require('brustjs-win32-arm64-msvc/package.json').version
166
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
166
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
168
  }
169
169
  return binding
170
170
  } catch (e) {
@@ -182,8 +182,8 @@ function requireNative() {
182
182
  try {
183
183
  const binding = require('brustjs-darwin-universal')
184
184
  const bindingPackageVersion = require('brustjs-darwin-universal/package.json').version
185
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
185
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
187
  }
188
188
  return binding
189
189
  } catch (e) {
@@ -198,8 +198,8 @@ function requireNative() {
198
198
  try {
199
199
  const binding = require('brustjs-darwin-x64')
200
200
  const bindingPackageVersion = require('brustjs-darwin-x64/package.json').version
201
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
201
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
203
  }
204
204
  return binding
205
205
  } catch (e) {
@@ -214,8 +214,8 @@ function requireNative() {
214
214
  try {
215
215
  const binding = require('brustjs-darwin-arm64')
216
216
  const bindingPackageVersion = require('brustjs-darwin-arm64/package.json').version
217
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
217
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
219
  }
220
220
  return binding
221
221
  } catch (e) {
@@ -234,8 +234,8 @@ function requireNative() {
234
234
  try {
235
235
  const binding = require('brustjs-freebsd-x64')
236
236
  const bindingPackageVersion = require('brustjs-freebsd-x64/package.json').version
237
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
237
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
239
  }
240
240
  return binding
241
241
  } catch (e) {
@@ -250,8 +250,8 @@ function requireNative() {
250
250
  try {
251
251
  const binding = require('brustjs-freebsd-arm64')
252
252
  const bindingPackageVersion = require('brustjs-freebsd-arm64/package.json').version
253
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
253
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
255
  }
256
256
  return binding
257
257
  } catch (e) {
@@ -271,8 +271,8 @@ function requireNative() {
271
271
  try {
272
272
  const binding = require('brustjs-linux-x64-musl')
273
273
  const bindingPackageVersion = require('brustjs-linux-x64-musl/package.json').version
274
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
274
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
276
  }
277
277
  return binding
278
278
  } catch (e) {
@@ -287,8 +287,8 @@ function requireNative() {
287
287
  try {
288
288
  const binding = require('brustjs-linux-x64-gnu')
289
289
  const bindingPackageVersion = require('brustjs-linux-x64-gnu/package.json').version
290
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
290
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
292
  }
293
293
  return binding
294
294
  } catch (e) {
@@ -305,8 +305,8 @@ function requireNative() {
305
305
  try {
306
306
  const binding = require('brustjs-linux-arm64-musl')
307
307
  const bindingPackageVersion = require('brustjs-linux-arm64-musl/package.json').version
308
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
308
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
310
  }
311
311
  return binding
312
312
  } catch (e) {
@@ -321,8 +321,8 @@ function requireNative() {
321
321
  try {
322
322
  const binding = require('brustjs-linux-arm64-gnu')
323
323
  const bindingPackageVersion = require('brustjs-linux-arm64-gnu/package.json').version
324
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
324
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
326
  }
327
327
  return binding
328
328
  } catch (e) {
@@ -339,8 +339,8 @@ function requireNative() {
339
339
  try {
340
340
  const binding = require('brustjs-linux-arm-musleabihf')
341
341
  const bindingPackageVersion = require('brustjs-linux-arm-musleabihf/package.json').version
342
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
342
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
344
  }
345
345
  return binding
346
346
  } catch (e) {
@@ -355,8 +355,8 @@ function requireNative() {
355
355
  try {
356
356
  const binding = require('brustjs-linux-arm-gnueabihf')
357
357
  const bindingPackageVersion = require('brustjs-linux-arm-gnueabihf/package.json').version
358
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
358
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
360
  }
361
361
  return binding
362
362
  } catch (e) {
@@ -373,8 +373,8 @@ function requireNative() {
373
373
  try {
374
374
  const binding = require('brustjs-linux-loong64-musl')
375
375
  const bindingPackageVersion = require('brustjs-linux-loong64-musl/package.json').version
376
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
376
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
378
  }
379
379
  return binding
380
380
  } catch (e) {
@@ -389,8 +389,8 @@ function requireNative() {
389
389
  try {
390
390
  const binding = require('brustjs-linux-loong64-gnu')
391
391
  const bindingPackageVersion = require('brustjs-linux-loong64-gnu/package.json').version
392
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
392
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
394
  }
395
395
  return binding
396
396
  } catch (e) {
@@ -407,8 +407,8 @@ function requireNative() {
407
407
  try {
408
408
  const binding = require('brustjs-linux-riscv64-musl')
409
409
  const bindingPackageVersion = require('brustjs-linux-riscv64-musl/package.json').version
410
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
410
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
412
  }
413
413
  return binding
414
414
  } catch (e) {
@@ -423,8 +423,8 @@ function requireNative() {
423
423
  try {
424
424
  const binding = require('brustjs-linux-riscv64-gnu')
425
425
  const bindingPackageVersion = require('brustjs-linux-riscv64-gnu/package.json').version
426
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
426
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
428
  }
429
429
  return binding
430
430
  } catch (e) {
@@ -440,8 +440,8 @@ function requireNative() {
440
440
  try {
441
441
  const binding = require('brustjs-linux-ppc64-gnu')
442
442
  const bindingPackageVersion = require('brustjs-linux-ppc64-gnu/package.json').version
443
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
443
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
445
  }
446
446
  return binding
447
447
  } catch (e) {
@@ -456,8 +456,8 @@ function requireNative() {
456
456
  try {
457
457
  const binding = require('brustjs-linux-s390x-gnu')
458
458
  const bindingPackageVersion = require('brustjs-linux-s390x-gnu/package.json').version
459
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
459
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
461
  }
462
462
  return binding
463
463
  } catch (e) {
@@ -476,8 +476,8 @@ function requireNative() {
476
476
  try {
477
477
  const binding = require('brustjs-openharmony-arm64')
478
478
  const bindingPackageVersion = require('brustjs-openharmony-arm64/package.json').version
479
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
479
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
481
  }
482
482
  return binding
483
483
  } catch (e) {
@@ -492,8 +492,8 @@ function requireNative() {
492
492
  try {
493
493
  const binding = require('brustjs-openharmony-x64')
494
494
  const bindingPackageVersion = require('brustjs-openharmony-x64/package.json').version
495
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
495
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
497
  }
498
498
  return binding
499
499
  } catch (e) {
@@ -508,8 +508,8 @@ function requireNative() {
508
508
  try {
509
509
  const binding = require('brustjs-openharmony-arm')
510
510
  const bindingPackageVersion = require('brustjs-openharmony-arm/package.json').version
511
- if (bindingPackageVersion !== '0.1.44-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
- throw new Error(`Native binding package version mismatch, expected 0.1.44-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
511
+ if (bindingPackageVersion !== '0.1.46-alpha' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.1.46-alpha but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
513
  }
514
514
  return binding
515
515
  } catch (e) {
@@ -605,10 +605,15 @@ module.exports.napiWsClose = nativeBinding.napiWsClose
605
605
  module.exports.napiWsRegisterHandlers = nativeBinding.napiWsRegisterHandlers
606
606
  module.exports.napiWsSend = nativeBinding.napiWsSend
607
607
  module.exports.napiWsSignalOpen = nativeBinding.napiWsSignalOpen
608
+ module.exports.pageCacheClear = nativeBinding.pageCacheClear
609
+ module.exports.pageCacheGet = nativeBinding.pageCacheGet
610
+ module.exports.pageCacheInvalidate = nativeBinding.pageCacheInvalidate
611
+ module.exports.pageCacheSet = nativeBinding.pageCacheSet
608
612
  module.exports.registerActions = nativeBinding.registerActions
609
613
  module.exports.registerRenderer = nativeBinding.registerRenderer
610
614
  module.exports.registerRoutes = nativeBinding.registerRoutes
611
615
  module.exports.resetWorkerPool = nativeBinding.resetWorkerPool
616
+ module.exports.responseCacheInvalidate = nativeBinding.responseCacheInvalidate
612
617
  module.exports.untilReady = nativeBinding.untilReady
613
618
  module.exports.untilShutdown = nativeBinding.untilShutdown
614
619
  module.exports.workerId = nativeBinding.workerId
package/runtime/index.ts CHANGED
@@ -1,9 +1,26 @@
1
+ import os from 'node:os'
1
2
  import * as native from './index.js'
2
3
  import type { EndpointDef } from './define-actions.ts'
3
4
  import { loadConfig } from './config.ts'
4
5
  import { configureCssEnabled, configureCssHrefsForRoute } from './css.ts'
5
6
  import { configureJinjaDir } from './islands/native-render.ts'
6
7
 
8
+ /** Default render slots per Bun worker: one per CPU, capped at 16. Above 16
9
+ * cores, set `BRUST_RENDER_SLOTS` explicitly to go higher. Slots > 1 only speed
10
+ * up renders that `await` DURING render (e.g. Suspense fetching async data);
11
+ * CPU-bound / native-jinja / cache-hit routes serialize on the worker's single
12
+ * JS isolate and are unaffected. Each slot costs one SAB region (`sabBytes`), so
13
+ * the cap bounds memory. slots > 1 is byte-identical to slots = 1. */
14
+ function defaultRenderSlots(): number {
15
+ let cores: number
16
+ try {
17
+ cores = os.availableParallelism()
18
+ } catch {
19
+ cores = os.cpus().length
20
+ }
21
+ return Math.min(Math.max(cores, 1), 16)
22
+ }
23
+
7
24
  export interface ServeOptions {
8
25
  /** Host/address to bind on. A hostname (e.g. `localhost`, resolved Rust-side)
9
26
  * or a literal IP such as `0.0.0.0` / `127.0.0.1`. */
@@ -54,9 +71,12 @@ export interface ServeOptions {
54
71
  * one-per-core. Default `min(availableParallelism, 4)` (fallback 2). */
55
72
  workerThreads?: number
56
73
  /** Render slots per Bun worker — concurrent in-flight renders per isolate.
57
- * Default 1 (byte-identical to single in-flight render per worker). The
58
- * count is propagated to each worker via the `BRUST_RENDER_SLOTS` env var
59
- * and scales the per-worker SAB so each slot keeps the single-slot capacity. */
74
+ * Default `min(cores, 16)` (set `BRUST_RENDER_SLOTS` to go higher on >16-core
75
+ * hosts). Only speeds renders that `await` during render (e.g. Suspense with
76
+ * async data); CPU-bound/native/cache routes serialize on the one isolate and
77
+ * are unaffected. The count is propagated to each worker via the
78
+ * `BRUST_RENDER_SLOTS` env var and scales the per-worker SAB (one region per
79
+ * slot, so the cap bounds memory). slots > 1 is byte-identical to slots = 1. */
60
80
  renderSlots?: number
61
81
  }
62
82
  }
@@ -144,7 +164,7 @@ export const brust = {
144
164
  // per-app wiring), else 1 — byte-identical single in-flight render.
145
165
  const renderSlots = Math.max(
146
166
  1,
147
- opts.tuning?.renderSlots ?? (Number(process.env.BRUST_RENDER_SLOTS) || 1),
167
+ opts.tuning?.renderSlots ?? (Number(process.env.BRUST_RENDER_SLOTS) || defaultRenderSlots()),
148
168
  )
149
169
  const baseEnv = { ...process.env, BRUST_RENDER_SLOTS: String(renderSlots) }
150
170
  const workersArr: Worker[] = []
@@ -190,7 +210,25 @@ export const brust = {
190
210
  const configs = routes.map((r) =>
191
211
  JSON.stringify({
192
212
  path: r.fullPath,
193
- cache: r.cache ?? null,
213
+ // Rust-safe projection: the L2 `key` FUNCTION and `key_ttl_seconds`
214
+ // stay TS-side (the worker reads them off the FlatRoute). Only the
215
+ // L1 directives cross napi. `bypass` passes through as `true` or the
216
+ // string expr — serde's untagged BypassSpec handles both. `false` and
217
+ // absent both normalize to null → None (never bypass), so an explicitly
218
+ // disabled bypass doesn't ride a cross-language `false` contract. An
219
+ // EMPTY-STRING expr deliberately passes through: Expr::parse("") fails
220
+ // route install, surfacing the misconfiguration loudly at boot instead
221
+ // of silently never bypassing.
222
+ cache: r.cache
223
+ ? {
224
+ ttl_seconds: r.cache.ttl_seconds,
225
+ prefix: r.cache.prefix ?? null,
226
+ bypass: r.cache.bypass === false ? null : (r.cache.bypass ?? null),
227
+ // Static L1 invalidation tags carried into each L1 entry so
228
+ // `cache.invalidate({ tags })` can reach the response cache.
229
+ tags: r.cache.tags ?? null,
230
+ }
231
+ : null,
194
232
  nativeTemplate: r.nativeTemplate ?? null,
195
233
  }),
196
234
  )
@@ -229,11 +267,12 @@ export const brust = {
229
267
  registerWsPaths(paths: string[]): void {
230
268
  ;(native as any).napiRegisterWsPaths(paths)
231
269
  },
232
- /** Set the response cache capacity (entries). Default is 1000.
233
- * Safe to call at any time; if shrinking below current size, excess
234
- * LRU entries are evicted. */
235
- configureCache(opts: { maxEntries: number }): void {
236
- ;(native as any).configureCache(opts.maxEntries)
270
+ /** Set the L1 response-cache and L2 page-cache capacities (entries).
271
+ * Default is 1000 each. Rust reconstructs both caches at the given
272
+ * capacities (moka fixes capacity at construction), so call this once at
273
+ * boot before serving begins. */
274
+ configureCache(opts: { maxEntries: number; pageMaxEntries: number }): void {
275
+ ;(native as any).configureCache(opts.maxEntries, opts.pageMaxEntries)
237
276
  },
238
277
  /** Tell Rust where to read `/_brust/islands/<file>` from. Called once at
239
278
  * boot after buildIslands() emits chunks. Path must be absolute. */
@@ -365,12 +404,19 @@ export const brust = {
365
404
  const endpoints: EndpointDef[] = opts.actions?.endpoints ?? []
366
405
 
367
406
  if (!isWorker) {
368
- const { host, port, workers, cacheMaxEntries } = await loadConfig(process.cwd(), {
369
- host: opts.address,
370
- port: opts.port,
371
- })
407
+ const { host, port, workers, cacheMaxEntries, cachePageMaxEntries } = await loadConfig(
408
+ process.cwd(),
409
+ {
410
+ host: opts.address,
411
+ port: opts.port,
412
+ },
413
+ )
372
414
  console.log(`[brust] main: spawning ${workers} worker threads`)
373
- if (cacheMaxEntries !== undefined) this.configureCache({ maxEntries: cacheMaxEntries })
415
+ if (cacheMaxEntries !== undefined || cachePageMaxEntries !== undefined)
416
+ this.configureCache({
417
+ maxEntries: cacheMaxEntries ?? 1000,
418
+ pageMaxEntries: cachePageMaxEntries ?? 1000,
419
+ })
374
420
 
375
421
  // md routes present? (leaf carries `__mdSource`, attached by mdRoutes()).
376
422
  // Checked inline — no md-module import — so md-free apps never load the
@@ -883,7 +929,13 @@ export const brust = {
883
929
  // Render slots for this worker (set in serve() via the worker env). The
884
930
  // SAB scales with the slot count so each slot's disjoint sub-region keeps
885
931
  // the single-slot capacity; at K=1 this is byte-identical to before.
886
- const renderSlots = Math.max(1, Number(process.env.BRUST_RENDER_SLOTS ?? 1) || 1)
932
+ // Normally main propagates the computed count via the worker env; the
933
+ // defaultRenderSlots() fallback keeps an in-process boot (env unset) in
934
+ // sync with the main-branch default above.
935
+ const renderSlots = Math.max(
936
+ 1,
937
+ Number(process.env.BRUST_RENDER_SLOTS) || defaultRenderSlots(),
938
+ )
887
939
  const sab = new SharedArrayBuffer((opts.sabBytes ?? 256 * 1024) * renderSlots)
888
940
  const view = new Uint8Array(sab)
889
941
 
@@ -24,6 +24,14 @@ import {
24
24
  registerNavigator,
25
25
  } from '../navigation/store.ts'
26
26
  import { installActiveNav } from '../navigation/active-nav.ts'
27
+ import {
28
+ pageCacheKey,
29
+ getCachedPage,
30
+ fetchPagePayload,
31
+ inflightPage,
32
+ prefetchPage,
33
+ type PagePayload,
34
+ } from './page-cache.ts'
27
35
 
28
36
  // Track React roots created by hydrateOne so we can unmount them before
29
37
  // removing their DOM in swapMainContent. Without this, removing the DOM
@@ -242,22 +250,47 @@ export function isFullDocumentPayload(html: string): boolean {
242
250
 
243
251
  let inFlight: AbortController | null = null
244
252
 
253
+ // scrollY of each page we navigated away from (keyed pathname+search), so
254
+ // back/forward restores the reading position instead of jumping to top.
255
+ const scrollPositions = new Map<string, number>()
256
+ // The page currently in the DOM. Tracked here (set at boot + on every commit)
257
+ // instead of read from `location` at save time, because popstate updates
258
+ // `location` to the DESTINATION before the event fires — reading it there
259
+ // would record the leaving page's scroll under the wrong key.
260
+ let currentPageKey = ''
261
+
245
262
  export async function navigate(url: URL, mode: 'push' | 'replace' | 'none'): Promise<void> {
246
263
  inFlight?.abort()
247
264
  const ac = new AbortController()
248
265
  inFlight = ac
249
266
  try {
250
267
  __navStart(url.pathname, url.search)
251
- const resp = await fetch(`/_brust/page${url.pathname}${url.search}`, {
252
- signal: ac.signal,
253
- headers: { Accept: 'application/json' },
254
- })
255
- if (!resp.ok) throw new Error(`navigation: status ${resp.status}`)
256
- const { html, title, store } = (await resp.json()) as {
257
- html: string
258
- title: string
259
- store?: Record<string, Record<string, unknown>>
268
+ const key = pageCacheKey(url)
269
+ // Visited pages are served from the in-memory cache — no refetch. Forward
270
+ // navigations respect PAGE_STALE_MS (default max age); back/forward reuses
271
+ // any entry regardless of age, like the browser's bfcache.
272
+ const cached = getCachedPage(key, mode === 'none' ? Number.POSITIVE_INFINITY : undefined)
273
+ let payload: PagePayload
274
+ if (cached) {
275
+ payload = cached
276
+ } else {
277
+ // A hover prefetch may already have this page in flight — await it
278
+ // instead of double-fetching; if it failed, fall back to a direct fetch.
279
+ const pre = inflightPage(key)
280
+ if (pre) {
281
+ try {
282
+ payload = await pre
283
+ } catch {
284
+ payload = await fetchPagePayload(url, ac.signal)
285
+ }
286
+ } else {
287
+ payload = await fetchPagePayload(url, ac.signal)
288
+ }
289
+ // The prefetch promise isn't tied to our AbortController, so an abort
290
+ // during that await doesn't throw — check for supersession explicitly.
291
+ if (ac.signal.aborted) return
260
292
  }
293
+ const { html, title, store } = payload
261
294
  // A standalone (no-<main>) route ships its FULL document here. We can't swap
262
295
  // that into the current shell's <main> without nesting a second document
263
296
  // (duplicate chrome — the classic two-topbars artifact), and the current
@@ -269,14 +302,20 @@ export async function navigate(url: URL, mode: 'push' | 'replace' | 'none'): Pro
269
302
  }
270
303
  const main = document.querySelector('main')
271
304
  if (!main) throw new Error('navigation: no <main> element')
305
+ scrollPositions.set(currentPageKey, window.scrollY)
272
306
  unmountIslandsIn(main as HTMLElement)
273
307
  swapMainContent(main as HTMLElement, html)
274
- if (store) applyStoreSnapshot(store)
308
+ // Only a FRESH payload re-applies the server store snapshot: replaying a
309
+ // cached (stale) snapshot would roll back live client store state the user
310
+ // changed since the page was first fetched.
311
+ if (!cached && store) applyStoreSnapshot(store)
275
312
  if (title) document.title = title
276
313
  if (mode === 'push') history.pushState({}, '', url.href)
277
314
  else if (mode === 'replace') history.replaceState({}, '', url.href)
278
- window.scrollTo(0, 0)
315
+ if (mode === 'none') window.scrollTo(0, scrollPositions.get(key) ?? 0)
316
+ else window.scrollTo(0, 0)
279
317
  hydrateMarkersIn(main as HTMLElement)
318
+ currentPageKey = key
280
319
  __navCommit(url.pathname, url.search)
281
320
  } catch (err) {
282
321
  if ((err as Error).name === 'AbortError') return
@@ -288,7 +327,76 @@ export async function navigate(url: URL, mode: 'push' | 'replace' | 'none'): Pro
288
327
  }
289
328
  }
290
329
 
330
+ /** Resolve an <a> to a prefetchable in-app URL, or null when prefetch must not
331
+ * run: not ours (cross-origin, target, download, /_brust/), explicitly opted
332
+ * out (data-brust-no-intercept disables SPA handling entirely;
333
+ * data-brust-no-prefetch keeps the SPA click but skips the speculative fetch),
334
+ * or the page we are already on. Exported for unit testing. */
335
+ export function prefetchUrlFor(a: HTMLAnchorElement): URL | null {
336
+ if (a.target && a.target !== '_self') return null
337
+ if (a.hasAttribute('download')) return null
338
+ if (a.dataset.brustNoIntercept !== undefined) return null
339
+ if (a.dataset.brustNoPrefetch !== undefined) return null
340
+ if (!a.getAttribute('href')) return null
341
+ const url = new URL(a.href, location.href)
342
+ if (url.origin !== location.origin) return null
343
+ if (url.pathname.startsWith('/_brust/')) return null
344
+ if (url.pathname === location.pathname && url.search === location.search) return null
345
+ return url
346
+ }
347
+
348
+ // Hover-intent delay: long enough that a cursor sweeping across a nav list
349
+ // doesn't fire a fetch per link, short enough to beat the click (~200-300ms
350
+ // between hover and press for a deliberate click).
351
+ const HOVER_DELAY_MS = 80
352
+
353
+ /** Hover/touch prefetch: pointer rests on an internal link for HOVER_DELAY_MS
354
+ * (or a touch starts — the tap commits within ~100ms anyway) → fetch its nav
355
+ * payload into the page cache so the eventual click swaps instantly.
356
+ * prefetchPage dedupes in-flight fetches and respects Save-Data/2g. */
357
+ function installPrefetch(): void {
358
+ const timers = new WeakMap<HTMLAnchorElement, ReturnType<typeof setTimeout>>()
359
+ document.addEventListener('pointerover', (e) => {
360
+ const a = (e.target as HTMLElement | null)?.closest('a') as HTMLAnchorElement | null
361
+ if (!a || timers.has(a)) return
362
+ const url = prefetchUrlFor(a)
363
+ if (!url) return
364
+ timers.set(
365
+ a,
366
+ setTimeout(() => {
367
+ timers.delete(a)
368
+ void prefetchPage(url)
369
+ }, HOVER_DELAY_MS),
370
+ )
371
+ })
372
+ document.addEventListener('pointerout', (e) => {
373
+ const a = (e.target as HTMLElement | null)?.closest('a') as HTMLAnchorElement | null
374
+ if (!a) return
375
+ // Moving between children of the same link is not a leave.
376
+ if (e.relatedTarget instanceof Node && a.contains(e.relatedTarget)) return
377
+ const t = timers.get(a)
378
+ if (t !== undefined) {
379
+ clearTimeout(t)
380
+ timers.delete(a)
381
+ }
382
+ })
383
+ document.addEventListener(
384
+ 'touchstart',
385
+ (e) => {
386
+ const a = (e.target as HTMLElement | null)?.closest('a') as HTMLAnchorElement | null
387
+ if (!a) return
388
+ const url = prefetchUrlFor(a)
389
+ if (url) void prefetchPage(url)
390
+ },
391
+ { passive: true },
392
+ )
393
+ }
394
+
291
395
  function installInterceptor(): void {
396
+ // We restore scroll ourselves on popstate (the page cache makes back/forward
397
+ // an instant in-place swap); stop the browser's automatic restoration from
398
+ // fighting the swap.
399
+ if ('scrollRestoration' in history) history.scrollRestoration = 'manual'
292
400
  document.addEventListener('click', (e) => {
293
401
  const target = e.target as HTMLElement | null
294
402
  const a = target?.closest('a') as HTMLAnchorElement | null
@@ -309,17 +417,20 @@ function installInterceptor(): void {
309
417
 
310
418
  if (typeof document !== 'undefined') {
311
419
  registerNavigator((url, replace) => navigate(url, replace ? 'replace' : 'push'))
420
+ currentPageKey = location.pathname + location.search
312
421
  if (document.readyState === 'loading') {
313
422
  document.addEventListener('DOMContentLoaded', () => {
314
423
  __navInit(location.pathname, location.search)
315
424
  installActiveNav()
316
425
  hydrateMarkersIn(document.body)
317
426
  installInterceptor()
427
+ installPrefetch()
318
428
  })
319
429
  } else {
320
430
  __navInit(location.pathname, location.search)
321
431
  installActiveNav()
322
432
  hydrateMarkersIn(document.body)
323
433
  installInterceptor()
434
+ installPrefetch()
324
435
  }
325
436
  }
@@ -0,0 +1,108 @@
1
+ // runtime/islands/page-cache.ts — in-memory cache for SPA navigation payloads
2
+ // (/_brust/page/*) plus the fetch/dedupe machinery behind hover prefetch.
3
+ // Browser-only at call time (fetch/navigator), but import-safe anywhere.
4
+ //
5
+ // Lifetime: one document load. A full reload (dev rebuild WS, full-document
6
+ // fallback, hard refresh) drops the whole cache with the JS heap, so dev never
7
+ // serves stale pages across rebuilds.
8
+
9
+ export interface PagePayload {
10
+ html: string
11
+ title: string
12
+ store?: Record<string, Record<string, unknown>>
13
+ }
14
+
15
+ /** Bound the cache so a long session can't grow memory unbounded (LRU evict). */
16
+ export const PAGE_CACHE_MAX = 64
17
+ /** Forward navigations reuse an entry for this long; back/forward (popstate)
18
+ * passes Infinity and reuses any entry regardless of age (bfcache-like). */
19
+ export const PAGE_STALE_MS = 5 * 60_000
20
+
21
+ interface Entry {
22
+ payload: PagePayload
23
+ at: number
24
+ }
25
+
26
+ const cache = new Map<string, Entry>()
27
+ const inflight = new Map<string, Promise<PagePayload>>()
28
+
29
+ export function pageCacheKey(url: URL): string {
30
+ return url.pathname + url.search
31
+ }
32
+
33
+ export function getCachedPage(key: string, maxAgeMs: number = PAGE_STALE_MS): PagePayload | null {
34
+ const e = cache.get(key)
35
+ if (!e) return null
36
+ if (Date.now() - e.at > maxAgeMs) {
37
+ cache.delete(key)
38
+ return null
39
+ }
40
+ // LRU bump: Map iteration order is insertion order, so re-inserting moves
41
+ // this key to the "most recently used" end.
42
+ cache.delete(key)
43
+ cache.set(key, e)
44
+ return e.payload
45
+ }
46
+
47
+ export function setCachedPage(key: string, payload: PagePayload): void {
48
+ cache.delete(key)
49
+ cache.set(key, { payload, at: Date.now() })
50
+ if (cache.size > PAGE_CACHE_MAX) {
51
+ const oldest = cache.keys().next().value
52
+ if (oldest !== undefined) cache.delete(oldest)
53
+ }
54
+ }
55
+
56
+ /** Fetch a nav payload and cache it on success. Shared by navigate (direct,
57
+ * abortable) and prefetch (deduped, signal-less). */
58
+ export async function fetchPagePayload(url: URL, signal?: AbortSignal): Promise<PagePayload> {
59
+ const resp = await fetch(`/_brust/page${url.pathname}${url.search}`, {
60
+ signal,
61
+ headers: { Accept: 'application/json' },
62
+ })
63
+ if (!resp.ok) throw new Error(`navigation: status ${resp.status}`)
64
+ const payload = (await resp.json()) as PagePayload
65
+ setCachedPage(pageCacheKey(url), payload)
66
+ return payload
67
+ }
68
+
69
+ /** @internal — the in-flight prefetch for `key`, if any. navigate() awaits it
70
+ * instead of double-fetching when a click lands mid-prefetch. */
71
+ export function inflightPage(key: string): Promise<PagePayload> | undefined {
72
+ return inflight.get(key)
73
+ }
74
+
75
+ function prefetchAllowed(): boolean {
76
+ if (typeof navigator === 'undefined') return false
77
+ const conn = (navigator as { connection?: { saveData?: boolean; effectiveType?: string } })
78
+ .connection
79
+ if (conn?.saveData) return false
80
+ if (typeof conn?.effectiveType === 'string' && conn.effectiveType.includes('2g')) return false
81
+ return true
82
+ }
83
+
84
+ /** Speculatively load a page into the cache (hover/touch intent). No-op when
85
+ * already cached fresh or when the connection asks not to (Save-Data, 2g).
86
+ * Failures are silent — the eventual click just fetches normally. */
87
+ export function prefetchPage(url: URL): Promise<void> {
88
+ if (!prefetchAllowed()) return Promise.resolve()
89
+ const key = pageCacheKey(url)
90
+ if (getCachedPage(key) !== null) return Promise.resolve()
91
+ let p = inflight.get(key)
92
+ if (!p) {
93
+ p = fetchPagePayload(url).finally(() => {
94
+ inflight.delete(key)
95
+ })
96
+ inflight.set(key, p)
97
+ }
98
+ return p.then(
99
+ () => undefined,
100
+ () => undefined,
101
+ )
102
+ }
103
+
104
+ // Test-only: drop all entries and in-flight markers.
105
+ export function __resetPageCacheForTest(): void {
106
+ cache.clear()
107
+ inflight.clear()
108
+ }
package/runtime/routes.ts CHANGED
@@ -156,11 +156,37 @@ export interface ErrorBoundaryProps {
156
156
  error: Error
157
157
  }
158
158
 
159
- export interface RouteCacheConfig {
160
- /** Time-to-live in seconds. */
159
+ /** L2 programmatic cache-key result. The `key` function builds the COMPLETE
160
+ * cache key (you concat url + query + DB-derived data yourself). */
161
+ export interface CacheKeyResult {
162
+ /** The COMPLETE L2 cache key. */
163
+ key: string
164
+ /** Tag groups for `cache.invalidate({ tags })`. */
165
+ tags?: string[]
166
+ /** Seconds; overrides `key_ttl_seconds` / `ttl_seconds`. */
167
+ ttl?: number
168
+ }
169
+
170
+ export interface RouteCacheConfig<Params = Record<string, string>> {
171
+ /** Base TTL in seconds (L1; L2 fallback). */
161
172
  ttl_seconds: number
162
- /** Request headers that affect content. Each becomes part of the cache key. */
163
- vary?: string[]
173
+ /** L1 declarative key prefix expression (evaluated in Rust, zero-Bun on hit). */
174
+ prefix?: string
175
+ /** Route to L2 when truthy: a key-expression (conditional) or `true` (always).
176
+ * Absent / `false` ⇒ L1 only. */
177
+ bypass?: string | boolean
178
+ /** L2 programmatic key (runs in the worker). Returns the COMPLETE key. */
179
+ key?: (ctx: {
180
+ req: BrustRequest
181
+ url: URL
182
+ params: Params
183
+ }) => CacheKeyResult | Promise<CacheKeyResult>
184
+ /** Static L2 TTL (seconds); `CacheKeyResult.ttl` overrides per-entry. */
185
+ key_ttl_seconds?: number
186
+ /** Static L1 invalidation tags. L1 entries cached for this route carry these
187
+ * tags so `cache.invalidate({ tags })` evicts them (L1 is no longer
188
+ * TTL-only). Route-level + static — not per-request. */
189
+ tags?: string[]
164
190
  }
165
191
 
166
192
  /** Shape returned by a middleware or by the terminal `next()` (loader + render).
@@ -421,10 +447,8 @@ function validateRoute(r: Route, basePath: string): void {
421
447
  if (r.children !== undefined) {
422
448
  assertNativeSubtree(r.children, where)
423
449
  }
424
- if (r.cache !== undefined) {
425
- throw new Error(`Route ${where}: 'native: true' cannot coexist with 'cache' (deferred)`)
426
- }
427
- // loader + middleware are EXPLICITLY allowed.
450
+ // `cache` is now allowed on native routes — they are the primary L1
451
+ // (zero-Bun) target. loader + middleware are EXPLICITLY allowed.
428
452
  }
429
453
  if (r.sse) {
430
454
  const where = r.path ?? '(no path)'
@@ -453,6 +477,14 @@ function validateRoute(r: Route, basePath: string): void {
453
477
  throw new Error(`Route ${where}: 'websocket' cannot have nested children`)
454
478
  }
455
479
  }
480
+ // L2 (cache.key) is only reachable via a truthy `bypass`. A key with no
481
+ // bypass is dead config — warn so it isn't silently ignored.
482
+ if (r.cache?.key && r.cache.bypass === undefined) {
483
+ const where = r.path ?? '(no path)'
484
+ console.warn(
485
+ `[brust] route ${where}: cache.key has no cache.bypass — L2 is unreachable (bypass never routes to it)`,
486
+ )
487
+ }
456
488
  }
457
489
 
458
490
  /** T2 — recursively assert every node in a native subtree is also `native: true`.
@@ -572,6 +604,9 @@ export type RouteCall =
572
604
  path: string
573
605
  params: Record<string, string>
574
606
  req: BrustRequest
607
+ /** Set by Rust when the route's `cache.bypass` matched — routes this
608
+ * request to the L2 programmatic cache (worker `cache.key`). */
609
+ bypassed?: boolean
575
610
  }
576
611
  | {
577
612
  kind: 'navigation'
@@ -639,6 +674,51 @@ export interface MakeRendererOptions {
639
674
  slots?: number
640
675
  }
641
676
 
677
+ /** May a framed single-chunk payload (`[meta_len: u16 BE][meta JSON][body]`)
678
+ * enter the L2 page cache? Only a plain 200 without Set-Cookie. The status
679
+ * gate is load-bearing: a jinja render failure comes back as a framed 500
680
+ * through the SAME napiRenderJinja success return as a real render, so without
681
+ * it one flaky render would poison the L2 key for its full TTL. A Set-Cookie
682
+ * response is per-client (a cached one would leak a session). Short/truncated/
683
+ * malformed payloads → NOT cacheable (fail closed). */
684
+ function payloadCacheable(payload: Uint8Array): boolean {
685
+ if (payload.length < 2) return false
686
+ const metaLen = (payload[0] << 8) | payload[1]
687
+ if (payload.length < 2 + metaLen) return false
688
+ try {
689
+ const meta = JSON.parse(new TextDecoder().decode(payload.subarray(2, 2 + metaLen)))
690
+ if (meta?.status !== 200) return false
691
+ const headers = (meta?.headers ?? {}) as Record<string, unknown>
692
+ return !Object.keys(headers).some((h) => h.toLowerCase() === 'set-cookie')
693
+ } catch {
694
+ return false
695
+ }
696
+ }
697
+
698
+ // Return a copy of a framed `[meta_len:u16 BE][meta JSON][body]` payload with
699
+ // `X-Brust-Cache: HIT` added to the meta headers — stamped on an L2 replay so a
700
+ // cache hit is observable on the wire (mirrors the L1 hit header). On any parse
701
+ // failure the original payload is returned unchanged (never corrupt a response).
702
+ function withCacheHitHeader(payload: Uint8Array): Uint8Array {
703
+ if (payload.length < 2) return payload
704
+ const metaLen = (payload[0] << 8) | payload[1]
705
+ if (payload.length < 2 + metaLen) return payload
706
+ try {
707
+ const meta = JSON.parse(new TextDecoder().decode(payload.subarray(2, 2 + metaLen)))
708
+ meta.headers = { ...(meta.headers ?? {}), 'X-Brust-Cache': 'HIT' }
709
+ const metaBytes = new TextEncoder().encode(JSON.stringify(meta))
710
+ const body = payload.subarray(2 + metaLen)
711
+ const out = new Uint8Array(2 + metaBytes.length + body.length)
712
+ out[0] = (metaBytes.length >> 8) & 0xff
713
+ out[1] = metaBytes.length & 0xff
714
+ out.set(metaBytes, 2)
715
+ out.set(body, 2 + metaBytes.length)
716
+ return out
717
+ } catch {
718
+ return payload
719
+ }
720
+ }
721
+
642
722
  export function makeRenderer(
643
723
  routes: FlatRoute[],
644
724
  view: Uint8Array,
@@ -757,6 +837,78 @@ export function makeRenderer(
757
837
  })
758
838
  }
759
839
 
840
+ // L2 programmatic page cache (Task 10). Only on a bypassed request whose
841
+ // route declares a `cache.key` function. Runs AFTER middleware (which may
842
+ // set cookies / auth context the key depends on) and BEFORE render.
843
+ // HIT → write the cached framed payload into the SAB slot, return its
844
+ // length — the exact fast-lane shape Rust reads from a native
845
+ // render, so the loader+render are skipped entirely.
846
+ // MISS → render normally, then capture slotView[0..len] and pageCacheSet.
847
+ // Streaming/Suspense responses never reach the `len > 0` fast-lane return
848
+ // below (renderBranchStreaming returns 0), so they are never L2-cached.
849
+ // L2 capture/replay rides the NATIVE SAB fast-lane (a framed payload in
850
+ // slotView + a length return). React routes emit via the chunk channel and
851
+ // return 0, so engaging L2 there would return a non-zero length into the
852
+ // streaming path = protocol corruption. Gate L2 to native routes; a React
853
+ // route with `bypass` still skips L1 and renders fresh (no L2).
854
+ const cc = flat.cache
855
+ const wantL2 =
856
+ call.bypassed === true && typeof cc?.key === 'function' && flat.nativeTemplate !== undefined
857
+ let l2Key: CacheKeyResult | undefined
858
+ if (wantL2 && cc) {
859
+ const url = new URL(call.req.url, 'http://internal') // req.url is a path+query
860
+ try {
861
+ l2Key = await cc.key!({ req: call.req, url, params: call.params ?? {} })
862
+ } catch (err) {
863
+ // A throwing/rejecting key() must not crash the worker — fall back to
864
+ // a fresh render. l2Key stays undefined → replay skipped, store no-op.
865
+ console.error('[brust] cache.key threw; bypassing L2 (rendering fresh):', err)
866
+ }
867
+ if (l2Key) {
868
+ const cached = (native as any).pageCacheGet?.(l2Key.key) as Buffer | null | undefined
869
+ if (cached && cached.length > 0) {
870
+ // REPLAY — stamp the framed meta with X-Brust-Cache: HIT, then write
871
+ // it into the slot and return its length (fast lane). Prefer the
872
+ // stamped payload; if the extra header tips it past the slot, fall
873
+ // back to the verbatim bytes; only a too-large verbatim payload
874
+ // falls through to a fresh render (never serve truncated bytes).
875
+ const stamped = withCacheHitHeader(cached)
876
+ const out =
877
+ stamped.length <= slotView.length
878
+ ? stamped
879
+ : cached.length <= slotView.length
880
+ ? cached
881
+ : null
882
+ if (out) {
883
+ slotView.set(out, 0)
884
+ return out.length
885
+ }
886
+ }
887
+ }
888
+ }
889
+ // Capture-and-store on an L2 miss. Wraps the native success returns; a
890
+ // no-op when not bypassed or no key. Only single-chunk (len > 0) bytes
891
+ // that fit the slot and carry no Set-Cookie are stored.
892
+ const maybeStoreL2 = (len: number): number => {
893
+ if (wantL2 && cc && l2Key && len > 0 && len <= slotView.length) {
894
+ const payload = slotView.subarray(0, len).slice() // copy out of the SAB
895
+ if (payloadCacheable(payload)) {
896
+ const ttlSec = l2Key.ttl ?? cc.key_ttl_seconds ?? cc.ttl_seconds
897
+ try {
898
+ ;(native as any).pageCacheSet?.(
899
+ l2Key.key,
900
+ l2Key.tags ?? [],
901
+ Math.round(ttlSec * 1000),
902
+ payload,
903
+ )
904
+ } catch (err) {
905
+ console.error('[brust] pageCacheSet failed (response served, not cached):', err)
906
+ }
907
+ }
908
+ }
909
+ return len
910
+ }
911
+
760
912
  // Sub-project J — native: true branch. Runs the leaf's loader (if any),
761
913
  // JSON-encodes the result into the SAB, then invokes napiRenderJinja
762
914
  // which performs the minijinja render Rust-side and emits a 200 chunk
@@ -896,12 +1048,14 @@ export function makeRenderer(
896
1048
  }
897
1049
  slotView.set(finalBytes, 0)
898
1050
  try {
899
- return (native as any).napiRenderJinja(
900
- Number(workerId),
901
- slot,
902
- finalBytes.length,
903
- flat.nativeTemplate,
904
- renderStatus,
1051
+ return maybeStoreL2(
1052
+ (native as any).napiRenderJinja(
1053
+ Number(workerId),
1054
+ slot,
1055
+ finalBytes.length,
1056
+ flat.nativeTemplate,
1057
+ renderStatus,
1058
+ ),
905
1059
  )
906
1060
  } catch (err) {
907
1061
  console.error(`[brust] napiRenderJinja failed for "${flat.nativeTemplate}":`, err)
@@ -926,12 +1080,14 @@ export function makeRenderer(
926
1080
  // writes the framed response into the SAB, and returns its length
927
1081
  // directly (no Promise round-trip). Return it up to the tsfn; Rust's
928
1082
  // fast-lane arm reads the SAB directly (no chunk channel).
929
- return (native as any).napiRenderJinja(
930
- Number(workerId),
931
- slot,
932
- dataBytes.length,
933
- flat.nativeTemplate,
934
- renderStatus,
1083
+ return maybeStoreL2(
1084
+ (native as any).napiRenderJinja(
1085
+ Number(workerId),
1086
+ slot,
1087
+ dataBytes.length,
1088
+ flat.nativeTemplate,
1089
+ renderStatus,
1090
+ ),
935
1091
  )
936
1092
  } catch (err) {
937
1093
  console.error(`[brust] napiRenderJinja failed for "${flat.nativeTemplate}":`, err)