-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(react): Add lazyRouteManifest option to resolve lazy-route names
#19086
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
Open
onurtemizkan
wants to merge
1
commit into
develop
Choose a base branch
from
onur/react-router-lazy-manifest-option
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+766
−2
Open
Changes from all commits
Commits
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
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
107 changes: 107 additions & 0 deletions
107
...2e-tests/test-applications/react-router-7-lazy-routes/tests/react-router-manifest.test.ts
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,107 @@ | ||
| import { expect, test, Page } from '@playwright/test'; | ||
|
|
||
| /** | ||
| * Canary tests: React Router route manifest exposure | ||
| * | ||
| * These tests verify that React Router doesn't expose lazy-loaded routes in `router.routes` | ||
| * before navigation completes. They will fail when React Router changes this behavior. | ||
| * | ||
| * - Tests pass when React Router doesn't expose lazy routes (current behavior) | ||
| * - Tests fail when React Router does expose lazy routes (future behavior) | ||
| * | ||
| * If these tests fail, React Router may now expose lazy routes natively, and the | ||
| * `lazyRouteManifest` workaround might no longer be needed. Check React Router's changelog | ||
| * and consider updating the SDK to use native route exposure. | ||
| * | ||
| * Note: `router.routes` is the documented way to access routes when using RouterProvider. | ||
| * See: https://github.com/remix-run/react-router/discussions/10857 | ||
| */ | ||
|
|
||
| /** | ||
| * Extracts all route paths from the React Router instance exposed on window.__REACT_ROUTER__. | ||
| * Recursively traverses the route tree and builds full path strings. | ||
| */ | ||
| async function extractRoutePaths(page: Page): Promise<string[]> { | ||
| return page.evaluate(() => { | ||
| const router = (window as Record<string, unknown>).__REACT_ROUTER__ as | ||
| | { routes?: Array<{ path?: string; children?: unknown[] }> } | ||
| | undefined; | ||
| if (!router?.routes) return []; | ||
|
|
||
| const paths: string[] = []; | ||
| function traverse(routes: Array<{ path?: string; children?: unknown[] }>, parent = ''): void { | ||
| for (const r of routes) { | ||
| const full = r.path ? (r.path.startsWith('/') ? r.path : `${parent}/${r.path}`) : parent; | ||
| if (r.path) paths.push(full); | ||
| if (r.children) traverse(r.children as Array<{ path?: string; children?: unknown[] }>, full); | ||
| } | ||
| } | ||
| traverse(router.routes); | ||
| return paths; | ||
| }); | ||
| } | ||
|
|
||
| test.describe('[CANARY] React Router Route Manifest Exposure', () => { | ||
| /** | ||
| * Verifies that lazy routes are not pre-populated in router.routes. | ||
| * If lazy routes appear in the initial route tree, React Router has changed behavior. | ||
| */ | ||
| test('React Router should not expose lazy routes before lazy handler resolves', async ({ page }) => { | ||
| await page.goto('/'); | ||
| await page.waitForTimeout(500); | ||
|
|
||
| const initialRoutes = await extractRoutePaths(page); | ||
| const hasSlowFetchInitially = initialRoutes.some(p => p.includes('/slow-fetch/:id')); | ||
|
|
||
| // Test passes if routes are not available initially (we need the workaround) | ||
| // Test fails if routes are available initially (workaround may not be needed!) | ||
| expect( | ||
| hasSlowFetchInitially, | ||
| ` | ||
| React Router now exposes lazy routes in the initial route tree! | ||
| This means the lazyRouteManifest workaround may no longer be needed. | ||
|
|
||
| Initial routes: ${JSON.stringify(initialRoutes, null, 2)} | ||
|
|
||
| Next steps: | ||
| 1. Verify this behavior is consistent and intentional | ||
| 2. Check React Router changelog for details | ||
| 3. Consider removing the lazyRouteManifest workaround | ||
| `, | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| /** | ||
| * Verifies that lazy route children are not in router.routes before visiting them. | ||
| */ | ||
| test('React Router should not have lazy route children before visiting them', async ({ page }) => { | ||
| await page.goto('/'); | ||
| await page.waitForTimeout(300); | ||
|
|
||
| const routes = await extractRoutePaths(page); | ||
| const hasLazyChildren = routes.some( | ||
| p => | ||
| p.includes('/lazy/inner/:id') || | ||
| p.includes('/another-lazy/sub/:id') || | ||
| p.includes('/slow-fetch/:id') || | ||
| p.includes('/deep/level2/level3/:id'), | ||
| ); | ||
|
|
||
| // Test passes if lazy children are not in routes before visiting (we need the workaround) | ||
| // Test fails if lazy children are in routes before visiting (workaround may not be needed!) | ||
| expect( | ||
| hasLazyChildren, | ||
| ` | ||
| React Router now includes lazy route children in router.routes upfront! | ||
| This means the lazyRouteManifest workaround may no longer be needed. | ||
|
|
||
| Routes at home page: ${JSON.stringify(routes, null, 2)} | ||
|
|
||
| Next steps: | ||
| 1. Verify this behavior is consistent and intentional | ||
| 2. Check React Router changelog for details | ||
| 3. Consider removing the lazyRouteManifest workaround | ||
| `, | ||
| ).toBe(false); | ||
| }); | ||
| }); |
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.
Unused export of
matchRouteManifestfrom index.tsLow Severity
The
matchRouteManifestfunction is exported fromindex.tsbut this export is never used. Both consumers import directly from./route-manifestinstead:utils.tsimports from'./route-manifest'and the test file imports from'../../src/reactrouter-compat-utils/route-manifest'. This export is dead code that adds unnecessary noise to the public API surface.