-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
perf(router-core): parallelize route asset projection #8151
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1231,25 +1231,41 @@ export async function projectLane( | |
| end = lane[1 /* matches */].length, | ||
| ): Promise<ProjectedLane> { | ||
| const matches = lane[1 /* matches */] | ||
| let projections: Array<[WorkMatch, Promise<any>]> | undefined | ||
| for (let index = start; index < end; index++) { | ||
| const match = matches[index]! | ||
| const routeOptions = getRoute(router, match).options | ||
| if (routeOptions.head || routeOptions.scripts) { | ||
| const context = { | ||
| ssr: router.options.ssr, | ||
| matches, | ||
| match, | ||
| params: match.params, | ||
| loaderData: match.loaderData, | ||
| } | ||
| let projection | ||
| try { | ||
| const context = { | ||
| ssr: router.options.ssr, | ||
| matches, | ||
| match, | ||
| params: match.params, | ||
| loaderData: match.loaderData, | ||
| } | ||
| const [head, scripts] = await waitFor( | ||
| projection = waitFor( | ||
| Promise.all([ | ||
| routeOptions.head?.(context), | ||
| routeOptions.scripts?.(context), | ||
| ]), | ||
|
Comment on lines
+1248
to
1252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a nested route's Useful? React with 👍 / 👎. |
||
| signal, | ||
| ) | ||
| } catch (cause) { | ||
| projection = Promise.reject(cause) | ||
| } | ||
| void projection.catch(() => {}) | ||
| ;(projections ??= []).push([match, projection]) | ||
| } | ||
| if (match.status !== 'success' || match._notFound) { | ||
| break | ||
| } | ||
| } | ||
| if (projections) { | ||
| for (const [match, projection] of projections) { | ||
| try { | ||
| const [head, scripts] = await projection | ||
| match.meta = head?.meta | ||
| match.links = head?.links | ||
| match.headScripts = head?.scripts | ||
|
|
@@ -1262,9 +1278,6 @@ export async function projectLane( | |
| console.error(cause) | ||
| } | ||
| } | ||
| if (match.status !== 'success' || match._notFound) { | ||
| break | ||
| } | ||
| } | ||
| return lane as ProjectedLane | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { createMemoryHistory } from '@tanstack/history' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
| import { BaseRootRoute, BaseRoute, createControlledPromise } from '../src' | ||
| import { createTestRouter, loadServerResponse } from './routerTestUtils' | ||
|
|
||
| describe.each([false, true])( | ||
| 'route asset projection (server: %s)', | ||
| (isServer) => { | ||
| test('starts child assets before parent assets settle', async () => { | ||
| const parentStarted = createControlledPromise<void>() | ||
| const parentHead = createControlledPromise<{ | ||
| meta: Array<{ title: string }> | ||
| }>() | ||
| const parentScripts = | ||
| createControlledPromise<Array<{ children: string }>>() | ||
| const childHead = vi.fn(() => ({ | ||
| meta: [{ title: 'child' }], | ||
| })) | ||
| const childScripts = vi.fn(() => [{ children: 'window.child = true' }]) | ||
|
|
||
| const rootRoute = new BaseRootRoute({}) | ||
| const parentRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/parent', | ||
| head: () => { | ||
| parentStarted.resolve() | ||
| return parentHead | ||
| }, | ||
| scripts: () => parentScripts, | ||
| }) | ||
| const childRoute = new BaseRoute({ | ||
| getParentRoute: () => parentRoute, | ||
| path: '/child', | ||
| head: childHead, | ||
| scripts: childScripts, | ||
| }) | ||
| const router = createTestRouter({ | ||
| routeTree: rootRoute.addChildren([ | ||
| parentRoute.addChildren([childRoute]), | ||
| ]), | ||
| history: createMemoryHistory({ initialEntries: ['/parent/child'] }), | ||
| isServer, | ||
| }) | ||
|
|
||
| const loading = isServer | ||
| ? loadServerResponse(router, '/parent/child') | ||
| : router.load() | ||
| try { | ||
| await parentStarted | ||
|
|
||
| expect(parentScripts.status).toBe('pending') | ||
| expect(childHead).toHaveBeenCalledTimes(1) | ||
| expect(childScripts).toHaveBeenCalledTimes(1) | ||
| } finally { | ||
| parentHead.resolve({ meta: [{ title: 'parent' }] }) | ||
| parentScripts.resolve([{ children: 'window.parent = true' }]) | ||
| await loading | ||
| } | ||
|
|
||
| expect(router.state.matches.at(-1)).toMatchObject({ | ||
| meta: [{ title: 'child' }], | ||
| scripts: [{ children: 'window.child = true' }], | ||
| }) | ||
| }) | ||
| }, | ||
| ) |
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.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Preserve the deferred projection result contract. Both schedulers store projection results as
Promise<any>, so TypeScript cannot validate the values assigned to route matches.packages/router-core/src/load-client.ts#L1234-L1234: use a typed tuple forheadandscriptsprojection results.packages/router-core/src/load-server.ts#L623-L623: use the corresponding typed tuple forhead,scripts, andheadersprojection results.📍 Affects 2 files
packages/router-core/src/load-client.ts#L1234-L1234(this comment)packages/router-core/src/load-server.ts#L623-L623🤖 Prompt for AI Agents
Source: Coding guidelines