-
Notifications
You must be signed in to change notification settings - Fork 304
feat: static/dynamic layout detection for skip-header optimization #767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
james-elicx
merged 11 commits into
cloudflare:main
from
NathanDrake2406:feat/layout-persistence-pr-6-static-dynamic
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
50fa22f
feat: add classifyLayoutSegmentConfig for layout segment config detec…
NathanDrake2406 1b85117
feat: add module graph layout classification (Layer 2)
NathanDrake2406 8604239
feat: per-layout dynamic detection in probe phase (Layer 3)
NathanDrake2406 6d99a42
feat: add __layoutFlags payload metadata and thread through router state
NathanDrake2406 bfbbd59
refactor: group classification options into single LayoutClassificati…
NathanDrake2406 4392f14
fix: default to dynamic flag when layout probe throws non-special error
NathanDrake2406 bcdc760
refactor: replace parallel arrays with LayoutEntry struct, tighten names
claude 8e09f70
style: fix prettier formatting
claude 1faf2cd
Revert "style: fix prettier formatting"
claude 5e7cf12
Reapply "style: fix prettier formatting"
claude 0a192d6
style: apply oxfmt formatting
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * Layout classification — determines whether each layout in an App Router | ||
| * route tree is static or dynamic via two complementary detection layers: | ||
| * | ||
| * Layer 1: Segment config (`export const dynamic`, `export const revalidate`) | ||
| * Layer 2: Module graph traversal (checks for transitive dynamic shim imports) | ||
| * | ||
| * Layer 3 (probe-based runtime detection) is handled separately in | ||
| * `app-page-execution.ts` at request time. | ||
| */ | ||
|
|
||
| import { classifyLayoutSegmentConfig } from "./report.js"; | ||
| import { createAppPageTreePath } from "../server/app-page-route-wiring.js"; | ||
|
|
||
| export type ModuleGraphClassification = "static" | "needs-probe"; | ||
| export type LayoutClassificationResult = "static" | "dynamic" | "needs-probe"; | ||
|
|
||
| export type ModuleInfoProvider = { | ||
| getModuleInfo(id: string): { | ||
| importedIds: string[]; | ||
| dynamicImportedIds: string[]; | ||
| } | null; | ||
| }; | ||
|
|
||
| type LayoutEntry = { | ||
| /** Rollup/Vite module ID for the layout file. */ | ||
| moduleId: string; | ||
| /** Directory depth from the app root, used to build the stable layout ID. */ | ||
| treePosition: number; | ||
| /** Segment config source code extracted at build time, or null when absent. */ | ||
| segmentConfig?: { code: string } | null; | ||
| }; | ||
|
|
||
| type RouteForClassification = { | ||
| layouts: readonly LayoutEntry[]; | ||
| routeSegments: string[]; | ||
| }; | ||
|
|
||
| /** | ||
| * BFS traversal of a layout's dependency tree. If any transitive import | ||
| * resolves to a dynamic shim path (headers, cache, server), the layout | ||
| * cannot be proven static at build time and needs a runtime probe. | ||
| */ | ||
| export function classifyLayoutByModuleGraph( | ||
| layoutModuleId: string, | ||
| dynamicShimPaths: ReadonlySet<string>, | ||
| moduleInfo: ModuleInfoProvider, | ||
| ): ModuleGraphClassification { | ||
| const visited = new Set<string>(); | ||
| const queue: string[] = [layoutModuleId]; | ||
| let head = 0; | ||
|
|
||
| while (head < queue.length) { | ||
| const currentId = queue[head++]!; | ||
|
|
||
| if (visited.has(currentId)) continue; | ||
| visited.add(currentId); | ||
|
|
||
| if (dynamicShimPaths.has(currentId)) return "needs-probe"; | ||
|
|
||
| const info = moduleInfo.getModuleInfo(currentId); | ||
| if (!info) continue; | ||
|
|
||
| for (const importedId of info.importedIds) { | ||
| if (!visited.has(importedId)) queue.push(importedId); | ||
| } | ||
| for (const dynamicId of info.dynamicImportedIds) { | ||
| if (!visited.has(dynamicId)) queue.push(dynamicId); | ||
| } | ||
| } | ||
|
|
||
| return "static"; | ||
| } | ||
|
|
||
| /** | ||
| * Classifies all layouts across all routes using a two-layer strategy: | ||
| * | ||
| * 1. Segment config (Layer 1) — short-circuits to "static" or "dynamic" | ||
| * 2. Module graph (Layer 2) — BFS for dynamic shim imports → "static" or "needs-probe" | ||
| * | ||
| * Shared layouts (same file appearing in multiple routes) are classified once | ||
| * and deduplicated by layout ID. | ||
| */ | ||
| export function classifyAllRouteLayouts( | ||
| routes: readonly RouteForClassification[], | ||
| dynamicShimPaths: ReadonlySet<string>, | ||
| moduleInfo: ModuleInfoProvider, | ||
| ): Map<string, LayoutClassificationResult> { | ||
| const result = new Map<string, LayoutClassificationResult>(); | ||
|
|
||
| for (const route of routes) { | ||
| for (const layout of route.layouts) { | ||
| const layoutId = `layout:${createAppPageTreePath(route.routeSegments, layout.treePosition)}`; | ||
|
|
||
| if (result.has(layoutId)) continue; | ||
|
|
||
| // Layer 1: segment config | ||
| if (layout.segmentConfig) { | ||
| const configResult = classifyLayoutSegmentConfig(layout.segmentConfig.code); | ||
| if (configResult !== null) { | ||
| result.set(layoutId, configResult); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Layer 2: module graph | ||
| result.set( | ||
| layoutId, | ||
| classifyLayoutByModuleGraph(layout.moduleId, dynamicShimPaths, moduleInfo), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor thought: the spread merge
{ ...state.layoutFlags, ...action.layoutFlags }means stale flags for layouts no longer in the current tree persist indefinitely (until a replace/hard navigation). This is harmless for the skip-header optimization (the consumer in PR 768 will only read flags for layouts actually in the current tree), but the state object grows monotonically across soft navigations. Not a practical concern for typical apps, but worth keeping in mind if layout flags ever carry more data.