Skip to content

Next.js: Add useLinkStatus mock to next/link export mock#34593

Merged
valentinpalkovic merged 9 commits into
storybookjs:nextfrom
philwolstenholme:claude/add-uselink-status-hook-ctuWL
May 19, 2026
Merged

Next.js: Add useLinkStatus mock to next/link export mock#34593
valentinpalkovic merged 9 commits into
storybookjs:nextfrom
philwolstenholme:claude/add-uselink-status-hook-ctuWL

Conversation

@philwolstenholme
Copy link
Copy Markdown
Contributor

@philwolstenholme philwolstenholme commented Apr 19, 2026

Next 15.3 introduced the useLinkStatus hook:

The useLinkStatus hook lets you track the pending state of a <Link>

When useLinkStatus is used in a component that is presented in a story then Storybook users get errors like this one:

TypeError: (0 , next_link__WEBPACK_IMPORTED_MODULE_2__.useLinkStatus) is not a function

or this in the Storybook server output:

export 'useLinkStatus' (imported as 'useLinkStatus') was not found in 'next/link' (possible exports: Link, default)

What I did

Added an export for useLinkStatus, which defaults to { pending: false } but can be overridden via mock. The code was written by Claude Code so let me know if there's anything that isn't in keeping with how Storybook normally does things and I'll make the changes.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Caution

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

I tried testing via yarn task sandbox --template nextjs/default-ts --start-from auto and following the instructions in CONTRIBUTING.md but got lost and couldn't get a local version of the nextjs package running locally to test with.

We can use the CI build to test the work via the new DefaultLinkStatus and PendingLinkStatus stories.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/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 PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>

Summary by CodeRabbit

  • New Features

    • Added useLinkStatus hook mock for Storybook to track Next.js Link navigation states.
  • Tests

    • Added two new Storybook stories demonstrating default and pending link status scenarios.

claude added 5 commits April 19, 2026 13:39
Adds the `useLinkStatus` hook (introduced in Next.js 15.3) to the
`@storybook/nextjs/link.mock` module. Without this export, Storybook
fails at build and runtime when a component imports `useLinkStatus`
from `next/link`.

Also exports the `LinkStatus` type to match the Next.js public API.

https://claude.ai/code/session_01Hw1UdSNqtAb34tbqFFw14S
Adds two template stories to Link.stories.tsx:
- DefaultLinkStatus: asserts the mock returns { pending: false } by default
- PendingLinkStatus: demonstrates overriding via beforeEach + mockReturnValue

Follows the same pattern used in Navigation.stories.tsx and ServerActions.stories.tsx.

https://claude.ai/code/session_01Hw1UdSNqtAb34tbqFFw14S
No other mock manually exports types — they rely on re-exporting from
the real Next.js dist modules. Keeping this consistent; the inline
return type on useLinkStatus is sufficient for type inference.

https://claude.ai/code/session_01Hw1UdSNqtAb34tbqFFw14S
@philwolstenholme philwolstenholme force-pushed the claude/add-uselink-status-hook-ctuWL branch from 42814c0 to 3ada6df Compare April 19, 2026 12:39
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 06211e44-5f31-4ebe-8b8b-fe724ae8fa39

📥 Commits

Reviewing files that changed from the base of the PR and between d226aa9 and 3ada6df.

📒 Files selected for processing (2)
  • code/frameworks/nextjs/src/export-mocks/link/index.tsx
  • code/frameworks/nextjs/template/stories/Link.stories.tsx

📝 Walkthrough

Walkthrough

This adds a new Storybook mock for the Next.js useLinkStatus hook and introduces test stories to verify its behavior in both idle and pending states. The mock returns a boolean pending flag, and stories demonstrate rendering based on this state.

Changes

Cohort / File(s) Summary
Next.js Link Mock
code/frameworks/nextjs/src/export-mocks/link/index.tsx
Added new named export useLinkStatus that returns { pending: false } with Storybook mock name next/link::useLinkStatus.
Link Stories
code/frameworks/nextjs/template/stories/Link.stories.tsx
Added imports for useLinkStatus hook and testing utilities. Created LinkStatusComponent that conditionally renders based on useLinkStatus() result. Added two new Story exports: DefaultLinkStatus (verifies idle state) and PendingLinkStatus (verifies pending state with mocked override).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • storybookjs/storybook#31251: Modifies the same Next.js link mock module (code/frameworks/nextjs/src/export-mocks/link/index.tsx) to add foundational mock Link exports that this PR extends with the new useLinkStatus export.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@valentinpalkovic valentinpalkovic moved this from Empathy Queue (prioritized) to In Progress in Core Team Projects Apr 27, 2026
@valentinpalkovic valentinpalkovic self-assigned this Apr 27, 2026
@valentinpalkovic
Copy link
Copy Markdown
Contributor

Hi @philwolstenholme,

Thank you so much for contributing the fix. Would you mind implementing a fix for @storybook/nextjs-vite as well? That would be much appreciated!

https://github.com/storybookjs/vite-plugin-storybook-nextjs

@valentinpalkovic valentinpalkovic changed the title Next.js: add useLinkStatus mock to next/link export mock Next.js: Add useLinkStatus mock to next/link export mock Apr 27, 2026
@philwolstenholme
Copy link
Copy Markdown
Contributor Author

philwolstenholme commented Apr 27, 2026

Hi @philwolstenholme,

Thank you so much for contributing the fix. Would you mind implementing a fix for @storybook/nextjs-vite as well? That would be much appreciated!

https://github.com/storybookjs/vite-plugin-storybook-nextjs

Hey @valentinpalkovic :) I wasn't sure what to do about @storybook/nextjs-vite when I first raised the PR.

I noticed there's no Link file in code/frameworks/nextjs-vite/src/export-mocks the way there is in code/frameworks/nextjs/src/export-mocks. I wasn't sure if that was deliberate or not?

Would I be able to copy the same code across from the non-Vite version?

@valentinpalkovic
Copy link
Copy Markdown
Contributor

Hi @philwolstenholme,

You're right. We don't have a LinkMock equivalent in nextjs-vite. That's a shame. @yatishgoel since you have contributed the initial implementation of LinkMock, would you be interested to implement LinkMock in nextjs-vite as well (https://github.com/storybookjs/vite-plugin-storybook-nextjs)?

@storybook-app-bot
Copy link
Copy Markdown

storybook-app-bot Bot commented May 6, 2026

Package Benchmarks

Commit: 8c4e985, ran on 20 May 2026 at 12:22:56 UTC

The following packages have significant changes to their size or dependencies:

storybook

Before After Difference
Dependency count 72 60 🎉 -12 🎉
Self size 20.30 MB 20.52 MB 🚨 +217 KB 🚨
Dependency size 36.17 MB 32.84 MB 🎉 -3.34 MB 🎉
Bundle Size Analyzer Link Link

@storybook/nextjs-vite

Before After Difference
Dependency count 93 93 0
Self size 1.38 MB 1.37 MB 🎉 -13 KB 🎉
Dependency size 24.78 MB 24.78 MB 🎉 -400 B 🎉
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 203 194 🎉 -9 🎉
Self size 908 KB 908 KB 🎉 -144 B 🎉
Dependency size 87.59 MB 84.57 MB 🎉 -3.02 MB 🎉
Bundle Size Analyzer Link Link

@storybook/codemod

Before After Difference
Dependency count 196 187 🎉 -9 🎉
Self size 32 KB 32 KB 0 B
Dependency size 86.08 MB 83.06 MB 🎉 -3.02 MB 🎉
Bundle Size Analyzer Link Link

create-storybook

Before After Difference
Dependency count 73 61 🎉 -12 🎉
Self size 1.08 MB 1.08 MB 0 B
Dependency size 56.48 MB 53.36 MB 🎉 -3.12 MB 🎉
Bundle Size Analyzer node node

@valentinpalkovic valentinpalkovic merged commit a754286 into storybookjs:next May 19, 2026
130 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Core Team Projects May 19, 2026
@github-actions github-actions Bot mentioned this pull request May 19, 2026
13 tasks
@valentinpalkovic valentinpalkovic added the patch:yes Bugfix & documentation PR that need to be picked to main branch label May 20, 2026
@github-actions github-actions Bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label May 22, 2026
RuBrock pushed a commit to RuBrock/storybook that referenced this pull request May 23, 2026
…-uselink-status-hook-ctuWL

Next.js: Add useLinkStatus mock to next/link export mock

(cherry picked from commit a754286)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci:normal nextjs patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants