Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vinext/src/client/vinext-next-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { isUnknownRecord } from "../utils/record.js";
export type VinextLinkPrefetchRoute = {
canPrefetchLoadingShell: boolean;
documentOnly?: boolean;
hasRuntimePrefetch?: boolean;
prefetchMode?: "inherited-runtime" | "runtime" | "static";
isDynamic: boolean;
patternParts: string[];
requiresDynamicNavigationRequest?: boolean;
Expand Down
292 changes: 292 additions & 0 deletions packages/vinext/src/entries/app-browser-entry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { resolveClientRuntimeModule, resolveRuntimeEntryModule } from "./runtime-entry-module.js";
import fs from "node:fs";
import path from "node:path";
import type {
VinextLinkPrefetchRoute,
VinextPagesLinkPrefetchRoute,
} from "../client/vinext-next-data.js";
import type { AppRoute } from "../routing/app-router.js";
import type { RouteManifest } from "../routing/app-route-graph.js";
import type { NextRewrite } from "../config/next-config.js";
import { escapeRegExp } from "../utils/regex.js";

/**
* Generate the virtual browser entry module.
Expand Down Expand Up @@ -71,14 +74,303 @@ function requiresDynamicNavigationRequest(route: AppRoute): boolean {

/** Project an `AppRoute` down to the public `VinextLinkPrefetchRoute` shape. */
export function toLinkPrefetchRoute(route: AppRoute): VinextLinkPrefetchRoute {
const prefetchMode = routePrefetchMode(route);
return {
canPrefetchLoadingShell: route.loadingPath !== null,
...(prefetchMode === "runtime" || prefetchMode === "inherited-runtime"
? { hasRuntimePrefetch: true }
: {}),
...(prefetchMode ? { prefetchMode } : {}),
patternParts: [...route.patternParts],
isDynamic: route.isDynamic,
...(requiresDynamicNavigationRequest(route) ? { requiresDynamicNavigationRequest: true } : {}),
};
}

type SegmentPrefetchMode = "runtime" | "static";

const segmentConfigExtensions = [".tsx", ".ts", ".jsx", ".js", ".mjs", ".mts", ".cjs", ".cts"];

function readFilePrefetchMode(filePath: string | null): SegmentPrefetchMode | null {
if (filePath === null) return null;

return readFileExportedPrefetchMode(filePath, "unstable_instant", new Set());
}

function readFileExportedPrefetchMode(
filePath: string,
exportName: string,
visited: Set<string>,
): SegmentPrefetchMode | null {
const cacheKey = `${path.resolve(filePath)}#${exportName}`;
if (visited.has(cacheKey)) return null;
visited.add(cacheKey);

let source: string;
try {
source = fs.readFileSync(filePath, "utf8");
} catch {
return null;
}

const directExportExpression = findConstInitializer(source, exportName, { exported: true });
if (directExportExpression) {
return readPrefetchModeExpression({
source,
expression: directExportExpression,
importerPath: filePath,
visited,
});
}

if (exportName === "default") {
const defaultExportExpression = findDefaultExportExpression(source);
if (defaultExportExpression) {
return readPrefetchModeExpression({
source,
expression: defaultExportExpression,
importerPath: filePath,
visited,
});
}
}

const exportRef = findNamedExportReference(source, exportName);
if (!exportRef) return null;
if (exportRef.source) {
const resolved = resolveRelativeImportPath(filePath, exportRef.source);
return resolved ? readFileExportedPrefetchMode(resolved, exportRef.localName, visited) : null;
}

return readPrefetchModeExpression({
source,
expression: exportRef.localName,
importerPath: filePath,
visited,
});
}

function readPrefetchModeExpression(input: {
expression: string;
importerPath: string;
source: string;
visited: Set<string>;
}): SegmentPrefetchMode | null {
const expression = unwrapExpression(input.expression.trim());
const objectMode = readObjectExpressionPrefetchMode(expression);
if (objectMode) return objectMode;
if (!isIdentifier(expression)) return null;

const localInitializer = findConstInitializer(input.source, expression, { exported: false });
if (localInitializer) {
return readPrefetchModeExpression({
...input,
expression: localInitializer,
});
}

const importRef = findImportedBinding(input.source, expression);
if (!importRef) return null;
const resolved = resolveRelativeImportPath(input.importerPath, importRef.source);
if (!resolved) return null;
return readFileExportedPrefetchMode(resolved, importRef.importedName, input.visited);
}

function readObjectExpressionPrefetchMode(expression: string): SegmentPrefetchMode | null {
if (!expression.startsWith("{")) return null;
const match = /(?:^|[{,])\s*(?:prefetch|["']prefetch["'])\s*:\s*(["'])(runtime|static)\1/.exec(
expression,
);
return match?.[2] === "runtime" || match?.[2] === "static" ? match[2] : null;
}

function findConstInitializer(
source: string,
name: string,
options: { exported: boolean },
): string | null {
const prefix = options.exported ? String.raw`export\s+const` : String.raw`const`;
const match = new RegExp(`${prefix}\\s+${escapeRegExp(name)}\\b`, "m").exec(source);
if (!match) return null;
const initializerStart = readConstInitializerStart(source, match.index + match[0].length);
if (initializerStart === null) return null;
return readInitializerExpression(source, initializerStart);
}

function readConstInitializerStart(source: string, startIndex: number): number | null {
let index = skipWhitespace(source, startIndex);
if (source[index] === "=") return index + 1;
if (source[index] !== ":") return null;

index++;
let quote: string | null = null;
let escaped = false;
let braceDepth = 0;
let bracketDepth = 0;
let parenDepth = 0;
let angleDepth = 0;
for (; index < source.length; index++) {
const char = source[index];
if (quote) {
if (escaped) {
escaped = false;
} else if (char === "\\") {
escaped = true;
} else if (char === quote) {
quote = null;
}
continue;
}
if (char === '"' || char === "'" || char === "`") {
quote = char;
continue;
}
if (char === "{") braceDepth++;
if (char === "}") braceDepth--;
if (char === "[") bracketDepth++;
if (char === "]") bracketDepth--;
if (char === "(") parenDepth++;
if (char === ")") parenDepth--;
if (char === "<") angleDepth++;
if (char === ">" && angleDepth > 0) angleDepth--;
const inNestedType = braceDepth > 0 || bracketDepth > 0 || parenDepth > 0 || angleDepth > 0;
if (char === ";" && !inNestedType) return null;
if (char === "=" && !inNestedType && source[index + 1] !== ">") return index + 1;
}
return null;
}

function skipWhitespace(source: string, startIndex: number): number {
let index = startIndex;
while (/\s/.test(source[index] ?? "")) index++;
return index;
}

function findDefaultExportExpression(source: string): string | null {
const match = /export\s+default\s+/.exec(source);
if (!match) return null;
return readInitializerExpression(source, match.index + match[0].length);
}

function findNamedExportReference(
source: string,
exportName: string,
): { localName: string; source: string | null } | null {
const exportPattern = /export\s+{([^}]+)}(?:\s+from\s+(["'])([^"']+)\2)?/g;
for (const match of source.matchAll(exportPattern)) {
const exportSource = match[3] ?? null;
for (const specifier of match[1].split(",")) {
const ref = parseImportExportSpecifier(specifier);
if (ref.exportedName === exportName) {
return { localName: ref.localName, source: exportSource };
}
}
}
return null;
}

function findImportedBinding(
source: string,
localName: string,
): { importedName: string; source: string } | null {
const namedImportPattern = /import\s+{([^}]+)}\s+from\s+(["'])([^"']+)\2/g;
for (const match of source.matchAll(namedImportPattern)) {
for (const specifier of match[1].split(",")) {
const ref = parseImportExportSpecifier(specifier);
if (ref.exportedName === localName) {
return { importedName: ref.localName, source: match[3] };
}
}
}

const defaultImportPattern = /import\s+([A-Za-z_$][\w$]*)\s*(?:,|\s+from\s+(["'])([^"']+)\2)/g;
for (const match of source.matchAll(defaultImportPattern)) {
if (match[1] === localName && match[3]) {
return { importedName: "default", source: match[3] };
}
}

return null;
}

function parseImportExportSpecifier(specifier: string): {
exportedName: string;
localName: string;
} {
const [localName, exportedName] = specifier
.trim()
.split(/\s+as\s+/)
.map((part) => part.trim());
return { exportedName: exportedName ?? localName, localName };
}

function readInitializerExpression(source: string, startIndex: number): string | null {
let quote: string | null = null;
let escaped = false;
let braceDepth = 0;
let bracketDepth = 0;
let parenDepth = 0;
for (let index = startIndex; index < source.length; index++) {
const char = source[index];
if (quote) {
if (escaped) {
escaped = false;
} else if (char === "\\") {
escaped = true;
} else if (char === quote) {
quote = null;
}
continue;
}
if (char === '"' || char === "'" || char === "`") {
quote = char;
continue;
}
if (char === "{") braceDepth++;
if (char === "}") braceDepth--;
if (char === "[") bracketDepth++;
if (char === "]") bracketDepth--;
if (char === "(") parenDepth++;
if (char === ")") parenDepth--;
if (char === ";" && braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) {
return source.slice(startIndex, index).trim();
}
}
return source.slice(startIndex).trim() || null;
}

function unwrapExpression(expression: string): string {
let current = expression;
while (current.startsWith("(") && current.endsWith(")")) {
current = current.slice(1, -1).trim();
}
return current;
}

function isIdentifier(value: string): boolean {
return /^[A-Za-z_$][\w$]*$/.test(value);
}

function resolveRelativeImportPath(importerPath: string, specifier: string): string | null {
if (!specifier.startsWith(".")) return null;
const basePath = path.resolve(path.dirname(importerPath), specifier);
const candidates = path.extname(basePath)
? [basePath]
: [
...segmentConfigExtensions.map((extension) => `${basePath}${extension}`),
...segmentConfigExtensions.map((extension) => path.join(basePath, `index${extension}`)),
];
return candidates.find((candidate) => fs.existsSync(candidate)) ?? null;
}

function routePrefetchMode(route: AppRoute): "inherited-runtime" | SegmentPrefetchMode | null {
const pagePrefetchMode = readFilePrefetchMode(route.pagePath);
if (pagePrefetchMode) return pagePrefetchMode;
return route.layouts.some((layoutPath) => readFilePrefetchMode(layoutPath) === "runtime")
? "inherited-runtime"
: null;
}

function buildRouteManifestExpression(routeManifest: RouteManifest | null): string {
if (routeManifest === null) return "null";

Expand Down
2 changes: 2 additions & 0 deletions packages/vinext/src/entries/app-rsc-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ export default createAppRscHandler({
middlewareContext,
mountedSlotsHeader,
params,
retainedPrefetchLayoutIds,
pprFallbackCacheShells,
pprFallbackShell,
renderedConcreteUrlPaths,
Expand Down Expand Up @@ -859,6 +860,7 @@ export default createAppRscHandler({
pprFallbackShell,
pprRuntime: ${cacheComponents ? "__appPagePprRuntime" : "undefined"},
renderedConcreteUrlPaths,
retainedPrefetchLayoutIds,
skipStaticParamsValidation,
staticParamsValidationParams,
rootParams,
Expand Down
Loading
Loading