-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Next.js: Return mocked router instead of actual router in useRouter #32131
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
valentinpalkovic
merged 1 commit into
storybookjs:next
from
JulioJ11:fix/nextjs-15-link-component
Aug 11, 2025
Merged
Next.js: Return mocked router instead of actual router in useRouter #32131
valentinpalkovic
merged 1 commit into
storybookjs:next
from
JulioJ11:fix/nextjs-15-link-component
Aug 11, 2025
+8
−1
Conversation
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
- Modified useRouter export to return navigationAPI mock - Prevents the Link components from using the real Next.js router - Addresses Next.js 15 Link navigation issue in Storybook - Fixes storybookjs#30390
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.
1 file reviewed, no comments
valentinpalkovic
approved these changes
Aug 11, 2025
View your CI Pipeline Execution ↗ for commit cac9c08
☁️ Nx Cloud last updated this comment at |
8 tasks
This was referenced Aug 11, 2025
Merged
Package BenchmarksCommit: The following packages have significant changes to their size or dependencies:
|
Before | After | Difference | |
---|---|---|---|
Dependency count | 12 | 12 | 0 |
Self size | 2.47 MB | 2.42 MB | 🎉 -53 KB 🎉 |
Dependency size | 8.93 MB | 8.93 MB | 🎉 -14 B 🎉 |
Bundle Size Analyzer | Link | Link |
storybook
Before | After | Difference | |
---|---|---|---|
Dependency count | 51 | 51 | 0 |
Self size | 41.75 MB | 41.37 MB | 🎉 -387 KB 🎉 |
Dependency size | 18.13 MB | 18.13 MB | 🎉 -2 B 🎉 |
Bundle Size Analyzer | Link | Link |
sb
Before | After | Difference | |
---|---|---|---|
Dependency count | 52 | 52 | 0 |
Self size | 1 KB | 1 KB | 🚨 +12 B 🚨 |
Dependency size | 59.89 MB | 59.50 MB | 🎉 -387 KB 🎉 |
Bundle Size Analyzer | Link | Link |
@storybook/cli
Before | After | Difference | |
---|---|---|---|
Dependency count | 220 | 220 | 0 |
Self size | 585 KB | 585 KB | 🚨 +13 B 🚨 |
Dependency size | 104.39 MB | 103.99 MB | 🎉 -394 KB 🎉 |
Bundle Size Analyzer | Link | Link |
@storybook/codemod
Before | After | Difference | |
---|---|---|---|
Dependency count | 189 | 189 | 0 |
Self size | 31 KB | 31 KB | 🎉 -2 B 🎉 |
Dependency size | 88.46 MB | 88.08 MB | 🎉 -387 KB 🎉 |
Bundle Size Analyzer | Link | Link |
yannbf
pushed a commit
that referenced
this pull request
Aug 11, 2025
Next.js: Return mocked router instead of actual router in useRouter (cherry picked from commit f0d9efe)
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.
Closes #30390
What I did
Modified the useRouter export in the Next.js navigation mock to return the mocked navigationAPI instead of the actual Next.js router. This prevents Link components in Next.js 15 from breaking out of the Storybook context when clicked.
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
Manual testing performed:
To test this fix:
-->
Documentation
MIGRATION.MD
No documentation updates required - this is an internal bug fix that restores expected behavior.
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.ts
Make sure this PR contains one of the labels below:
Available labels
bug
: Internal changes that fixes incorrect behavior.maintenance
: User-facing maintenance tasks.dependencies
: Upgrading (sometimes downgrading) dependencies.build
: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup
: Minor cleanup style change. Will not show up in release changelog.documentation
: Documentation only changes. Will not show up in release changelog.feature request
: Introducing a new feature.BREAKING CHANGE
: Changes that break compatibility in some way with current major version.other
: Changes that don't fit in the above categories.🦋 Canary release
This pull request has been released as version
0.0.0-pr-32131-sha-cac9c086
. Try it out in a new sandbox by runningnpx [email protected] sandbox
or in an existing project withnpx [email protected] upgrade
.More information
0.0.0-pr-32131-sha-cac9c086
fix/nextjs-15-link-component
cac9c086
1754912091
)To request a new release of this pull request, mention the
@storybookjs/core
team.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=32131
Greptile Summary
This PR fixes a critical issue in Next.js 15 where Link components would navigate outside of Storybook stories to actual routes, causing "Not Found" errors. The fix modifies the
useRouter
export in the Next.js navigation mock (code/frameworks/nextjs/src/export-mocks/navigation/index.ts
) to return the mockednavigationAPI
instead of the actual Next.js router.Previously,
useRouter
was implemented as a passthrough mock that called the real Next.js router implementation. This worked in earlier versions but broke with Next.js 15's changes to the Link component behavior. The new implementation returns the mocknavigationAPI
object when available, ensuring all navigation actions (push, replace, forward, back, prefetch, refresh) stay within the Storybook context.This change integrates seamlessly with the existing navigation mock system, which already provides the
navigationAPI
object through thecreateNavigation
function and manages mock lifecycle throughgetRouter
. The fix maintains the same error handling pattern used elsewhere in the mock system by throwingNextjsRouterMocksNotAvailable
when mocks aren't initialized.Confidence score: 4/5
• This fix addresses a clear regression and follows established patterns in the codebase
• The solution is targeted and minimal, changing only the problematic
useRouter
implementation• Needs manual testing verification since automated test coverage is incomplete