Skip to content

Commit d4e7bc5

Browse files
test(build): gate query.ts out of a router-only app
The action gate now bundles a third app — createRouter and useNavigate, no query, no action — through both resolution paths and fails if it carries query.ts (its declarations; its one distinctive literal is server-only). The query app must still carry them, so the markers can't silently move. Co-authored-by: Cursor <[email protected]>
1 parent 3451fd7 commit d4e7bc5

2 files changed

Lines changed: 72 additions & 35 deletions

File tree

‎.changeset/flat-build-action-split.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
Router apps that never import `action` no longer bundle the action layer through the flat build (#655). The flat `dist/index.js` — what Rolldown, esbuild and webpack resolve without the `solid` condition — was built with `inlineDynamicImports`, which turned the server-function form fallback's lazy import into a static reference to all of `action.ts`: the form-submit handler, submission and `aria-busy` state, the single-flight consumer, and signals' `action`. The flat build now emits `action.ts` and `query.ts` as their own modules (`dist/index-action.js`, `dist/index-query.js`, with the rest in `dist/index-core.js` and `dist/index.js` as the re-exporting entry), so a bundler keeps the fallback's target as a lazy chunk exactly as it does for the `solid` condition's per-module output.
66

7-
No behavior change. The action layer still installs itself into the router's seams on the first `action()` (form handler, form claims, flight consumer, flash decoder); a submit before that leaves client-only action urls to the browser and runs server-function forms through the lazily loaded action module. Measured with solidjs/solid's size harness (Rolldown, brotli): the router standalone drops from 32,480 B to 27,456 B minified (11,232 → 9,653 B br) for an app without `action`; the `page: base + router` scenario drops 8,372 B minified (2,565 B br). Apps that import `action` pay ~230 B minified (≤ 76 B br) for the module boundaries. `pnpm build` now runs `scripts/check-action-gate.mjs`, which bundles a router + `query` app with and without `action` through both resolution paths and fails if the action layer reaches the read-only app's eager graph.
7+
No behavior change. The action layer still installs itself into the router's seams on the first `action()` (form handler, form claims, flight consumer, flash decoder); a submit before that leaves client-only action urls to the browser and runs server-function forms through the lazily loaded action module. Measured with solidjs/solid's size harness (Rolldown, brotli): the router standalone drops from 32,480 B to 27,456 B minified (11,232 → 9,653 B br) for an app without `action`; the `page: base + router` scenario drops 8,372 B minified (2,565 B br). Apps that import `action` pay ~230 B minified (≤ 76 B br) for the module boundaries. `pnpm build` now runs `scripts/check-action-gate.mjs`, which bundles a router-only app, a router + `query` app, and the same with `action` through both resolution paths, and fails if the action layer reaches an app without `action` or query.ts reaches the router-only app.

‎scripts/check-action-gate.mjs‎

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
// only the seams the action module fills on first `action()` (events.ts's
55
// form handler, claims.ts's form slot, routing.ts's flight consumer and flash
66
// decoder) plus the lazy fallback for server-function forms, whose target is
7-
// a split point. Two apps are bundled against the built `dist/` through both
8-
// resolution paths a consumer can take: the flat build (no `solid`
7+
// a split point. Likewise an app that never imports `query` must not bundle
8+
// query.ts (the fallback's other dependency). Three apps — router only,
9+
// + `query`, + `query` and `action` — are bundled against the built `dist/`
10+
// through both resolution paths a consumer can take: the flat build (no `solid`
911
// condition: Rolldown, esbuild and webpack defaults) and the `solid`
1012
// condition compiled by @solidjs/vite-plugin. solid-js and @solidjs/web are
1113
// bundled in, so what the action layer retains from them is visible too.
1214
//
15+
// query.ts has no marker literal: its one distinctive string is server-only
16+
// and folds out of these browser builds, so its declarations identify it.
17+
//
1318
// `isPending`/`latest` are not markers: the router core reads them itself
1419
// (`isRouting`, navigation intent), and through them signals' optimistic
1520
// engine is retained by every router app.
@@ -22,25 +27,47 @@ import { build } from "vite";
2227
import solid from "@solidjs/vite-plugin";
2328

2429
const dist = fileURLToPath(new URL("../dist/", import.meta.url));
25-
// literals that survive minification
26-
const LITERALS = [
27-
"Only POST forms are supported for Actions",
28-
"Client Actions need explicit names if server rendered",
29-
"routerActionSubmitHooks"
30-
];
31-
// declarations, read from an unminified build (comments can mention the
32-
// names, never declare them)
33-
const DECLARATIONS = [
34-
"handleFormAction",
35-
"markFormBusy",
36-
"toAction",
37-
"settleActionResult",
38-
"setupFlightDataConsumer",
39-
"installRouterIntegrations",
40-
// @solidjs/signals' action and its transition bookkeeping
41-
"restoreTransition"
42-
];
30+
// Each layer's markers: literals that survive minification, and declarations
31+
// read from an unminified build (comments can mention the names, never
32+
// declare them). Every marker appears in that layer's module only.
33+
const LAYERS = {
34+
action: {
35+
literals: [
36+
"Only POST forms are supported for Actions",
37+
"Client Actions need explicit names if server rendered",
38+
"routerActionSubmitHooks"
39+
],
40+
declarations: [
41+
"handleFormAction",
42+
"markFormBusy",
43+
"toAction",
44+
"settleActionResult",
45+
"setupFlightDataConsumer",
46+
"installRouterIntegrations",
47+
// @solidjs/signals' action and its transition bookkeeping
48+
"restoreTransition"
49+
]
50+
},
51+
// query.ts: the cache and its key operations. The core never imports it
52+
// (liveQuery.ts does), so a router app without `query` carries none of it.
53+
query: {
54+
literals: [],
55+
declarations: ["query", "getCache", "cacheKeyOp", "revalidate"]
56+
}
57+
};
4358

59+
const routerOnly = entry => `import { createRouter, useNavigate } from "${dist}${entry}";
60+
function Home() {
61+
const navigate = useNavigate();
62+
return () => navigate("/item/1");
63+
}
64+
export const Router = createRouter({
65+
routes: [
66+
{ path: "/", component: Home },
67+
{ path: "/item/:id", component: () => null }
68+
]
69+
});
70+
`;
4471
const app = entry => `import { createRouter, useNavigate, query } from "${dist}${entry}";
4572
const getItem = query(async id => ({ id }), "item");
4673
function Home() {
@@ -100,9 +127,19 @@ async function bundle(input, conditions, plugins, minify) {
100127
return [...eager].map(c => c.code).join("\n");
101128
}
102129

103-
const found = (min, plain) => [
104-
...LITERALS.filter(s => min.includes(s)),
105-
...DECLARATIONS.filter(s => plain.includes(`function ${s}(`))
130+
/** The layer's markers present in a bundle. */
131+
const found = (layer, min, plain) => [
132+
...LAYERS[layer].literals.filter(m => min.includes(m)),
133+
...LAYERS[layer].declarations.filter(m => plain.includes(`function ${m}(`))
134+
];
135+
const markers = layer => [...LAYERS[layer].literals, ...LAYERS[layer].declarations];
136+
137+
// Per app: the layers it must not carry, and the layers it must carry in full
138+
// (the latter proves the markers still identify the layer).
139+
const apps = [
140+
{ variant: "router-only", source: routerOnly, absent: ["action", "query"], present: [] },
141+
{ variant: "read-only", source: app, absent: ["action"], present: ["query"] },
142+
{ variant: "+action", source: withAction, absent: [], present: ["action"] }
106143
];
107144

108145
const paths = {
@@ -118,25 +155,25 @@ try {
118155
const failures = [];
119156
for (const [name, { entry, conditions, plugins }] of Object.entries(paths)) {
120157
const sizes = [];
121-
for (const [variant, source] of [
122-
["read-only", app(entry)],
123-
["+action", withAction(entry)]
124-
]) {
158+
for (const { variant, source, absent, present } of apps) {
125159
const input = join(root, `${name}-${variant.replace(/\W/g, "")}.jsx`);
126-
writeFileSync(input, source);
160+
writeFileSync(input, source(entry));
127161
const [min, plain] = await Promise.all([
128162
bundle(input, conditions, plugins, true),
129163
bundle(input, conditions, plugins, false)
130164
]);
131-
const hits = found(min, plain);
132165
sizes.push(`${variant} ${min.length} B min / ${brotli(min)} B br`);
133-
if (variant === "read-only" && hits.length)
134-
failures.push(`${name} read-only bundle carries the action layer: ${hits.join(", ")}`);
135-
if (variant === "+action") {
136-
const missing = [...LITERALS, ...DECLARATIONS].filter(m => !hits.includes(m));
166+
for (const layer of absent) {
167+
const hits = found(layer, min, plain);
168+
if (hits.length)
169+
failures.push(`${name} ${variant} bundle carries the ${layer} layer: ${hits.join(", ")}`);
170+
}
171+
for (const layer of present) {
172+
const hits = found(layer, min, plain);
173+
const missing = markers(layer).filter(m => !hits.includes(m));
137174
if (missing.length)
138175
failures.push(
139-
`${name} +action bundle lacks ${missing.join(", ")} — markers moved, update scripts/check-action-gate.mjs`
176+
`${name} ${variant} bundle lacks ${layer} markers ${missing.join(", ")} — markers moved, update scripts/check-action-gate.mjs`
140177
);
141178
}
142179
}

0 commit comments

Comments
 (0)