Dev serves a disabled feature in full: the stub swap misses the `?v=` Vite adds to resolved ids #114
Version: @tanstack/redact 0.2.3, the check unchanged on main (37c051c9). vite 8.3.5, @vitejs/plugin-react 6.1.2.
With viewTransitions off, which both presets default to, vite build swaps in features/view-transition/stub.js, but vite dev still serves the full feature. Dev and production then run different code: an app that renders <ViewTransition> animates in dev and silently doesn't in production.
Reproduction#
npm i, then npx vite --port 5199 and, in another shell, the two curls below. Then npx vite build.
package.json
{
"name": "redact-view-transitions-dev",
"private": true,
"type": "module",
"dependencies": {
"@tanstack/redact": "0.2.3",
"@vitejs/plugin-react": "6.1.2",
"vite": "8.3.5"
}
}
vite.config.js
import { redact } from "@tanstack/redact/vite"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
export default defineConfig({ plugins: [react(), redact()] })
index.html
<!doctype html><html><body><div id="root"></div><script type="module" src="/src/main.jsx"></script></body></html>
src/main.jsx
import { createRoot } from "react-dom/client"
createRoot(document.getElementById("root")).render(<h1>hi</h1>)
curl -s http://localhost:5199/ > /dev/null
curl -s http://localhost:5199/node_modules/@tanstack/redact/dist/dom/dispatcher.js | grep -o '"[^"]*view-transition[^"]*"'
view-transition module | startViewTransition in the output | |
|---|---|---|
vite dev | …/dist/dom/features/view-transition/index.js?v=… (re-exports full.js) | yes |
vite build | stub.js | no |
reconcile.js imports the same index.js?v=… in dev.
Cause#
In dev, Vite's resolver appends its version query to files it resolves under node_modules (?v=<browserHash>), and Redact excludes itself from dependency optimization, so this.resolve() returns …/view-transition/index.js?v=46679b34. The peer check in packages/redact/src/vite/index.ts anchors on the end of the id:
const resolvedFeature = r?.id.match(/features[\\/](context|hydration|fragment-refs|view-transition|class|suspense-runtime)[\\/]index\.(ts|js)$/)
so it never matches in dev and the stub isn't swapped in. A build resolves without the query, which is why production is right.
Suggested fix#
Match the path without its query, and keep the query on the stub's id:
const [path = '', query] = r?.id.split('?') ?? []
const resolvedFeature = path.match(/features[\\/](context|hydration|fragment-refs|view-transition|class|suspense-runtime)[\\/]index\.(ts|js)$/)
if (resolvedFeature?.[1] === peer[1]) {
return path.replace(/index\.(ts|js)$/, 'stub.$1') + (query === undefined ? '' : `?${query}`)
}
With this applied to 0.2.3's dist/vite/index.js, the repro's dispatcher.js and reconcile.js import …/view-transition/stub.js?v=… in dev. The other features this check covers would go through the same path when turned off; I've only checked view-transition.
tannerlinsley closed this
Sign in to join this conversation.