Add ssrPathname prop for SSR path-based route matching#99
Merged
Conversation
When ssrPathname is provided, the Router uses it to match path-based routes during SSR instead of only matching pathless routes. Loaders are still skipped during SSR. Once the client hydrates, the real URL from the Navigation API takes over and ssrPathname is ignored. https://claude.ai/code/session_01GFC4bkwjrJwBJzqrQxK77n
There was a problem hiding this comment.
Pull request overview
This PR adds a new ssrPathname prop to the Router component to enable path-based route matching during server-side rendering (SSR). Previously, only pathless routes could match during SSR. With this prop, developers can specify a pathname that the router uses to match path-based routes during SSR, while loaders remain skipped. Once the client hydrates, the Navigation API takes over and the ssrPathname is ignored.
Changes:
- Added
ssrPathnameoptional prop to Router component with comprehensive JSDoc documentation - Modified route matching logic to use
ssrPathname ?? nullwhen locationEntry is unavailable during SSR - Added comprehensive test suite with 8 test cases covering various scenarios including nested routes, params extraction, and loader behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/router/src/Router.tsx | Added ssrPathname prop definition with documentation, updated route matching logic to use ssrPathname during SSR, and added ssrPathname to useMemo dependency array |
| packages/router/src/tests/fallback.test.tsx | Added comprehensive test suite for ssrPathname feature covering path matching, nested routes, params, loaders, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Routes with loaders are now excluded from matching during SSR (when ssrPathname is provided). This preserves type safety since loader data is unavailable during SSR. When all children of a route have loaders, the parent renders as an SSR shell instead. Added skipLoaders option to matchRoutes and corresponding unit tests. https://claude.ai/code/session_01GFC4bkwjrJwBJzqrQxK77n
The pathname === null checks in matchRoute already handle loader skipping, so skipLoaders: true is redundant but clearer in that case. https://claude.ai/code/session_01GFC4bkwjrJwBJzqrQxK77n
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When ssrPathname is provided, the Router uses it to match path-based
routes during SSR instead of only matching pathless routes. Loaders
are still skipped during SSR. Once the client hydrates, the real URL
from the Navigation API takes over and ssrPathname is ignored.
https://claude.ai/code/session_01GFC4bkwjrJwBJzqrQxK77n