@timber-js/app 0.2.0-alpha.224 → 0.2.0-alpha.226
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks/{actions-7dTXYJfu.js → actions-B1xSIv32.js} +2 -2
- package/dist/_chunks/{actions-7dTXYJfu.js.map → actions-B1xSIv32.js.map} +1 -1
- package/dist/_chunks/{cache-api-TDZ4hlDc.js → cache-api-DsSXA4Sp.js} +68 -47
- package/dist/_chunks/cache-api-DsSXA4Sp.js.map +1 -0
- package/dist/_chunks/{cli-check-Dv3f8d2j.js → cli-check-B33yhAPa.js} +2 -2
- package/dist/_chunks/{cli-check-Dv3f8d2j.js.map → cli-check-B33yhAPa.js.map} +1 -1
- package/dist/_chunks/cli-context-BPdyA7Fp.js +1 -0
- package/dist/_chunks/{cli-context-CbZp32SK.js → cli-context-CwTKMjVe.js} +10 -2
- package/dist/_chunks/cli-context-CwTKMjVe.js.map +1 -0
- package/dist/_chunks/{error-boundary-DRQXGWCp.js → error-boundary-D3YyVVuP.js} +2 -2
- package/dist/_chunks/{error-boundary-DRQXGWCp.js.map → error-boundary-D3YyVVuP.js.map} +1 -1
- package/dist/_chunks/navigation-root-M3B2ZrdO.js.map +1 -1
- package/dist/_chunks/{singleflight-BChPMjwH.js → singleflight-CzoOVB9Y.js} +2 -3
- package/dist/_chunks/singleflight-CzoOVB9Y.js.map +1 -0
- package/dist/_chunks/{status-page-marker-XIQXlTOb.js → status-page-marker-C8POawuF.js} +2 -2
- package/dist/_chunks/{status-page-marker-XIQXlTOb.js.map → status-page-marker-C8POawuF.js.map} +1 -1
- package/dist/adapters/cloudflare-kv-cache.d.ts +0 -1
- package/dist/adapters/cloudflare-kv-cache.d.ts.map +1 -1
- package/dist/adapters/cloudflare-kv-cache.js.map +1 -1
- package/dist/cache/index.d.ts +5 -5
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +1 -1
- package/dist/cache/redis-handler.d.ts +0 -1
- package/dist/cache/redis-handler.d.ts.map +1 -1
- package/dist/cache/singleflight.d.ts +12 -2
- package/dist/cache/singleflight.d.ts.map +1 -1
- package/dist/cache/stable-stringify.d.ts +6 -0
- package/dist/cache/stable-stringify.d.ts.map +1 -1
- package/dist/cache/tag-aware-handler.d.ts +0 -1
- package/dist/cache/tag-aware-handler.d.ts.map +1 -1
- package/dist/cache/timber-cache.d.ts +1 -1
- package/dist/cache/timber-cache.d.ts.map +1 -1
- package/dist/cli-context.d.ts +8 -0
- package/dist/cli-context.d.ts.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +8 -2
- package/dist/cli.js.map +1 -1
- package/dist/client/error-boundary.js +1 -1
- package/dist/client/internal.js +3 -3
- package/dist/client/navigation-root.d.ts.map +1 -1
- package/dist/client/nuqs-adapter.d.ts +4 -1
- package/dist/client/nuqs-adapter.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +112 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/client-in-server-package-deps.d.ts +63 -0
- package/dist/plugins/client-in-server-package-deps.d.ts.map +1 -0
- package/dist/plugins/optimize-package-imports.d.ts +11 -0
- package/dist/plugins/optimize-package-imports.d.ts.map +1 -1
- package/dist/server/flight-injector-core.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/internal.js +162 -33
- package/dist/server/internal.js.map +1 -1
- package/dist/server/pipeline-helpers.d.ts +5 -2
- package/dist/server/pipeline-helpers.d.ts.map +1 -1
- package/dist/server/pipeline.d.ts.map +1 -1
- package/dist/server/prebuilt-runtime.d.ts.map +1 -1
- package/dist/server/response-framing.d.ts +43 -0
- package/dist/server/response-framing.d.ts.map +1 -0
- package/dist/server/ssr-entry.d.ts.map +1 -1
- package/dist/server/ssr-wrappers.d.ts +9 -10
- package/dist/server/ssr-wrappers.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/adapters/cloudflare-kv-cache.ts +1 -5
- package/src/cache/index.ts +7 -27
- package/src/cache/redis-handler.ts +1 -5
- package/src/cache/singleflight.ts +17 -5
- package/src/cache/stable-stringify.ts +53 -0
- package/src/cache/tag-aware-handler.ts +1 -5
- package/src/cache/timber-cache.ts +55 -52
- package/src/cli-context.ts +9 -0
- package/src/cli.ts +16 -0
- package/src/client/navigation-root.tsx +7 -6
- package/src/client/nuqs-adapter.tsx +46 -28
- package/src/index.ts +2 -0
- package/src/plugins/client-in-server-package-deps.ts +139 -0
- package/src/plugins/optimize-package-imports.ts +1 -1
- package/src/server/flight-injector-core.ts +13 -9
- package/src/server/pipeline-helpers.ts +9 -5
- package/src/server/pipeline.ts +7 -0
- package/src/server/prebuilt-runtime.ts +125 -121
- package/src/server/response-framing.ts +59 -0
- package/src/server/ssr-entry.ts +0 -2
- package/src/server/ssr-wrappers.tsx +39 -49
- package/dist/_chunks/cache-api-TDZ4hlDc.js.map +0 -1
- package/dist/_chunks/cli-context-CbZp32SK.js.map +0 -1
- package/dist/_chunks/cli-context-n5hmkdla.js +0 -1
- package/dist/_chunks/singleflight-BChPMjwH.js.map +0 -1
- package/dist/server/nuqs-ssr-provider.d.ts +0 -32
- package/dist/server/nuqs-ssr-provider.d.ts.map +0 -1
- package/src/server/nuqs-ssr-provider.tsx +0 -64
package/src/cli-context.ts
CHANGED
|
@@ -33,6 +33,15 @@ export interface PluginWithApi {
|
|
|
33
33
|
api?: unknown;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Whether a resolved Vite plugin list includes timber. Vite resolves a
|
|
38
|
+
* missing config file to an empty one rather than failing, so a caller that
|
|
39
|
+
* needs timber must ask — `timber dev` would otherwise serve plain Vite.
|
|
40
|
+
*/
|
|
41
|
+
export function hasTimberPlugin(plugins: readonly { name: string }[]): boolean {
|
|
42
|
+
return plugins.some((plugin) => plugin.name === TIMBER_ROOT_SYNC_PLUGIN);
|
|
43
|
+
}
|
|
44
|
+
|
|
36
45
|
/**
|
|
37
46
|
* Vite's own `resolveConfig`, narrowed to what this module passes and reads.
|
|
38
47
|
*
|
package/src/cli.ts
CHANGED
|
@@ -177,6 +177,22 @@ export async function runDev(options: CommandOptions, _deps?: ViteDeps): Promise
|
|
|
177
177
|
const server = await createServer({
|
|
178
178
|
configFile: options.config,
|
|
179
179
|
});
|
|
180
|
+
// The supervisor respawns this worker whenever a config file changes,
|
|
181
|
+
// including when a git checkout removes it. Vite treats a missing config
|
|
182
|
+
// as an empty one, so without this check the respawned worker would serve
|
|
183
|
+
// plain Vite on its default port: no app, and full-page reloads for every
|
|
184
|
+
// .html file it sees, the build output included.
|
|
185
|
+
const { hasTimberPlugin } = await import('./cli-context.ts');
|
|
186
|
+
if (!hasTimberPlugin(server.config.plugins)) {
|
|
187
|
+
await server.close();
|
|
188
|
+
const where = server.config.configFile ?? `no Vite config found in ${server.config.root}`;
|
|
189
|
+
throw new Error(
|
|
190
|
+
`[timber] The Vite config does not use the timber plugin (${where}).\n\n` +
|
|
191
|
+
` \`timber dev\` stopped instead of starting plain Vite. This happens when a\n` +
|
|
192
|
+
` config file changes on disk, for example a git checkout to a branch\n` +
|
|
193
|
+
` without timber. Run \`timber dev\` again once vite.config.ts lists timber().`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
180
196
|
await server.listen();
|
|
181
197
|
server.printUrls();
|
|
182
198
|
return;
|
|
@@ -218,19 +218,20 @@ export function NavigationRoot({
|
|
|
218
218
|
|
|
219
219
|
// Inject TopLoader alongside the element tree. It subscribes to the router's
|
|
220
220
|
// pending store itself, so it takes no props from here beyond its config,
|
|
221
|
-
// and only it re-renders when a navigation starts or ends.
|
|
222
|
-
// when
|
|
221
|
+
// and only it re-renders when a navigation starts or ends. Its slot holds
|
|
222
|
+
// `null` when the loader is disabled via config.
|
|
223
223
|
//
|
|
224
224
|
// This Fragment is a FORK — two children — so it advances React's tree
|
|
225
225
|
// context and shifts every `useId` below it. `server/ssr-wrappers.tsx`
|
|
226
|
-
// mirrors it for exactly that reason
|
|
227
|
-
//
|
|
226
|
+
// mirrors it for exactly that reason, and renders the document's default
|
|
227
|
+
// <meta> tags in the first slot. The fork is unconditional so the server
|
|
228
|
+
// never needs to know whether the loader is enabled: a `null` child still
|
|
229
|
+
// counts toward the fork on both sides. See tests/ssr-wrapper-parity.test.ts.
|
|
228
230
|
const showTopLoader = topLoaderConfig?.enabled !== false;
|
|
229
|
-
if (!showTopLoader) return rendered.element;
|
|
230
231
|
return createElement(
|
|
231
232
|
Fragment,
|
|
232
233
|
null,
|
|
233
|
-
createElement(TopLoader, { config: topLoaderConfig }),
|
|
234
|
+
showTopLoader ? createElement(TopLoader, { config: topLoaderConfig }) : null,
|
|
234
235
|
rendered.element
|
|
235
236
|
);
|
|
236
237
|
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
'use client';
|
|
14
14
|
|
|
15
|
-
import {
|
|
15
|
+
import { useMemo, type ReactNode } from 'react';
|
|
16
16
|
import {
|
|
17
17
|
unstable_createAdapterProvider as createAdapterProvider,
|
|
18
18
|
renderQueryString,
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
import { getRouter } from './router-ref.ts';
|
|
23
23
|
import { getNavigationApi } from './navigation-api.ts';
|
|
24
24
|
import { locationSearch } from './location-search.ts';
|
|
25
|
+
import { useNavigationContext } from './navigation-context.ts';
|
|
25
26
|
|
|
26
27
|
// ─── Adapter Hook ─────────────────────────────────────────────────
|
|
27
28
|
|
|
@@ -29,28 +30,36 @@ import { locationSearch } from './location-search.ts';
|
|
|
29
30
|
* Custom adapter hook for nuqs. Returns the current search params
|
|
30
31
|
* and an updateUrl function that integrates with timber's router.
|
|
31
32
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
33
|
+
* nuqs calls this once per `useQueryStates` call, from the calling
|
|
34
|
+
* component, so every instance must read ONE source. It reads the router's:
|
|
35
|
+
* `NavigationContext.search`, the value `useSearchParams()` reads. The router
|
|
36
|
+
* moves it on every navigation, traversal and shallow write — the Navigation
|
|
37
|
+
* API's shallow handler and the History API patch both run
|
|
38
|
+
* `syncShallowSearch` (browser-entry/router-init.ts) — so the adapter needs
|
|
39
|
+
* no listeners of its own, and nuqs and `useSearchParams()` cannot disagree.
|
|
40
|
+
* During SSR the same provider carries the request's raw query string, so
|
|
41
|
+
* this one hook serves both sides.
|
|
42
|
+
*
|
|
43
|
+
* Per-instance state used to live here instead: a shallow write moved only
|
|
44
|
+
* the writer's copy, and every other `useQueryStates` call snapped back to
|
|
45
|
+
* the old value on its next render.
|
|
46
|
+
*
|
|
47
|
+
* @param watchKeys - URL keys this hook instance reads. The returned params
|
|
48
|
+
* hold only those keys and keep their identity while those keys are
|
|
49
|
+
* unchanged, so a write to another key does not re-render this instance's
|
|
50
|
+
* consumers through nuqs.
|
|
34
51
|
*/
|
|
35
|
-
function useTimberAdapter(
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// timber dispatches a custom event after client navigations complete
|
|
47
|
-
window.addEventListener('timber:navigation-end', sync);
|
|
48
|
-
|
|
49
|
-
return () => {
|
|
50
|
-
window.removeEventListener('popstate', sync);
|
|
51
|
-
window.removeEventListener('timber:navigation-end', sync);
|
|
52
|
-
};
|
|
53
|
-
}, []);
|
|
52
|
+
function useTimberAdapter(watchKeys: string[]): AdapterInterface {
|
|
53
|
+
const nav = useNavigationContext();
|
|
54
|
+
if (nav === null) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
'[timber] useQueryStates() was called outside the timber app tree ' +
|
|
57
|
+
'(no NavigationProvider found). In tests, render the component ' +
|
|
58
|
+
'inside the timber providers.'
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
const watched = filterKeys(new URLSearchParams(nav.search), watchKeys).toString();
|
|
62
|
+
const searchParams = useMemo(() => new URLSearchParams(watched), [watched]);
|
|
54
63
|
|
|
55
64
|
const updateUrl = useMemo(() => {
|
|
56
65
|
return (search: URLSearchParams, options: Required<AdapterOptions>) => {
|
|
@@ -63,9 +72,9 @@ function useTimberAdapter(_watchKeys: string[]): AdapterInterface {
|
|
|
63
72
|
// info: { shallow: true } so the navigate event handler knows to
|
|
64
73
|
// skip interception. Without this, the navigate event fired by
|
|
65
74
|
// history.pushState/replaceState would trigger a full RSC fetch.
|
|
66
|
-
const
|
|
67
|
-
if (
|
|
68
|
-
|
|
75
|
+
const navigation = getNavigationApi();
|
|
76
|
+
if (navigation) {
|
|
77
|
+
navigation.navigate(url.toString(), {
|
|
69
78
|
history: options.history === 'push' ? 'push' : 'replace',
|
|
70
79
|
info: { shallow: true },
|
|
71
80
|
});
|
|
@@ -75,8 +84,8 @@ function useTimberAdapter(_watchKeys: string[]): AdapterInterface {
|
|
|
75
84
|
method.call(window.history, window.history.state, '', url.toString());
|
|
76
85
|
}
|
|
77
86
|
|
|
78
|
-
//
|
|
79
|
-
|
|
87
|
+
// No local state to set: both paths reach the router's
|
|
88
|
+
// syncShallowSearch, which re-renders NavigationContext.
|
|
80
89
|
} else {
|
|
81
90
|
// Non-shallow (timber default): trigger RSC navigation to fetch
|
|
82
91
|
// fresh server data for the new search params.
|
|
@@ -100,6 +109,12 @@ function useTimberAdapter(_watchKeys: string[]): AdapterInterface {
|
|
|
100
109
|
};
|
|
101
110
|
}
|
|
102
111
|
|
|
112
|
+
/** The entries of `search` whose key is in `keys`; all of them when `keys` is empty. */
|
|
113
|
+
function filterKeys(search: URLSearchParams, keys: string[]): URLSearchParams {
|
|
114
|
+
if (keys.length === 0) return search;
|
|
115
|
+
return new URLSearchParams([...search].filter(([key]) => keys.includes(key)));
|
|
116
|
+
}
|
|
117
|
+
|
|
103
118
|
// ─── Provider Component ───────────────────────────────────────────
|
|
104
119
|
|
|
105
120
|
// Lazily created — createAdapterProvider calls React.createElement internally,
|
|
@@ -111,7 +126,10 @@ let _TimberNuqsProvider: ReturnType<typeof createAdapterProvider> | undefined;
|
|
|
111
126
|
* Wraps the React tree with nuqs's adapter context, configured with
|
|
112
127
|
* timber's default options.
|
|
113
128
|
*
|
|
114
|
-
* Auto-injected
|
|
129
|
+
* Auto-injected around the app on both sides — the browser's root render
|
|
130
|
+
* (browser-entry/router-init.ts) and the SSR wrapper chain
|
|
131
|
+
* (server/ssr-wrappers.tsx) — no user setup required. It must sit above
|
|
132
|
+
* NavigationProvider's consumers, which every `useQueryStates` caller is.
|
|
115
133
|
*/
|
|
116
134
|
export function TimberNuqsAdapter({ children }: { children: ReactNode }) {
|
|
117
135
|
const Provider = (_TimberNuqsProvider ??= createAdapterProvider(useTimberAdapter));
|
package/src/index.ts
CHANGED
|
@@ -36,6 +36,7 @@ import { timberEncryptionRuntime } from './plugins/encryption-runtime.ts';
|
|
|
36
36
|
import { timberCacheTransform } from './plugins/cache.ts';
|
|
37
37
|
import { timberRequestDep } from './plugins/request-dep/index.ts';
|
|
38
38
|
import { timberOptimizePackageImports } from './plugins/optimize-package-imports.ts';
|
|
39
|
+
import { timberClientInServerPackageDeps } from './plugins/client-in-server-package-deps.ts';
|
|
39
40
|
import { timberPrebuilt } from './plugins/prebuilt.ts';
|
|
40
41
|
import { timberPrerenderSugar } from './plugins/prerender-sugar.ts';
|
|
41
42
|
import { timberCloudflareWasm } from './plugins/cloudflare-wasm.ts';
|
|
@@ -579,6 +580,7 @@ export function timber(config?: TimberUserConfig): PluginOption[] {
|
|
|
579
580
|
fonts,
|
|
580
581
|
timberMdx(ctx),
|
|
581
582
|
timberOptimizePackageImports(ctx), // Listed barrels → the modules that define the imported names
|
|
583
|
+
timberClientInServerPackageDeps(), // Dev: a package's inner 'use client' file → its bare path, so the browser pre-bundles it
|
|
582
584
|
timberRequestDep(ctx),
|
|
583
585
|
timberCacheTransform(ctx),
|
|
584
586
|
timberPrerenderSugar(ctx), // export const prerender → cache.component() desugaring
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* timber-client-in-server-package-deps — in dev, the RSC environment resolves
|
|
3
|
+
* a package's inner 'use client' file through its bare package path, so the
|
|
4
|
+
* browser loads it from the client pre-bundle.
|
|
5
|
+
*
|
|
6
|
+
* plugin-rsc records a package source only for a file the RSC environment
|
|
7
|
+
* reaches through a bare import. A server component that reaches a 'use
|
|
8
|
+
* client' file through the package's own relative imports (lucide-react's
|
|
9
|
+
* `icons/play.mjs` → `createLucideIcon.mjs` → `./Icon.mjs`, or
|
|
10
|
+
* `@mui/material/Button` → `./Button.mjs`) gets a client reference whose
|
|
11
|
+
* browser module re-exports the file's absolute path
|
|
12
|
+
* (`client-in-server-package-proxy`). Vite serves that file, and everything it
|
|
13
|
+
* imports, raw:
|
|
14
|
+
*
|
|
15
|
+
* - If an app client component also imports the package, the browser holds
|
|
16
|
+
* two instances of those modules, the pre-bundled one and the raw one, so a
|
|
17
|
+
* context the package provides (`LucideProvider`) does not reach icons
|
|
18
|
+
* rendered by server components. plugin-rsc detects this and warns "client
|
|
19
|
+
* component dependency is inconsistently optimized" on every dev start.
|
|
20
|
+
* - If the file imports a CommonJS package (`prop-types`), the browser
|
|
21
|
+
* cannot link it and the page fails (vitejs/vite-plugin-react#1538).
|
|
22
|
+
*
|
|
23
|
+
* This plugin resolves such an import as the bare specifier the file has from
|
|
24
|
+
* the app root (`lucide-react/dist/esm/Icon.mjs`). plugin-rsc then records that
|
|
25
|
+
* specifier as the file's package source and gives the browser
|
|
26
|
+
* `client-package-proxy`, which imports the bare specifier. The client deps
|
|
27
|
+
* optimizer pre-bundles it like any package import, splitting the modules it
|
|
28
|
+
* shares with the package's own entry into one chunk, so the browser loads one
|
|
29
|
+
* instance. No inconsistency remains, so plugin-rsc does not warn.
|
|
30
|
+
*
|
|
31
|
+
* Every rule leans toward leaving the import alone, which keeps plugin-rsc's
|
|
32
|
+
* raw-file behavior:
|
|
33
|
+
* - only in the RSC environment, in dev (a build has no proxy modules);
|
|
34
|
+
* - only for a non-bare source (a bare one already gets a package source);
|
|
35
|
+
* - only from a package file, or for an absolute path into `node_modules`
|
|
36
|
+
* (what the barrel rewrite emits), so an app module's own relative imports
|
|
37
|
+
* are not resolved twice;
|
|
38
|
+
* - only when the resolved id is a plain file (no query but Vite's `?v=`; a
|
|
39
|
+
* `?raw` or `?url` import asks for something other than the module), under
|
|
40
|
+
* `node_modules`, whose directive prologue holds 'use client';
|
|
41
|
+
* - only when the bare specifier resolves to the same file, from the importer
|
|
42
|
+
* here and from the app root in plugin-rsc. A package whose `exports` hides
|
|
43
|
+
* the file, or a transitive dependency the app cannot import by name, stays
|
|
44
|
+
* as it is.
|
|
45
|
+
*
|
|
46
|
+
* A file found after startup is a new client dep, so the client optimizer
|
|
47
|
+
* re-runs once, as for any import discovered late; Vite keeps it in the dep
|
|
48
|
+
* cache for later starts. See design/21-dev-server.md §"Inner 'use client'
|
|
49
|
+
* Files of Packages".
|
|
50
|
+
*
|
|
51
|
+
* Remove once plugin-rsc imports these files through the client pre-bundle
|
|
52
|
+
* itself.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
import { readFile } from 'node:fs/promises';
|
|
56
|
+
import type { Plugin } from 'vite';
|
|
57
|
+
import { detectDirective } from '../analyze/classify.ts';
|
|
58
|
+
import { asFilePath } from './optimize-package-imports.ts';
|
|
59
|
+
|
|
60
|
+
const NODE_MODULES = '/node_modules/';
|
|
61
|
+
|
|
62
|
+
// Vite's own test for a bare import (packages/vite/src/node/utils.ts).
|
|
63
|
+
const BARE_IMPORT_RE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The bare specifier `file` has as a package path, or null when the file is not
|
|
67
|
+
* inside a package. The last `node_modules` segment is the package, so a pnpm
|
|
68
|
+
* store path (`.pnpm/lucide-react@1.66.0/node_modules/lucide-react/…`) names
|
|
69
|
+
* the package, not the store.
|
|
70
|
+
*/
|
|
71
|
+
export function packagePathOf(file: string): string | null {
|
|
72
|
+
const index = file.lastIndexOf(NODE_MODULES);
|
|
73
|
+
if (index === -1) return null;
|
|
74
|
+
const id = file.slice(index + NODE_MODULES.length);
|
|
75
|
+
return id === '' || id.startsWith('.') ? null : id;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function timberClientInServerPackageDeps(rscEnvironmentName = 'rsc'): Plugin {
|
|
79
|
+
// File path → whether its directive prologue holds 'use client'. Installed
|
|
80
|
+
// packages are assumed not to change while the process runs, as the barrel
|
|
81
|
+
// rewrite assumes. The read is cached as a promise so concurrent resolves
|
|
82
|
+
// share it; a failed read caches false (the import is left alone).
|
|
83
|
+
const isClientFile = new Map<string, Promise<boolean>>();
|
|
84
|
+
|
|
85
|
+
function hasUseClient(file: string): Promise<boolean> {
|
|
86
|
+
let cached = isClientFile.get(file);
|
|
87
|
+
if (!cached) {
|
|
88
|
+
cached = readFile(file, 'utf8').then(
|
|
89
|
+
// A text check skips parsing the files that cannot carry it.
|
|
90
|
+
(code) => code.includes('use client') && detectDirective(code, file) === 'use client',
|
|
91
|
+
() => false
|
|
92
|
+
);
|
|
93
|
+
isClientFile.set(file, cached);
|
|
94
|
+
}
|
|
95
|
+
return cached;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
name: 'timber-client-in-server-package-deps',
|
|
100
|
+
apply: 'serve',
|
|
101
|
+
resolveId: {
|
|
102
|
+
order: 'pre',
|
|
103
|
+
async handler(source, importer, options) {
|
|
104
|
+
if (this.environment.name !== rscEnvironmentName || !importer) return;
|
|
105
|
+
if (BARE_IMPORT_RE.test(source) || source.startsWith('\0')) return;
|
|
106
|
+
// A package's own relative imports, or an absolute path into a
|
|
107
|
+
// package (what the barrel rewrite emits). An app module's relative
|
|
108
|
+
// imports stay in the app and are not resolved twice.
|
|
109
|
+
if (!importer.includes(NODE_MODULES) && !source.includes(NODE_MODULES)) return;
|
|
110
|
+
const resolved = await this.resolve(source, importer, { ...options, skipSelf: true });
|
|
111
|
+
if (!resolved || resolved.external) return resolved;
|
|
112
|
+
// Vite's `?v=<hash>` is dropped; any other query (`?raw`, `?url`)
|
|
113
|
+
// asks for something other than the module, so it is left alone.
|
|
114
|
+
const file = asFilePath(resolved.id);
|
|
115
|
+
const bare = file && packagePathOf(file);
|
|
116
|
+
if (!file || !bare || !(await hasUseClient(file))) return resolved;
|
|
117
|
+
// Resolving the bare specifier runs plugin-rsc's resolver for bare
|
|
118
|
+
// imports, which records it as the file's package source when it
|
|
119
|
+
// resolves from the app root to this same file. Otherwise it records
|
|
120
|
+
// nothing and the file keeps its raw proxy.
|
|
121
|
+
//
|
|
122
|
+
// The bare specifier is one this plugin made up, not one the package
|
|
123
|
+
// wrote, so it may not resolve at all: a package whose `exports` map
|
|
124
|
+
// hides the file still imports it by a relative path, and Vite throws
|
|
125
|
+
// ("is not exported under the conditions") instead of returning
|
|
126
|
+
// null. That only means the file has no package path; the relative
|
|
127
|
+
// import that already resolved stays.
|
|
128
|
+
let viaPackage: Awaited<ReturnType<typeof this.resolve>>;
|
|
129
|
+
try {
|
|
130
|
+
viaPackage = await this.resolve(bare, importer, { ...options, skipSelf: true });
|
|
131
|
+
} catch {
|
|
132
|
+
return resolved;
|
|
133
|
+
}
|
|
134
|
+
if (!viaPackage || asFilePath(viaPackage.id) !== file) return resolved;
|
|
135
|
+
return viaPackage;
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -338,7 +338,7 @@ function langForFile(file: string): Lang {
|
|
|
338
338
|
* the emitted path resolves to the same id the barrel's own import would, which
|
|
339
339
|
* keeps one module instance per file.
|
|
340
340
|
*/
|
|
341
|
-
function asFilePath(id: string): string | null {
|
|
341
|
+
export function asFilePath(id: string): string | null {
|
|
342
342
|
const path = id.replace(/\?v=[\w.-]+$/, '');
|
|
343
343
|
return isAbsolute(path) && !path.includes('?') && !path.includes('\0') ? path : null;
|
|
344
344
|
}
|
|
@@ -305,18 +305,22 @@ export class FlightInjectorCore {
|
|
|
305
305
|
// scripts (Node only — a Web source never has chunks to take).
|
|
306
306
|
this.enqueue(this.encoder.push([result.value, ...this.reader.takeBuffered()]));
|
|
307
307
|
|
|
308
|
-
//
|
|
309
|
-
//
|
|
310
|
-
//
|
|
308
|
+
// While HTML is still streaming, yield first so HTML chunks get
|
|
309
|
+
// priority. Once flush() fires (HTML_DONE) there is no HTML left to
|
|
310
|
+
// wait for.
|
|
311
311
|
if (!isHtmlDone(this.machine.state)) {
|
|
312
312
|
await new Promise<void>((r) => setImmediate(r));
|
|
313
|
-
// After yielding, drain. Without this, hydration blocks waiting
|
|
314
|
-
// for flight data stuck in pending[]. Safe because we're between
|
|
315
|
-
// event-loop ticks (no transform() call mid-execution → no
|
|
316
|
-
// mid-tag risk; the buffered transform upstream coalesced HTML
|
|
317
|
-
// chunks to coherent fragments).
|
|
318
|
-
if (this.pending.length > 0) onYieldDrain();
|
|
319
313
|
}
|
|
314
|
+
// Drain after every read, in both phases. Safe while streaming
|
|
315
|
+
// because we're between event-loop ticks (no transform() call
|
|
316
|
+
// mid-execution → no mid-tag risk; the buffered transform upstream
|
|
317
|
+
// coalesced HTML chunks to coherent fragments), and trivially safe
|
|
318
|
+
// after HTML_DONE. Draining only while streaming held every script
|
|
319
|
+
// read after Fizz finished until the whole Flight stream ended: a
|
|
320
|
+
// promise prop that settles late kept the shell's own rows — what
|
|
321
|
+
// hydration needs — off the wire until it resolved (design/05
|
|
322
|
+
// §"Promise Props").
|
|
323
|
+
if (this.pending.length > 0) onYieldDrain();
|
|
320
324
|
}
|
|
321
325
|
} catch (err) {
|
|
322
326
|
// Release the source on any failure (a timed-out read leaves it live);
|
|
@@ -504,7 +504,10 @@ const skippedFiles = new Map<string, string>();
|
|
|
504
504
|
* per request that joined it, so the report happens inside the body. A
|
|
505
505
|
* body still running when the singleflight times out is reported then,
|
|
506
506
|
* with the timeout error (the signal's reason), and its later rejection
|
|
507
|
-
* is
|
|
507
|
+
* is not reported again.
|
|
508
|
+
* - **Settles with the work's outcome.** The value or the error passes
|
|
509
|
+
* through, so an inline re-render that joined this flight receives the
|
|
510
|
+
* captured bytes, or the failure, instead of `undefined`.
|
|
508
511
|
* - **Against the request whose stale read started it**, read from the
|
|
509
512
|
* request context the work runs in. With no request (the build-time
|
|
510
513
|
* capture store) it is only logged.
|
|
@@ -513,11 +516,11 @@ const skippedFiles = new Map<string, string>();
|
|
|
513
516
|
* revalidated page tree included). The request's own abort does not
|
|
514
517
|
* suppress the report: the work runs after the response either way.
|
|
515
518
|
*/
|
|
516
|
-
export async function runBackgroundFlight(
|
|
519
|
+
export async function runBackgroundFlight<T>(
|
|
517
520
|
signal: AbortSignal,
|
|
518
|
-
work: () => Promise<
|
|
521
|
+
work: () => Promise<T>,
|
|
519
522
|
log: (error: unknown) => void
|
|
520
|
-
): Promise<
|
|
523
|
+
): Promise<T> {
|
|
521
524
|
const failed = (error: unknown): void => {
|
|
522
525
|
log(error);
|
|
523
526
|
const request = requestContextAls.getStore()?.request;
|
|
@@ -527,9 +530,10 @@ export async function runBackgroundFlight(
|
|
|
527
530
|
const settled = new AbortController();
|
|
528
531
|
onAbortUntil(signal, () => failed(signal.reason), settled.signal);
|
|
529
532
|
try {
|
|
530
|
-
await work();
|
|
533
|
+
return await work();
|
|
531
534
|
} catch (error) {
|
|
532
535
|
if (!signal.aborted) failed(error);
|
|
536
|
+
throw error;
|
|
533
537
|
} finally {
|
|
534
538
|
settled.abort();
|
|
535
539
|
}
|
package/src/server/pipeline.ts
CHANGED
|
@@ -39,6 +39,7 @@ import { outcomeToResponse } from './pipeline-outcome.ts';
|
|
|
39
39
|
import { canonicalize } from './canonicalize.ts';
|
|
40
40
|
import { enforceRscCacheKey, hardenRscPayloadResponse } from './rsc-cache-key-guard.ts';
|
|
41
41
|
import { stripHeadBody } from './head-response.ts';
|
|
42
|
+
import { dropUpstreamFraming } from './response-framing.ts';
|
|
42
43
|
import { RSC_KEY_HEADERS } from '../shared/rsc-cache-key.ts';
|
|
43
44
|
import { isDebug } from './debug.ts';
|
|
44
45
|
import type { ReactFormState } from 'react-dom/client';
|
|
@@ -536,6 +537,12 @@ export function createPipeline(config: PipelineConfig): (req: Request) => Promis
|
|
|
536
537
|
// covered, not just the success path (TIM-1268).
|
|
537
538
|
hardenRscPayloadResponse(result);
|
|
538
539
|
|
|
540
|
+
// A forwarded fetch() Response carries the upstream's framing
|
|
541
|
+
// (the encoded body's length, its connection headers) around
|
|
542
|
+
// a decoded body — applied at this single exit so every
|
|
543
|
+
// producer is covered (design/13-security.md #42).
|
|
544
|
+
dropUpstreamFraming(result);
|
|
545
|
+
|
|
539
546
|
// A HEAD response carries the headers its GET would produce and
|
|
540
547
|
// no content (RFC 9110 §9.3.2). Applied at this single exit so
|
|
541
548
|
// every response shape is covered — page render, API route,
|