diff --git a/packages/router-generator/src/generator.ts b/packages/router-generator/src/generator.ts index 3a08ebc3db..357334ce52 100644 --- a/packages/router-generator/src/generator.ts +++ b/packages/router-generator/src/generator.ts @@ -952,6 +952,12 @@ export class Generator { } const value: GetRoutesByFileMapResultValue = { routeId: node.routePath ?? `/${rootPathId}`, + kind: + node === this.scan!.rootRouteNode + ? 'root' + : node._fsRouteType === 'lazy' + ? 'lazy' + : 'file', routerConstructor: state.facts.routerConstructor, } map.set(node.fullPath, value) diff --git a/packages/router-generator/src/types.ts b/packages/router-generator/src/types.ts index cdd9bbe474..0f24da776c 100644 --- a/packages/router-generator/src/types.ts +++ b/packages/router-generator/src/types.ts @@ -132,6 +132,8 @@ export interface TreeModel { export type GetRoutesByFileMapResultValue = { routeId: string + /** The file's kind in the tree: the root route, a route file, or a `.lazy` route file. */ + kind: 'root' | 'file' | 'lazy' /** * The file's `Route` is the router package's `createFileRoute` / * `createLazyFileRoute` call, found by binding (not the app's own diff --git a/packages/router-plugin/src/core/router-plugin-context.ts b/packages/router-plugin/src/core/router-plugin-context.ts index 0d2208a441..62413af03e 100644 --- a/packages/router-plugin/src/core/router-plugin-context.ts +++ b/packages/router-plugin/src/core/router-plugin-context.ts @@ -85,6 +85,7 @@ export function changedRoutePaths( const before = previous.get(path) if ( before?.routeId !== entry.routeId || + before.kind !== entry.kind || before.routerConstructor !== entry.routerConstructor ) { out.push(path) diff --git a/packages/router-plugin/src/planners/registry.ts b/packages/router-plugin/src/planners/registry.ts index 579432de3a..776c159364 100644 --- a/packages/router-plugin/src/planners/registry.ts +++ b/packages/router-plugin/src/planners/registry.ts @@ -18,7 +18,11 @@ export class RouteRegistryView implements RouteRegistry { lookup(path: string): RouteEntryFacts | null { const entry = this.context.routesByFile.get(normalizePath(path)) return entry - ? { routeId: entry.routeId, routerConstructor: entry.routerConstructor } + ? { + routeId: entry.routeId, + kind: entry.kind, + routerConstructor: entry.routerConstructor, + } : null } diff --git a/packages/router-plugin/src/planners/route.ts b/packages/router-plugin/src/planners/route.ts index 868647aa9d..4e0c4dddef 100644 --- a/packages/router-plugin/src/planners/route.ts +++ b/packages/router-plugin/src/planners/route.ts @@ -105,6 +105,8 @@ type RouteRecognition = /** The router planner's phase A result: the route registry entry. */ export interface RouteEntryFacts { routeId: string + /** The file's kind in the route tree (`GetRoutesByFileMapResultValue.kind`). */ + kind: RouteKind /** `Route` is the router package's constructor call (`GetRoutesByFileMapResultValue.routerConstructor`; absent: not vouched for). */ routerConstructor?: boolean } @@ -735,10 +737,10 @@ export function routerRouteId(routeId: string): string { return routeId === '/__root' ? '__root__' : routeId } -/** The route-registry dependency fingerprint of a file: its route id and whether the router's constructor made its Route. */ +/** The route-registry dependency fingerprint of a file: its route id, its kind and whether the router's constructor made its Route. */ export function routeEntryDigest(entry: RouteEntryFacts | null): string | null { return entry - ? `${entry.routeId}\u0000${entry.routerConstructor ? 'router' : 'foreign'}` + ? `${entry.routeId}\u0000${entry.kind}\u0000${entry.routerConstructor ? 'router' : 'foreign'}` : null } diff --git a/packages/router-plugin/tests/registry-entry.test.ts b/packages/router-plugin/tests/registry-entry.test.ts index 914f8e5373..71badafb46 100644 --- a/packages/router-plugin/tests/registry-entry.test.ts +++ b/packages/router-plugin/tests/registry-entry.test.ts @@ -1,8 +1,9 @@ /** * The route registry as the compilers read it: an entry's digest and the - * change detection cover the `routerConstructor` fact as well as the id, so - * a plan that answered an import from the registry is re-planned when the - * generator reports the file's Route is no longer the router's own. + * change detection cover the `routerConstructor` fact and the file's kind as + * well as the id, so a plan that answered an import from the registry, or + * planned the route of a kind, is re-planned when the generator reports the + * file's Route is no longer the router's own or the file another kind. */ import { expect, test } from 'vitest' import { changedRoutePaths, createRouterPluginContext } from '../src/context' @@ -10,8 +11,17 @@ import { RouteRegistryView } from '../src/planners' import type { GetRoutesByFileMapResult } from '@tanstack/router-generator' const registry = ( - entries: Record, -): GetRoutesByFileMapResult => new Map(Object.entries(entries)) + entries: Record< + string, + { routeId: string; routerConstructor: boolean; kind?: 'root' | 'lazy' } + >, +): GetRoutesByFileMapResult => + new Map( + Object.entries(entries).map(([path, entry]) => [ + path, + { kind: 'file', ...entry }, + ]), + ) test('a change of routerConstructor is a registry change of that path, with another digest', () => { const previous = registry({ @@ -31,6 +41,7 @@ test('a change of routerConstructor is a registry change of that path, with anot const digestBefore = view.digestFor('/app/src/routes/a.tsx') expect(view.lookup('/app/src/routes/a.tsx')).toEqual({ routeId: '/a', + kind: 'file', routerConstructor: true, }) context.routesByFile = next @@ -40,3 +51,24 @@ test('a change of routerConstructor is a registry change of that path, with anot ) expect(view.digestFor('/app/src/routes/none.tsx')).toBeNull() }) + +test('a change of the kind is a registry change of that path, with another digest', () => { + const previous = registry({ + '/app/src/routes/a.tsx': { routeId: '/a', routerConstructor: true }, + }) + const next = registry({ + '/app/src/routes/a.tsx': { + routeId: '/a', + routerConstructor: true, + kind: 'root', + }, + }) + expect(changedRoutePaths(next, previous)).toEqual(['/app/src/routes/a.tsx']) + + const context = createRouterPluginContext() + const view = new RouteRegistryView(context) + context.routesByFile = previous + const digestBefore = view.digestFor('/app/src/routes/a.tsx') + context.routesByFile = next + expect(view.digestFor('/app/src/routes/a.tsx')).not.toBe(digestBefore) +}) diff --git a/packages/router-utils/src/compiler/host.ts b/packages/router-utils/src/compiler/host.ts index cc9160cc16..95e3b583ef 100644 --- a/packages/router-utils/src/compiler/host.ts +++ b/packages/router-utils/src/compiler/host.ts @@ -189,9 +189,12 @@ export function fileModifiedAt(path: string): Promise { * the file. */ export interface RouteRegistry { - lookup: ( - path: string, - ) => { routeId: string; routerConstructor?: boolean } | null + lookup: (path: string) => { + routeId: string + /** The file's kind in the route tree. */ + kind: 'root' | 'file' | 'lazy' + routerConstructor?: boolean + } | null digestFor: (path: string) => string | null } diff --git a/packages/start-plugin-core/tests/planners/route-registry-prefetch.test.ts b/packages/start-plugin-core/tests/planners/route-registry-prefetch.test.ts index ce9fd8da34..a36abab078 100644 --- a/packages/start-plugin-core/tests/planners/route-registry-prefetch.test.ts +++ b/packages/start-plugin-core/tests/planners/route-registry-prefetch.test.ts @@ -53,7 +53,7 @@ function setup( const routes: NonNullable = { lookup: (path) => path in entries - ? { routeId: path, routerConstructor: entries[path]! } + ? { routeId: path, kind: 'file', routerConstructor: entries[path]! } : null, digestFor: (path) => path in entries