-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router): warn when Outlet is rendered in fallback components #8045
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@tanstack/react-router': patch | ||
| '@tanstack/solid-router': patch | ||
| '@tanstack/vue-router': patch | ||
| --- | ||
|
|
||
| Warn when an Outlet is rendered inside a pending, error, or not-found component. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 'use client' | ||
|
|
||
| import * as React from 'react' | ||
|
|
||
| export type NonRouteComponent = | ||
| | 'pendingComponent' | ||
| | 'errorComponent' | ||
| | 'notFoundComponent' | ||
|
|
||
| export const nonRouteComponentContext = | ||
| process.env.NODE_ENV !== 'production' | ||
| ? React.createContext<NonRouteComponent | undefined>(undefined) | ||
| : undefined | ||
|
|
||
| export function wrapInNonRouteComponentContext( | ||
| element: React.ReactElement, | ||
| component: NonRouteComponent, | ||
| ): React.ReactElement { | ||
| const Context = nonRouteComponentContext! | ||
| return <Context.Provider value={component}>{element}</Context.Provider> | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import { afterEach, expect, test, vi } from 'vitest' | ||
| import { cleanup, render, screen } from '@testing-library/react' | ||
| import { createMemoryHistory } from '@tanstack/history' | ||
| import { createControlledPromise, notFound } from '@tanstack/router-core' | ||
| import { | ||
| Outlet, | ||
| RouterProvider, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| } from '../src' | ||
|
|
||
| const outletWarning = ( | ||
| component: 'pendingComponent' | 'errorComponent' | 'notFoundComponent', | ||
| ) => | ||
| `Warning: An <Outlet /> was rendered inside a ${component}. <Outlet /> should only be rendered inside a route component.` | ||
|
|
||
| afterEach(() => { | ||
| cleanup() | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| test('does not warn when Outlet is rendered inside a route component', async () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
| const rootRoute = createRootRoute({ | ||
| component: () => ( | ||
| <> | ||
| <span>Root route</span> | ||
| <Outlet /> | ||
| </> | ||
| ), | ||
| }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <span>Index route</span>, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
|
|
||
| expect(await screen.findByText('Index route')).toBeInTheDocument() | ||
| expect(warn).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('warns when Outlet is rendered inside a pendingComponent', async () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
| const pending = createControlledPromise<void>() | ||
| const rootRoute = createRootRoute({ component: Outlet }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <span>Index route</span>, | ||
| }) | ||
| const pendingRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/pending', | ||
| loader: () => pending, | ||
| pendingMs: 0, | ||
| pendingComponent: () => ( | ||
| <> | ||
| <span>Pending route</span> | ||
| <Outlet /> | ||
| </> | ||
| ), | ||
| component: () => <span>Resolved route</span>, | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, pendingRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| await screen.findByText('Index route') | ||
|
|
||
| const navigation = router.navigate({ to: '/pending' }) | ||
| expect(await screen.findByText('Pending route')).toBeInTheDocument() | ||
| pending.resolve() | ||
| await navigation | ||
|
|
||
| expect(warn).toHaveBeenCalledWith(outletWarning('pendingComponent')) | ||
| }) | ||
|
|
||
| test('warns when Outlet is rendered inside an errorComponent', async () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
| const rootRoute = createRootRoute({ component: Outlet }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| loader: () => { | ||
| throw new Error('Loader failed') | ||
| }, | ||
| errorComponent: () => ( | ||
| <> | ||
| <span>Error route</span> | ||
| <Outlet /> | ||
| </> | ||
| ), | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
|
|
||
| expect(await screen.findByText('Error route')).toBeInTheDocument() | ||
| expect(warn).toHaveBeenCalledWith(outletWarning('errorComponent')) | ||
| }) | ||
|
|
||
| test('warns when Outlet is rendered inside a notFoundComponent', async () => { | ||
| const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) | ||
| const rootRoute = createRootRoute({ component: Outlet }) | ||
| const indexRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/', | ||
| component: () => <span>Index route</span>, | ||
| }) | ||
| const notFoundRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/not-found', | ||
| component: () => { | ||
| throw notFound() | ||
| }, | ||
| notFoundComponent: () => ( | ||
| <> | ||
| <span>Not found route</span> | ||
| <Outlet /> | ||
| </> | ||
| ), | ||
| }) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([indexRoute, notFoundRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/'] }), | ||
| }) | ||
|
|
||
| render(<RouterProvider router={router} />) | ||
| await screen.findByText('Index route') | ||
| await router.navigate({ to: '/not-found' }) | ||
|
|
||
| expect(await screen.findByText('Not found route')).toBeInTheDocument() | ||
| expect(warn).toHaveBeenCalledWith(outletWarning('notFoundComponent')) | ||
| }) | ||
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.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Add router-level fallback warning tests.
These tests cover only route-level
pendingComponent,errorComponent, andnotFoundComponentoptions. Add cases that configuredefaultPendingComponent,defaultErrorComponent, anddefaultNotFoundComponentoncreateRouterwhile the affected route omits its equivalent option.This is required by the PR objective for router-level fallback coverage.
🤖 Prompt for AI Agents