Builder-Vite: Use relative path for mocker entry in production builds#33792
Merged
valentinpalkovic merged 2 commits intoFeb 9, 2026
Merged
Conversation
…uilds The vite-inject-mocker plugin injects a `<script>` tag with an absolute path (`/vite-inject-mocker-entry.js`) into the HTML. When the built Storybook is hosted under a non-root base path (e.g. GitHub Pages at `/my-repo/`), the browser resolves the absolute URL against the host root, resulting in a 404. The fix switches to a relative path (`./vite-inject-mocker-entry.js`) during production builds while keeping the absolute path in dev mode (where Vite's dev-server needs it for `resolveId` matching). This is consistent with other assets in `iframe.html` which already use relative paths (e.g. `./sb-common-assets/…`). Fixes storybookjs#32428 Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughPlugin behavior changed to inject a relative script path for production builds and an absolute path for development; tests added to validate transformIndexHtml injection, path selection, placement, and edge cases. Changes
Sequence Diagram(s)sequenceDiagram
participant Vite as Vite (build/serve)
participant Plugin as vite-inject-mocker plugin
participant HTML as index.html
Vite->>Plugin: configResolved({ command: "build" | "serve" })
Vite->>Plugin: transformIndexHtml(html)
Plugin->>Plugin: compute entrySrc (./entry.js if build, /entry.js if serve)
Plugin->>HTML: inject <script src="entrySrc"> immediately after <head>
Plugin-->>Vite: return transformed HTML
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
✨ Finishing touches
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. Comment |
valentinpalkovic
approved these changes
Feb 9, 2026
This was referenced Feb 9, 2026
Closed
Merged
valentinpalkovic
added a commit
that referenced
this pull request
Feb 17, 2026
…ative-path Builder-Vite: Use relative path for mocker entry in production builds (cherry picked from commit 3c49657)
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.
Human View
What I did
Fixed the
vite-inject-mockerplugin to use a relative path (./vite-inject-mocker-entry.js) instead of an absolute path (/vite-inject-mocker-entry.js) when injecting the mocker runtime<script>tag during production builds.Why
When Storybook artifacts are hosted under a non-root base path (e.g. GitHub Pages at
https://user.github.io/my-repo/), the browser resolves the absolute path against the host root, causing a 404:This is consistent with other assets in
iframe.htmlwhich already use relative paths (e.g../sb-common-assets/nunito-sans-regular.woff2).How
The
transformIndexHtmlhook now checksviteConfig.command:build→ uses./vite-inject-mocker-entry.js(relative, works at any base path)serve→ keeps/vite-inject-mocker-entry.js(absolute, required for Vite dev-serverresolveIdmatching)The emitted chunk (
fileName: 'vite-inject-mocker-entry.js') is placed at the same directory level asiframe.html, so the relative path resolves correctly.Testing
Added 6 unit tests covering:
<head>)<head>with attributes<head>tag (graceful no-op)All 6 new tests pass. All pre-existing tests in
builder-viteremain green.Checklist
Closes #32428
AI View (DCCE Protocol v1.0)
Metadata
AI Contribution Summary
Verification Steps Performed
Human Review Guidance
./vite-inject-mocker-entry.js,/vite-inject-mocker-entry.js,iframe.htmlMade with M7 Cursor
Summary by CodeRabbit
Tests
Bug Fixes