@timber-js/app 0.2.0-alpha.34 → 0.2.0-alpha.35

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.
Files changed (230) hide show
  1. package/dist/_chunks/{als-registry-B7DbZ2hS.js → als-registry-Ba7URUIn.js} +1 -1
  2. package/dist/_chunks/als-registry-Ba7URUIn.js.map +1 -0
  3. package/dist/_chunks/chunk-DYhsFzuS.js +33 -0
  4. package/dist/_chunks/{debug-B3Gypr3D.js → debug-ECi_61pb.js} +1 -1
  5. package/dist/_chunks/{debug-B3Gypr3D.js.map → debug-ECi_61pb.js.map} +1 -1
  6. package/dist/_chunks/define-cookie-w5GWm_bL.js +93 -0
  7. package/dist/_chunks/define-cookie-w5GWm_bL.js.map +1 -0
  8. package/dist/_chunks/error-boundary-TYEQJZ1-.js +211 -0
  9. package/dist/_chunks/error-boundary-TYEQJZ1-.js.map +1 -0
  10. package/dist/_chunks/{format-RyoGQL74.js → format-cX7wzEp2.js} +2 -2
  11. package/dist/_chunks/{format-RyoGQL74.js.map → format-cX7wzEp2.js.map} +1 -1
  12. package/dist/_chunks/{interception-BOoWmLUA.js → interception-D2djYaIm.js} +112 -77
  13. package/dist/_chunks/interception-D2djYaIm.js.map +1 -0
  14. package/dist/_chunks/{metadata-routes-Cjmvi3rQ.js → metadata-routes-BU684ls2.js} +1 -1
  15. package/dist/_chunks/{metadata-routes-Cjmvi3rQ.js.map → metadata-routes-BU684ls2.js.map} +1 -1
  16. package/dist/_chunks/{request-context-BQUC8PHn.js → request-context-CZz_T0Bc.js} +40 -71
  17. package/dist/_chunks/request-context-CZz_T0Bc.js.map +1 -0
  18. package/dist/_chunks/segment-context-Dpq2XOKg.js +34 -0
  19. package/dist/_chunks/segment-context-Dpq2XOKg.js.map +1 -0
  20. package/dist/_chunks/stale-reload-C0ValzG7.js +47 -0
  21. package/dist/_chunks/stale-reload-C0ValzG7.js.map +1 -0
  22. package/dist/_chunks/{tracing-CemImE6h.js → tracing-BPyIzIdu.js} +2 -2
  23. package/dist/_chunks/{tracing-CemImE6h.js.map → tracing-BPyIzIdu.js.map} +1 -1
  24. package/dist/_chunks/{use-query-states-D5KaffOK.js → use-query-states-BvW0TKDn.js} +1 -1
  25. package/dist/_chunks/{use-query-states-D5KaffOK.js.map → use-query-states-BvW0TKDn.js.map} +1 -1
  26. package/dist/_chunks/wrappers-C1SN725w.js +331 -0
  27. package/dist/_chunks/wrappers-C1SN725w.js.map +1 -0
  28. package/dist/cache/index.js +1 -1
  29. package/dist/client/error-boundary.d.ts +10 -1
  30. package/dist/client/error-boundary.d.ts.map +1 -1
  31. package/dist/client/error-boundary.js +1 -125
  32. package/dist/client/index.d.ts +2 -2
  33. package/dist/client/index.d.ts.map +1 -1
  34. package/dist/client/index.js +193 -90
  35. package/dist/client/index.js.map +1 -1
  36. package/dist/client/link.d.ts +8 -8
  37. package/dist/client/link.d.ts.map +1 -1
  38. package/dist/client/navigation-context.d.ts +2 -2
  39. package/dist/client/router.d.ts +25 -3
  40. package/dist/client/router.d.ts.map +1 -1
  41. package/dist/client/rsc-fetch.d.ts +23 -2
  42. package/dist/client/rsc-fetch.d.ts.map +1 -1
  43. package/dist/client/segment-cache.d.ts +1 -1
  44. package/dist/client/segment-cache.d.ts.map +1 -1
  45. package/dist/client/stale-reload.d.ts +15 -0
  46. package/dist/client/stale-reload.d.ts.map +1 -1
  47. package/dist/client/top-loader.d.ts +1 -1
  48. package/dist/client/top-loader.d.ts.map +1 -1
  49. package/dist/client/use-params.d.ts +2 -2
  50. package/dist/client/use-params.d.ts.map +1 -1
  51. package/dist/client/use-query-states.d.ts +1 -1
  52. package/dist/codec.d.ts +21 -0
  53. package/dist/codec.d.ts.map +1 -0
  54. package/dist/cookies/define-cookie.d.ts +33 -12
  55. package/dist/cookies/define-cookie.d.ts.map +1 -1
  56. package/dist/cookies/index.js +1 -81
  57. package/dist/index.d.ts +87 -12
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +346 -210
  60. package/dist/index.js.map +1 -1
  61. package/dist/params/define.d.ts +76 -0
  62. package/dist/params/define.d.ts.map +1 -0
  63. package/dist/params/index.d.ts +8 -0
  64. package/dist/params/index.d.ts.map +1 -0
  65. package/dist/params/index.js +104 -0
  66. package/dist/params/index.js.map +1 -0
  67. package/dist/plugins/adapter-build.d.ts.map +1 -1
  68. package/dist/plugins/build-manifest.d.ts.map +1 -1
  69. package/dist/plugins/client-chunks.d.ts +32 -0
  70. package/dist/plugins/client-chunks.d.ts.map +1 -0
  71. package/dist/plugins/entries.d.ts.map +1 -1
  72. package/dist/plugins/routing.d.ts.map +1 -1
  73. package/dist/plugins/server-bundle.d.ts.map +1 -1
  74. package/dist/plugins/static-build.d.ts.map +1 -1
  75. package/dist/routing/codegen.d.ts +2 -2
  76. package/dist/routing/codegen.d.ts.map +1 -1
  77. package/dist/routing/index.js +1 -1
  78. package/dist/routing/scanner.d.ts.map +1 -1
  79. package/dist/routing/status-file-lint.d.ts +2 -1
  80. package/dist/routing/status-file-lint.d.ts.map +1 -1
  81. package/dist/routing/types.d.ts +6 -4
  82. package/dist/routing/types.d.ts.map +1 -1
  83. package/dist/rsc-runtime/rsc.d.ts +1 -1
  84. package/dist/rsc-runtime/rsc.d.ts.map +1 -1
  85. package/dist/search-params/codecs.d.ts +1 -1
  86. package/dist/search-params/define.d.ts +153 -0
  87. package/dist/search-params/define.d.ts.map +1 -0
  88. package/dist/search-params/index.d.ts +4 -5
  89. package/dist/search-params/index.d.ts.map +1 -1
  90. package/dist/search-params/index.js +3 -474
  91. package/dist/search-params/registry.d.ts +1 -1
  92. package/dist/search-params/wrappers.d.ts +53 -0
  93. package/dist/search-params/wrappers.d.ts.map +1 -0
  94. package/dist/server/access-gate.d.ts +4 -0
  95. package/dist/server/access-gate.d.ts.map +1 -1
  96. package/dist/server/action-encryption.d.ts +76 -0
  97. package/dist/server/action-encryption.d.ts.map +1 -0
  98. package/dist/server/action-handler.d.ts.map +1 -1
  99. package/dist/server/als-registry.d.ts +4 -4
  100. package/dist/server/als-registry.d.ts.map +1 -1
  101. package/dist/server/build-manifest.d.ts +2 -2
  102. package/dist/server/early-hints.d.ts +13 -5
  103. package/dist/server/early-hints.d.ts.map +1 -1
  104. package/dist/server/error-boundary-wrapper.d.ts +4 -0
  105. package/dist/server/error-boundary-wrapper.d.ts.map +1 -1
  106. package/dist/server/flight-injection-state.d.ts +78 -0
  107. package/dist/server/flight-injection-state.d.ts.map +1 -0
  108. package/dist/server/form-data.d.ts +29 -0
  109. package/dist/server/form-data.d.ts.map +1 -1
  110. package/dist/server/html-injectors.d.ts.map +1 -1
  111. package/dist/server/index.d.ts +1 -1
  112. package/dist/server/index.d.ts.map +1 -1
  113. package/dist/server/index.js +1819 -1629
  114. package/dist/server/index.js.map +1 -1
  115. package/dist/server/node-stream-transforms.d.ts.map +1 -1
  116. package/dist/server/pipeline.d.ts.map +1 -1
  117. package/dist/server/request-context.d.ts +28 -40
  118. package/dist/server/request-context.d.ts.map +1 -1
  119. package/dist/server/route-element-builder.d.ts +7 -0
  120. package/dist/server/route-element-builder.d.ts.map +1 -1
  121. package/dist/server/route-matcher.d.ts +2 -2
  122. package/dist/server/route-matcher.d.ts.map +1 -1
  123. package/dist/server/rsc-entry/error-renderer.d.ts.map +1 -1
  124. package/dist/server/rsc-entry/index.d.ts.map +1 -1
  125. package/dist/server/slot-resolver.d.ts.map +1 -1
  126. package/dist/server/ssr-entry.d.ts.map +1 -1
  127. package/dist/server/ssr-render.d.ts +3 -0
  128. package/dist/server/ssr-render.d.ts.map +1 -1
  129. package/dist/server/tree-builder.d.ts +12 -8
  130. package/dist/server/tree-builder.d.ts.map +1 -1
  131. package/dist/server/types.d.ts +1 -3
  132. package/dist/server/types.d.ts.map +1 -1
  133. package/dist/server/version-skew.d.ts +61 -0
  134. package/dist/server/version-skew.d.ts.map +1 -0
  135. package/dist/shims/navigation-client.d.ts +1 -1
  136. package/dist/shims/navigation-client.d.ts.map +1 -1
  137. package/dist/shims/navigation.d.ts +1 -1
  138. package/dist/shims/navigation.d.ts.map +1 -1
  139. package/dist/utils/state-machine.d.ts +80 -0
  140. package/dist/utils/state-machine.d.ts.map +1 -0
  141. package/package.json +12 -8
  142. package/src/client/browser-entry.ts +55 -13
  143. package/src/client/error-boundary.tsx +18 -1
  144. package/src/client/index.ts +9 -1
  145. package/src/client/link.tsx +9 -9
  146. package/src/client/navigation-context.ts +2 -2
  147. package/src/client/router.ts +102 -55
  148. package/src/client/rsc-fetch.ts +63 -2
  149. package/src/client/segment-cache.ts +1 -1
  150. package/src/client/stale-reload.ts +28 -0
  151. package/src/client/top-loader.tsx +2 -2
  152. package/src/client/use-params.ts +3 -3
  153. package/src/client/use-query-states.ts +1 -1
  154. package/src/codec.ts +21 -0
  155. package/src/cookies/define-cookie.ts +69 -18
  156. package/src/index.ts +255 -65
  157. package/src/params/define.ts +260 -0
  158. package/src/params/index.ts +28 -0
  159. package/src/plugins/adapter-build.ts +6 -0
  160. package/src/plugins/build-manifest.ts +11 -0
  161. package/src/plugins/client-chunks.ts +65 -0
  162. package/src/plugins/entries.ts +3 -6
  163. package/src/plugins/routing.ts +40 -14
  164. package/src/plugins/server-bundle.ts +32 -1
  165. package/src/plugins/shims.ts +1 -1
  166. package/src/plugins/static-build.ts +8 -4
  167. package/src/routing/codegen.ts +109 -88
  168. package/src/routing/scanner.ts +55 -6
  169. package/src/routing/status-file-lint.ts +2 -1
  170. package/src/routing/types.ts +7 -4
  171. package/src/rsc-runtime/rsc.ts +2 -0
  172. package/src/search-params/codecs.ts +1 -1
  173. package/src/search-params/define.ts +504 -0
  174. package/src/search-params/index.ts +12 -18
  175. package/src/search-params/registry.ts +1 -1
  176. package/src/search-params/wrappers.ts +85 -0
  177. package/src/server/access-gate.tsx +38 -8
  178. package/src/server/action-encryption.ts +144 -0
  179. package/src/server/action-handler.ts +16 -0
  180. package/src/server/als-registry.ts +4 -4
  181. package/src/server/build-manifest.ts +4 -4
  182. package/src/server/early-hints.ts +36 -15
  183. package/src/server/error-boundary-wrapper.ts +57 -14
  184. package/src/server/flight-injection-state.ts +152 -0
  185. package/src/server/form-data.ts +76 -0
  186. package/src/server/html-injectors.ts +42 -26
  187. package/src/server/index.ts +2 -4
  188. package/src/server/node-stream-transforms.ts +68 -41
  189. package/src/server/pipeline.ts +98 -26
  190. package/src/server/request-context.ts +49 -124
  191. package/src/server/route-element-builder.ts +102 -99
  192. package/src/server/route-matcher.ts +2 -2
  193. package/src/server/rsc-entry/error-renderer.ts +3 -2
  194. package/src/server/rsc-entry/index.ts +26 -11
  195. package/src/server/rsc-entry/rsc-payload.ts +2 -2
  196. package/src/server/rsc-entry/ssr-renderer.ts +4 -4
  197. package/src/server/slot-resolver.ts +204 -206
  198. package/src/server/ssr-entry.ts +3 -1
  199. package/src/server/ssr-render.ts +3 -0
  200. package/src/server/tree-builder.ts +84 -48
  201. package/src/server/types.ts +1 -3
  202. package/src/server/version-skew.ts +104 -0
  203. package/src/shims/navigation-client.ts +1 -1
  204. package/src/shims/navigation.ts +1 -1
  205. package/src/utils/state-machine.ts +111 -0
  206. package/dist/_chunks/als-registry-B7DbZ2hS.js.map +0 -1
  207. package/dist/_chunks/interception-BOoWmLUA.js.map +0 -1
  208. package/dist/_chunks/request-context-BQUC8PHn.js.map +0 -1
  209. package/dist/_chunks/ssr-data-MjmprTmO.js +0 -88
  210. package/dist/_chunks/ssr-data-MjmprTmO.js.map +0 -1
  211. package/dist/_chunks/use-cookie-DX-l1_5E.js +0 -91
  212. package/dist/_chunks/use-cookie-DX-l1_5E.js.map +0 -1
  213. package/dist/client/error-boundary.js.map +0 -1
  214. package/dist/cookies/index.js.map +0 -1
  215. package/dist/plugins/dynamic-transform.d.ts +0 -72
  216. package/dist/plugins/dynamic-transform.d.ts.map +0 -1
  217. package/dist/search-params/analyze.d.ts +0 -54
  218. package/dist/search-params/analyze.d.ts.map +0 -1
  219. package/dist/search-params/builtin-codecs.d.ts +0 -105
  220. package/dist/search-params/builtin-codecs.d.ts.map +0 -1
  221. package/dist/search-params/create.d.ts +0 -106
  222. package/dist/search-params/create.d.ts.map +0 -1
  223. package/dist/search-params/index.js.map +0 -1
  224. package/dist/server/prerender.d.ts +0 -77
  225. package/dist/server/prerender.d.ts.map +0 -1
  226. package/src/plugins/dynamic-transform.ts +0 -161
  227. package/src/search-params/analyze.ts +0 -192
  228. package/src/search-params/builtin-codecs.ts +0 -228
  229. package/src/search-params/create.ts +0 -321
  230. package/src/server/prerender.ts +0 -139
@@ -23,7 +23,7 @@ export interface FetchResult {
23
23
  headElements: HeadElement[] | null;
24
24
  /** Segment metadata from X-Timber-Segments header for populating the segment cache. */
25
25
  segmentInfo: SegmentInfo[] | null;
26
- /** Route params from X-Timber-Params header for populating useParams(). */
26
+ /** Route params from X-Timber-Params header for populating useSegmentParams(). */
27
27
  params: Record<string, string | string[]> | null;
28
28
  /** Segment paths that were skipped by the server (for client-side merging). */
29
29
  skippedSegments: string[] | null;
@@ -58,6 +58,43 @@ function appendRscParam(url: string): string {
58
58
  return `${url}${separator}_rsc=${generateCacheBustId()}`;
59
59
  }
60
60
 
61
+ // ─── Deployment ID ───────────────────────────────────────────────
62
+
63
+ /**
64
+ * The client's deployment ID, set at bootstrap from the runtime config.
65
+ * Sent with every RSC/action request for version skew detection.
66
+ * Null in dev mode. See TIM-446.
67
+ */
68
+ let clientDeploymentId: string | null = null;
69
+
70
+ /** Set the client deployment ID. Called once at bootstrap. */
71
+ export function setClientDeploymentId(id: string | null): void {
72
+ clientDeploymentId = id;
73
+ }
74
+
75
+ /** Get the client deployment ID. */
76
+ export function getClientDeploymentId(): string | null {
77
+ return clientDeploymentId;
78
+ }
79
+
80
+ // ─── Reload Signal ───────────────────────────────────────────────
81
+
82
+ /** Header name used by the server to signal a version skew reload. */
83
+ export const RELOAD_HEADER = 'X-Timber-Reload';
84
+
85
+ /** Header name for the client's deployment ID. */
86
+ export const DEPLOYMENT_ID_HEADER = 'X-Timber-Deployment-Id';
87
+
88
+ /**
89
+ * Check if a response signals a version skew reload.
90
+ * Triggers a full page reload if the server indicates the client is stale.
91
+ */
92
+ export function checkReloadSignal(response: Response): boolean {
93
+ return response.headers.get(RELOAD_HEADER) === '1';
94
+ }
95
+
96
+ // ─── Header Builder ──────────────────────────────────────────────
97
+
61
98
  export function buildRscHeaders(
62
99
  stateTree: { segments: string[] } | undefined,
63
100
  currentUrl?: string
@@ -75,6 +112,13 @@ export function buildRscHeaders(
75
112
  if (currentUrl) {
76
113
  headers['X-Timber-URL'] = currentUrl;
77
114
  }
115
+ // Send deployment ID for version skew detection (TIM-446).
116
+ // The server compares this against the current build's ID.
117
+ // On mismatch, the server signals a reload instead of returning
118
+ // an RSC payload with mismatched module references.
119
+ if (clientDeploymentId) {
120
+ headers[DEPLOYMENT_ID_HEADER] = clientDeploymentId;
121
+ }
78
122
  return headers;
79
123
  }
80
124
 
@@ -135,7 +179,7 @@ export function extractSkippedSegments(response: Response): string[] | null {
135
179
  * Extract route params from the X-Timber-Params response header.
136
180
  * Returns null if the header is missing or malformed.
137
181
  *
138
- * Used to populate useParams() after client-side navigation.
182
+ * Used to populate useSegmentParams() after client-side navigation.
139
183
  */
140
184
  export function extractParams(response: Response): Record<string, string | string[]> | null {
141
185
  const header = response.headers.get('X-Timber-Params');
@@ -161,6 +205,17 @@ export class RedirectError extends Error {
161
205
  }
162
206
  }
163
207
 
208
+ /**
209
+ * Thrown when the server signals a version skew (X-Timber-Reload header).
210
+ * Caught in navigate() to trigger a full page reload via triggerStaleReload().
211
+ * See TIM-446.
212
+ */
213
+ export class VersionSkewError extends Error {
214
+ constructor() {
215
+ super('Version skew detected — server has been redeployed');
216
+ }
217
+ }
218
+
164
219
  // ─── Fetch ───────────────────────────────────────────────────────
165
220
 
166
221
  /**
@@ -192,6 +247,12 @@ export async function fetchRscPayload(
192
247
  let params: Record<string, string | string[]> | null = null;
193
248
  let skippedSegments: string[] | null = null;
194
249
  const wrappedPromise = fetchPromise.then((response) => {
250
+ // Version skew detection (TIM-446): if the server signals a reload,
251
+ // throw VersionSkewError so the caller (router navigate) can trigger
252
+ // a full page reload via triggerStaleReload().
253
+ if (checkReloadSignal(response)) {
254
+ throw new VersionSkewError();
255
+ }
195
256
  // Detect server-side redirects. The server returns 204 + X-Timber-Redirect
196
257
  // for RSC payload requests instead of a raw 302, because fetch with
197
258
  // redirect: "manual" turns 302s into opaque redirects (status 0, null body)
@@ -11,7 +11,7 @@ export interface PrefetchResult {
11
11
  headElements: HeadElement[] | null;
12
12
  /** Segment metadata from X-Timber-Segments header for populating the segment cache. */
13
13
  segmentInfo?: SegmentInfo[] | null;
14
- /** Route params from X-Timber-Params header for populating useParams(). */
14
+ /** Route params from X-Timber-Params header for populating useSegmentParams(). */
15
15
  params?: Record<string, string | string[]> | null;
16
16
  /** Segment paths skipped by the server (for client-side merging). */
17
17
  skippedSegments?: string[] | null;
@@ -32,6 +32,34 @@ export function isStaleClientReference(error: unknown): boolean {
32
32
  return msg.includes('Could not find the module') || msg.includes('client reference not found');
33
33
  }
34
34
 
35
+ /**
36
+ * Check if an error is a chunk load failure from a dynamic import.
37
+ *
38
+ * After a deployment, old chunk filenames no longer exist. When the client
39
+ * tries to dynamically import a chunk that's been replaced, the browser
40
+ * throws one of these errors:
41
+ *
42
+ * - Chromium: "Failed to fetch dynamically imported module: <url>"
43
+ * - Firefox: "error loading dynamically imported module: <url>"
44
+ * - Safari: "Importing a module script failed."
45
+ * - Vite/Rollup: "Unable to preload CSS for <url>"
46
+ *
47
+ * See TIM-446
48
+ */
49
+ export function isChunkLoadError(error: unknown): boolean {
50
+ if (!(error instanceof Error)) return false;
51
+ const msg = error.message.toLowerCase();
52
+ return (
53
+ msg.includes('failed to fetch dynamically imported module') ||
54
+ msg.includes('error loading dynamically imported module') ||
55
+ msg.includes('importing a module script failed') ||
56
+ msg.includes('unable to preload css') ||
57
+ // Webpack-style chunk load errors (unlikely in Vite but defensive)
58
+ msg.includes('loading chunk') ||
59
+ msg.includes('loading css chunk')
60
+ );
61
+ }
62
+
35
63
  /**
36
64
  * Trigger a full page reload to pick up new bundles.
37
65
  *
@@ -39,7 +39,7 @@ export interface TopLoaderConfig {
39
39
  color?: string;
40
40
  /** Bar height in pixels. Default: 3. */
41
41
  height?: number;
42
- /** Show subtle glow/shadow effect. Default: true. */
42
+ /** Show subtle glow/shadow effect. Default: false. */
43
43
  shadow?: boolean;
44
44
  /** Delay in ms before showing the bar. Default: 0. */
45
45
  delay?: number;
@@ -51,7 +51,7 @@ export interface TopLoaderConfig {
51
51
 
52
52
  const DEFAULT_COLOR = '#2299DD';
53
53
  const DEFAULT_HEIGHT = 3;
54
- const DEFAULT_SHADOW = true;
54
+ const DEFAULT_SHADOW = false;
55
55
  const DEFAULT_DELAY = 0;
56
56
  const DEFAULT_Z_INDEX = 1600;
57
57
 
@@ -119,9 +119,9 @@ export function notifyParamsListeners(): void {
119
119
  * exact params shape from the generated Routes interface.
120
120
  * @overload Fallback — returns the generic params record.
121
121
  */
122
- export function useParams<R extends keyof Routes>(route: R): Routes[R]['params'];
123
- export function useParams(route?: string): Record<string, string | string[]>;
124
- export function useParams(_route?: string): Record<string, string | string[]> {
122
+ export function useSegmentParams<R extends keyof Routes>(route: R): Routes[R]['params'];
123
+ export function useSegmentParams(route?: string): Record<string, string | string[]>;
124
+ export function useSegmentParams(_route?: string): Record<string, string | string[]> {
125
125
  // Try reading from NavigationContext (client-side, inside React tree).
126
126
  // During SSR, no NavigationProvider is mounted, so this returns null.
127
127
  // When called outside a React component, useContext throws — caught below.
@@ -17,7 +17,7 @@ import type {
17
17
  SearchParamsDefinition,
18
18
  SetParams,
19
19
  QueryStatesOptions,
20
- } from '#/search-params/create.js';
20
+ } from '#/search-params/define.js';
21
21
  import { getSearchParams } from '#/search-params/registry.js';
22
22
 
23
23
  // ─── Codec Bridge ─────────────────────────────────────────────────
package/src/codec.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Shared codec protocol for parsing and serializing string values.
3
+ *
4
+ * Used by both search params and cookies. Any object with parse + serialize
5
+ * methods satisfies this interface. nuqs parsers are valid codecs natively.
6
+ *
7
+ * Design doc: design/23a-search-params-triage.md §"Unify Codec<T> type"
8
+ */
9
+
10
+ /**
11
+ * A codec that converts between string values and typed values.
12
+ *
13
+ * The canonical protocol shared across search params, cookies, and
14
+ * any future timber feature that needs string ↔ typed conversion.
15
+ */
16
+ export interface Codec<T> {
17
+ /** String → typed value. Receives undefined when the value is absent. */
18
+ parse(value: string | string[] | undefined): T;
19
+ /** Typed value → string. Return null to omit/clear. */
20
+ serialize(value: T): string | null;
21
+ }
@@ -2,36 +2,42 @@
2
2
  * defineCookie — typed cookie definitions.
3
3
  *
4
4
  * Bundles name + codec + options into a reusable CookieDefinition<T>
5
- * with .get(), .set(), .delete() server methods and a .useCookie() client hook.
5
+ * with async .getCookie(), .setCookie(), .deleteCookie() server methods
6
+ * and a sync .useCookie() client hook.
7
+ *
8
+ * Server methods are async to future-proof the API for v2 features
9
+ * (signed cookies via crypto.subtle, encrypted cookies, external stores).
6
10
  *
7
11
  * Reuses the SearchParamCodec protocol via fromSchema() bridge.
8
12
  * Validation on read returns the codec default (never throws).
9
13
  *
14
+ * IMPORTANT: This module must NOT have top-level value imports from either
15
+ * server or client modules. Server methods lazy-import request-context;
16
+ * useCookie() lazy-imports use-cookie. This ensures:
17
+ * - Client bundles don't pull in ALS/server code
18
+ * - RSC bundles don't pull in useSyncExternalStore/client code
19
+ * - Tree-shaking is not required for correctness
20
+ *
10
21
  * See design/29-cookies.md §"Typed Cookies with Schema Validation"
11
22
  */
12
23
 
13
- import { cookies } from '#/server/request-context.js';
14
24
  import type { CookieOptions } from '#/server/request-context.js';
15
- import { useCookie as useRawCookie } from '#/client/use-cookie.js';
16
25
  import type { ClientCookieOptions } from '#/client/use-cookie.js';
17
26
 
18
27
  // ─── Types ────────────────────────────────────────────────────────────────
19
28
 
29
+ import type { Codec } from '#/codec.js';
30
+
20
31
  /**
21
32
  * A codec that converts between string cookie values and typed values.
22
- * Intentionally identical to SearchParamCodec<T>.
33
+ * Type alias for the shared Codec<T> protocol.
23
34
  */
24
- export interface CookieCodec<T> {
25
- parse(value: string | string[] | undefined): T;
26
- serialize(value: T): string | null;
27
- }
35
+ export type CookieCodec<T> = Codec<T>;
28
36
 
29
37
  /** Options for defineCookie: codec + CookieOptions merged. */
30
- export interface DefineCookieOptions<T> extends Omit<CookieOptions, 'signed'> {
38
+ export interface DefineCookieOptions<T> extends CookieOptions {
31
39
  /** Codec for parsing/serializing the cookie value. */
32
40
  codec: CookieCodec<T>;
33
- /** Sign the cookie with HMAC-SHA256. */
34
- signed?: boolean;
35
41
  }
36
42
 
37
43
  /** A fully typed cookie definition with server and client methods. */
@@ -44,16 +50,49 @@ export interface CookieDefinition<T> {
44
50
  readonly codec: CookieCodec<T>;
45
51
 
46
52
  /** Server: read the typed value from the current request. */
47
- get(): T;
53
+ getCookie(): Promise<T>;
48
54
  /** Server: set the typed value on the response. */
49
- set(value: T): void;
55
+ setCookie(value: T): Promise<void>;
50
56
  /** Server: delete the cookie. */
51
- delete(): void;
57
+ deleteCookie(): Promise<void>;
52
58
 
53
59
  /** Client: React hook for reading/writing this cookie. Returns [value, setter, deleter]. */
54
60
  useCookie(): [T, (value: T) => void, () => void];
55
61
  }
56
62
 
63
+ // ─── Lazy Module References ───────────────────────────────────────────────
64
+ //
65
+ // These are resolved on first use, not at module load time. This prevents
66
+ // the server module graph from pulling in client code and vice versa.
67
+ // The dynamic import() in server methods is natural (they're async).
68
+ // For useCookie() (sync), we cache the module reference after first load.
69
+
70
+ let _useCookieModule: typeof import('#/client/use-cookie.js') | undefined;
71
+
72
+ function getUseCookieModule(): typeof import('#/client/use-cookie.js') {
73
+ if (!_useCookieModule) {
74
+ // In the client/SSR environment, this module is already in the module
75
+ // graph (imported by the client entry). The throw is a safeguard —
76
+ // if useCookie() is somehow called before the module is available,
77
+ // the developer gets a clear error instead of a silent failure.
78
+ throw new Error(
79
+ '[timber] defineCookie().useCookie() requires @timber-js/app/client to be loaded. ' +
80
+ 'This hook can only be used in client components.'
81
+ );
82
+ }
83
+ return _useCookieModule;
84
+ }
85
+
86
+ /**
87
+ * Register the client cookie module. Called by the client entry to wire
88
+ * up the lazy reference without a top-level import.
89
+ *
90
+ * @internal — framework use only
91
+ */
92
+ export function _registerUseCookieModule(mod: typeof import('#/client/use-cookie.js')): void {
93
+ _useCookieModule = mod;
94
+ }
95
+
57
96
  // ─── Factory ──────────────────────────────────────────────────────────────
58
97
 
59
98
  /**
@@ -69,6 +108,13 @@ export interface CookieDefinition<T> {
69
108
  * httpOnly: false,
70
109
  * maxAge: 60 * 60 * 24 * 365,
71
110
  * });
111
+ *
112
+ * // Server
113
+ * const theme = await themeCookie.getCookie();
114
+ * await themeCookie.setCookie('dark');
115
+ *
116
+ * // Client
117
+ * const [theme, setTheme] = themeCookie.useCookie();
72
118
  * ```
73
119
  */
74
120
  export function defineCookie<T>(
@@ -83,13 +129,15 @@ export function defineCookie<T>(
83
129
  options: resolvedOptions,
84
130
  codec,
85
131
 
86
- get(): T {
132
+ async getCookie(): Promise<T> {
133
+ const { cookies } = await import('#/server/request-context.js');
87
134
  const jar = cookies();
88
- const raw = resolvedOptions.signed ? jar.getSigned(name) : jar.get(name);
135
+ const raw = jar.get(name);
89
136
  return codec.parse(raw);
90
137
  },
91
138
 
92
- set(value: T): void {
139
+ async setCookie(value: T): Promise<void> {
140
+ const { cookies } = await import('#/server/request-context.js');
93
141
  const serialized = codec.serialize(value);
94
142
  if (serialized === null) {
95
143
  cookies().delete(name, {
@@ -101,7 +149,8 @@ export function defineCookie<T>(
101
149
  }
102
150
  },
103
151
 
104
- delete(): void {
152
+ async deleteCookie(): Promise<void> {
153
+ const { cookies } = await import('#/server/request-context.js');
105
154
  cookies().delete(name, {
106
155
  path: resolvedOptions.path,
107
156
  domain: resolvedOptions.domain,
@@ -109,6 +158,8 @@ export function defineCookie<T>(
109
158
  },
110
159
 
111
160
  useCookie(): [T, (value: T) => void, () => void] {
161
+ const { useCookie: useRawCookie } = getUseCookieModule();
162
+
112
163
  // Extract client-safe options (no httpOnly — client cookies can't be httpOnly)
113
164
  const clientOpts: ClientCookieOptions = {
114
165
  path: resolvedOptions.path,