Skip to content
Merged
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
35 changes: 35 additions & 0 deletions code/frameworks/tanstack-react/src/routing/duplicate-tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest';
import {
createLazyRoute,
createMemoryHistory,
createRootRoute,
createRoute,
Expand Down Expand Up @@ -233,4 +234,38 @@ describe('duplicateRouteTree matrix (code-based and file-based trees)', () => {
expect(override).toHaveBeenCalled();
expect(original).not.toHaveBeenCalled();
});

it('carries lazy route bindings onto clones', async () => {
const marker = () => null;
const root = createRootRoute();
const page = createRoute({ path: '/lazy', getParentRoute: () => root });
page.lazy(() => Promise.resolve(createLazyRoute('/lazy')({ component: marker })));
root.addChildren([page]);

const { root: cloned } = duplicateRouteTree(root as any);
const router = createRouter({
routeTree: cloned,
history: createMemoryHistory({ initialEntries: ['/lazy'] }),
});
await router.load();

expect((router as any).routesById['/lazy'].options.component).toBe(marker);
});

it('carries a lazy binding on the root route onto the clone', async () => {
const marker = () => null;
const root = createRootRoute();
(root as any).lazy(() => Promise.resolve(createLazyRoute('__root__')({ component: marker })));
const child = createRoute({ path: '/child', getParentRoute: () => root });
root.addChildren([child]);

const { root: cloned } = duplicateRouteTree(root as any);
const router = createRouter({
routeTree: cloned as any,
history: createMemoryHistory({ initialEntries: ['/child'] }),
});
await router.load();

expect((router as any).routesById['__root__'].options.component).toBe(marker);
});
});
9 changes: 9 additions & 0 deletions code/frameworks/tanstack-react/src/routing/duplicate-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ function cloneChild(
(cloned as any).update({ id: explicitId });
}

const lazyFn = (oldRoute as any).lazyFn;
if (lazyFn) {
(cloned as any).lazy(lazyFn);
}

byId.set(oldRoute.id, cloned as unknown as AnyRoute);

const children = (oldRoute as any).children as AnyRoute[] | undefined;
Expand Down Expand Up @@ -150,6 +155,10 @@ export function duplicateRouteTree(
...restRoot,
...rootOverride,
} as any);
const rootLazyFn = (rootRoute as any).lazyFn;
if (rootLazyFn) {
(newRoot as any).lazy(rootLazyFn);
}
byId.set('__root__', newRoot as unknown as AnyRoute);

const children = (rootRoute as any).children as AnyRoute[] | undefined;
Expand Down