NextJS: Support as prop in next/link mock#35148
Conversation
The next/link mock destructured the `as` prop but never used it, so `<Link href="/users/[id]" as="/users/123">` rendered the unresolved route template (`/users/[id]`) instead of the resolved URL that real Next.js shows (`/users/123`). Compute the anchor href from `as ?? href`, resolving `as` as either a string or a UrlObject the same way `href` is already resolved. Applied to both @storybook/nextjs and @storybook/nextjs-vite, with regression tests covering the `as` string, `as` UrlObject, and `href` fallback. Closes storybookjs#35145
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR adds support for the ChangesLink mock
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Comment |
as prop in next/link mock when rendering hrefas prop in next/link mock
Package BenchmarksCommit: No significant changes detected, all good. 👏 |
Closes #35145
What I did
The
next/linkmock in@storybook/nextjsand@storybook/nextjs-vitedestructured theasprop but never used it, so the rendered anchor always used the rawhref. As a result, a dynamic link like:rendered
<a href="/users/[id]">in Storybook instead of the resolved<a href="/users/123">that a real Next.js app produces. This made it impossible to inspect/verify the final URL when hovering or inspecting links.The fix computes the anchor href from
as ?? href, resolvingasas either a string or aUrlObjectusing the same logic already applied tohref. Whenasis not provided, behavior is unchanged (it falls back tohref). The change is applied identically to both frameworks:code/frameworks/nextjs/src/export-mocks/link/index.tsxcode/frameworks/nextjs-vite/src/export-mocks/link/index.tsxRegression tests were added next to each mock covering the
asstring, theasUrlObject, and thehreffallback cases.Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
Caution
This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!
yarn task --task sandbox --start-from auto --template nextjs-vite/default-tsLinkstory file (template/stories/Link.stories.tsx) and add a story that uses bothhrefandas, e.g.:hrefis the resolved/users/123(not the/users/[id]template).href(noas) still renders itshrefunchanged.Documentation
MIGRATION.MD
No documentation changes are required — this aligns the mock's behavior with the existing, already-documented Next.js
Linkasprop.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.tsDeclare whether manual QA will be needed for this PR during the next release, through
qa:neededorqa:skipMake 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/coreteam 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
Tests
asprop parameter.Bug Fixes
asprop as a URL override, with appropriate fallback tohrefresolution and query parameter serialization.