Skip to content

Commit 3e9a726

Browse files
committed
fix(router): #582 reject "." in slot names + document prerender tag-union as by-design
loaderParentId (segment-system.tsx) strips a loader segment id from the first `D<index>.` separator to recover its parent. That is only correct while slot names contain no ".": a parallel/intercept slot named "@D3.foo" embeds as `${shortCode}[email protected]`, so the regex mis-cuts at the slot's own "D3." and the loader's data is silently dropped (a bare "D" like "@detail" is fine — no trailing dot). Add assertValidSlotName in the parallel()/intercept() DSL helpers to reject a "." at definition time with a clear error, turning a silent tree-corruption into a loud failure, and note the invariant loaderParentId relies on. Also document (prerender-api-design.md) that the prerender serve path records no cache({ tags }) into the document tag union on a hit — so a pre-rendered route's document-cache entry is not reached by updateTag/revalidateTag. This is intentional, not a gap: the baked payload is immutable until rebuild, so invalidating the wrapper would only re-serve the identical body. Closes the remaining #582 cross-seam item as by-design rather than implementing a no-op tag-capture across the prerender artifact format. Userland coverage through the public urls()/parallel()/intercept() DSL: a dotted parallel slot and a dotted intercept slot both throw; a "@D3foo"/"@detail" name (D+digits, no dot) is accepted (guard non-vacuity).
1 parent cdddc25 commit 3e9a726

4 files changed

Lines changed: 90 additions & 1 deletion

File tree

‎packages/rangojs-router/docs/prerender-api-design.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,15 @@ entry). To make a tag-invalidatable route, serve it from the runtime cache (a
391391
`Passthrough()` route, or a non-prerendered route with `cache()` / `cacheTag`)
392392
rather than pre-rendering it.
393393

394+
By the same rule the prerender serve path (`yieldFromStore`) records no
395+
`cache({ tags })` into the request-scoped document tag union (`ctx._requestTags`)
396+
on a hit — so if a pre-rendered route is also wrapped in document caching, its
397+
document-cache entry carries an empty tag union and is not reached by
398+
`updateTag` / `revalidateTag`. This is intentional, not a gap: the baked payload
399+
is immutable until the next build, so invalidating the document-cache wrapper
400+
would only re-serve the identical prerendered body. Tag-invalidate the document
401+
by serving the route from the runtime cache instead of pre-rendering it.
402+
394403
### Middleware
395404

396405
Skipped during pre-rendering (no request object). Middleware runs at request

‎packages/rangojs-router/src/__tests__/urls.test.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,4 +1042,62 @@ describe("urls()", () => {
10421042
).toThrow(/middleware\(\[fn1, fn2/);
10431043
});
10441044
});
1045+
1046+
describe("slot name validation", () => {
1047+
// urls() builds lazily; the DSL helpers (and this guard) run when
1048+
// urlPatterns.handler() executes inside a RangoContext, like the
1049+
// middleware-form tests above.
1050+
let manifest: Map<string, EntryData>;
1051+
let patterns: Map<string, string>;
1052+
1053+
beforeEach(() => {
1054+
manifest = new Map();
1055+
patterns = new Map();
1056+
});
1057+
1058+
const build = (urlPatterns: { handler: () => unknown }) =>
1059+
RangoContext.run(
1060+
{ manifest, patterns, namespace: "test", parent: null, counters: {} },
1061+
() => urlPatterns.handler(),
1062+
);
1063+
1064+
it("rejects a parallel slot name containing '.' (the segment-id separator)", () => {
1065+
// "@D3.foo" would mis-cut loaderParentId (strips at the first `D<index>.`),
1066+
// silently dropping the loader's data. Reject it at definition time.
1067+
const urlPatterns = urls(({ layout, parallel }) => [
1068+
layout(
1069+
() => <div>Layout</div>,
1070+
() => [parallel({ "@D3.foo": () => <div /> })],
1071+
),
1072+
]);
1073+
expect(() => build(urlPatterns)).toThrow(/must not contain "\."/);
1074+
});
1075+
1076+
it("rejects an intercept slot name containing '.'", () => {
1077+
const urlPatterns = urls(({ layout, intercept }) => [
1078+
layout(
1079+
() => <div>Layout</div>,
1080+
() => [intercept("@D3.foo", ".x", () => <div />)],
1081+
),
1082+
]);
1083+
expect(() => build(urlPatterns)).toThrow(/must not contain "\."/);
1084+
});
1085+
1086+
it("accepts a slot name with a bare 'D' and digits but no '.' (non-vacuity)", () => {
1087+
// "@D3foo"/"@Detail" match no `D<digits>.` (no dot), so loaderParentId
1088+
// strips correctly and the guard must NOT reject them.
1089+
const urlPatterns = urls(({ layout, parallel }) => [
1090+
layout(
1091+
() => <div>Layout</div>,
1092+
() => [
1093+
parallel({
1094+
"@D3foo": () => <div />,
1095+
"@Detail": () => <div />,
1096+
}),
1097+
],
1098+
),
1099+
]);
1100+
expect(() => build(urlPatterns)).not.toThrow();
1101+
});
1102+
});
10451103
});

‎packages/rangojs-router/src/route-definition/dsl-helpers.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ const middleware: RouteHelpers<any, any>["middleware"] = (...args: any[]) => {
524524
} as MiddlewareItem;
525525
};
526526

527+
// Slot names become part of segment ids: a parallel/intercept slot is encoded
528+
// as `${shortCode}.${slotName}`, and loader segments append `D${index}.${loaderId}`.
529+
// A "." in the slot name collides with that separator -- loaderParentId
530+
// (segment-system.tsx) strips from the FIRST `D<index>.`, so a name like
531+
// "@D3.foo" is mis-cut to "@" and the loader's data is silently dropped. Reject
532+
// the dot at definition time so the failure is loud, not a corrupted tree at
533+
// runtime. (A bare "D" without a trailing dot -- e.g. "@Detail" -- is fine.)
534+
function assertValidSlotName(slotName: string): void {
535+
invariant(
536+
!slotName.includes("."),
537+
`Slot name "${slotName}" must not contain ".". The dot is a reserved ` +
538+
`segment-id separator; a name like "@D3.foo" corrupts loader segment-id ` +
539+
`parsing and silently drops the loader's data. Rename the slot.`,
540+
);
541+
}
542+
527543
const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
528544
const { store, ctx } = requireDslContext(
529545
"parallel() must be called inside urls()",
@@ -539,6 +555,7 @@ const parallel: RouteHelpers<any, any>["parallel"] = (slots, use) => {
539555
);
540556

541557
const slotNames = Object.keys(slots as Record<string, any>) as `@${string}`[];
558+
for (const slotName of slotNames) assertValidSlotName(slotName);
542559

543560
const namespace = `${ctx.namespace}.$${store.getNextIndex("parallel")}`;
544561

@@ -698,6 +715,8 @@ const intercept = (
698715
"intercept() cannot be used inside parallel()",
699716
);
700717

718+
assertValidSlotName(slotName);
719+
701720
const namespace = `${ctx.namespace}.$${store.getNextIndex("intercept")}.${slotName}`;
702721

703722
// Dot-prefixed = local (add include prefix), unprefixed = global (use as-is)

‎packages/rangojs-router/src/segment-system.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,10 @@ export async function renderSegments(
504504
// slot name is user-controlled (`@${string}`) and may contain an uppercase "D"
505505
// (e.g. "@Detail"). Strip from the first `D<index>.` separator so the slot name
506506
// is preserved; splitting on a bare "D" mis-cut "@Detail" to "@" and silently
507-
// dropped the loader's data.
507+
// dropped the loader's data. The first-`D<index>.` strip is only correct because
508+
// slot names cannot contain "." -- assertValidSlotName (route-definition/
509+
// dsl-helpers.ts) rejects a "." at definition time, so a name like "@D3.foo"
510+
// (which WOULD mis-cut here) can never reach this function.
508511
function loaderParentId(loaderSegmentId: string): string {
509512
return loaderSegmentId.replace(/D\d+\..*$/, "");
510513
}

0 commit comments

Comments
 (0)