Next.js: Return mocked router instead of actual router in useRouter#32131
Merged
valentinpalkovic merged 1 commit intoAug 11, 2025
Merged
Conversation
- 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
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)
This was referenced Aug 18, 2025
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.
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:mergedorci:dailyGH 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.tsMake 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 storybook@0.0.0-pr-32131-sha-cac9c086 sandboxor in an existing project withnpx storybook@0.0.0-pr-32131-sha-cac9c086 upgrade.More information
0.0.0-pr-32131-sha-cac9c086fix/nextjs-15-link-componentcac9c0861754912091)To request a new release of this pull request, mention the
@storybookjs/coreteam.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=32131Greptile 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
useRouterexport in the Next.js navigation mock (code/frameworks/nextjs/src/export-mocks/navigation/index.ts) to return the mockednavigationAPIinstead of the actual Next.js router.Previously,
useRouterwas 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 mocknavigationAPIobject 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
navigationAPIobject through thecreateNavigationfunction and manages mock lifecycle throughgetRouter. The fix maintains the same error handling pattern used elsewhere in the mock system by throwingNextjsRouterMocksNotAvailablewhen 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
useRouterimplementation• Needs manual testing verification since automated test coverage is incomplete