Prerender ignores Vite's root: `vite build <dir>` and `createBuilder({ root })` fail from another folder
Maintainer thường phản hồi trong vòng 1 ngày
@aaronanish-t đang làm issue này rồi.
Từ ngày 4/11/2026.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- bun, react, typescript, vite
- Lĩnh vực
- backend, build-system
Hướng nghiên cứu
Read src/vite/prerender.ts, src/vite/preview-server-plugin/plugin.ts, and src/vite/post-server-build.ts, then reproduce with the linked minimal project by running vite build project from its parent directory. Trace how the resolved Vite root is used for preview startup and output paths. Done means prerender succeeds and all generated files land under project/dist/; linked PR #8826 is already open.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Which project does this relate to?
Start
Describe the bug
When Vite's root is not the working directory, the prerender step after vite build fails. The client and SSR builds succeed.
Prerender resolves paths against process.cwd() instead of the build's resolved root, in three places (@tanstack/start-plugin-core 1.171.55, same code on main):
src/vite/prerender.tsL68-74:vite.preview()gets onlyconfigFile, so the preview config takes itsrootfromprocess.cwd(). The Start plugin then looks forsrcDirectoryunder the working directory, and the preview server fails to start.src/vite/preview-server-plugin/plugin.tsL39-43:getServerOutputDirectory(server.config)returns the relativebuild.outDir(dist/server), andpathToFileURL()resolves it againstprocess.cwd(). The prerender request for/returns 500.src/vite/prerender.tsL29 andsrc/vite/post-server-build.tsL18: the client output directory stays relative.src/prerender.tsL194-195 (path.resolve(outputDir, …)) and the sitemap step then writeindex.html,pages.jsonandsitemap.xmlto<cwd>/dist/client. The build exits 0, so this one is silent.
Vite keeps build.outDir relative in the resolved config and resolves it with path.resolve(config.root, outDir) itself.
Complete minimal reproducer
Reproduction repository: https://github.com/loriensleafs/tanstack-start-prerender-root-repro
The files, all in repro/project/:
package.json
{
"name": "start-prerender-root-repro",
"private": true,
"type": "module",
"scripts": { "build": "vite build" },
"dependencies": {
"@tanstack/react-router": "1.170.46",
"@tanstack/react-start": "1.169.4",
"react": "19.3.1",
"react-dom": "19.3.1"
},
"devDependencies": {
"@vitejs/plugin-react": "6.1.2",
"vite": "8.3.5"
}
}
vite.config.ts
import { tanstackStart } from '@tanstack/react-start/plugin/vite'
import viteReact from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
tanstackStart({
srcDirectory: 'app',
prerender: { enabled: true },
}),
viteReact(),
],
})
build.ts (the programmatic form)
import { createBuilder } from 'vite'
const builder = await createBuilder({ root: import.meta.dirname })
await builder.buildApp()
app/router.tsx
import { createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'
export function getRouter() {
return createRouter({ routeTree })
}
app/routes/__root.tsx
import { HeadContent, Outlet, Scripts, createRootRoute } from '@tanstack/react-router'
export const Route = createRootRoute({
head: () => ({ meta: [{ charSet: 'utf-8' }, { title: 'Repro' }] }),
component: () => (
<html>
<head>
<HeadContent />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
),
})
app/routes/index.tsx
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/')({
component: () => <h1>Hello from the prerender</h1>,
})
Steps to Reproduce the Bug
-
cd repro/project && bun install -
bun --bun vite build: passes, anddist/client/index.htmlis written. -
rm -rf dist && cd ..(now inrepro/) -
bun --bun project/node_modules/.bin/vite build project: fails after the client and SSR builds:error during build: Error: Failed to start the Vite preview server for prerendering [cause]: Error: Could not resolve entry for router entry: router in /path/to/repro/app -
bun --bun project/build.ts: the same error (createBuilder({ root })).
Each of the following runs starts from repro/:
vite.config.ts change |
Result |
|---|---|
| none | Fails: router in /path/to/repro/app (step 4) |
none, default src/ and no srcDirectory |
Fails: router in /path/to/repro/src |
root: import.meta.dirname |
Preview starts, prerender fails: Failed to fetch /: Internal Server Error, from Cannot find module '/path/to/repro/dist/server/server.js' |
root + absolute environments.ssr.build.outDir (and sitemap.enabled) |
Exit 0, but index.html, pages.json and sitemap.xml go to /path/to/repro/dist/client/, and project/dist/client/ holds only assets/ |
root + absolute build.outDir |
Passes |
nitro() added (nitro 3.0.260903-beta), no root |
Fails: router in /path/to/repro/app |
nitro() added + root: import.meta.dirname |
Passes |
Expected behavior
As a user, I expected vite build project and createBuilder({ root }) to prerender the same as vite build from inside project/, with all output under project/dist/. Instead, the prerender looks for the source and the build output under the working directory, and fails or writes the pages there.
Screenshots or Videos
N/A (build time).
Platform
- Router / Start Version:
@tanstack/react-start1.169.4 (@tanstack/start-plugin-core1.171.55, the latest on npm),@tanstack/react-router1.170.46 - OS: macOS (arm64)
- Browser: N/A (build time)
- Browser Version: N/A
- Bundler: vite (
@vitejs/plugin-react6.1.2, React 19.3.1) - Bundler Version: 8.3.5
- Runtime: Bun 1.4.4 (
bun --bun). Not tried with Node.
Additional context
Workaround. With Nitro, set root: import.meta.dirname in vite.config.ts. Without Nitro, also set an absolute build.outDir:
import { join } from 'node:path'
export default defineConfig({
root: import.meta.dirname,
build: { outDir: join(import.meta.dirname, 'dist') },
// plugins as above
})
Suggested fix. Pass the resolved root to the preview server, and resolve both output directories against it:
// src/vite/prerender.ts
- const outputDir = clientEnv.config.build.outDir
+ const outputDir = path.resolve(clientEnv.config.root, clientEnv.config.build.outDir)
…
return await vite.preview({
configFile: viteConfig.configFile,
+ root: viteConfig.root,
preview: { port: 0, open: false },
})
// src/vite/preview-server-plugin/plugin.ts
- const serverOutputDir = getServerOutputDirectory(server.config)
+ const serverOutputDir = path.resolve(server.config.root, getServerOutputDirectory(server.config))
// src/vite/post-server-build.ts
- return getClientOutputDirectory(builder.config)
+ return path.resolve(builder.config.root, getClientOutputDirectory(builder.config))
I patched these edits into dist/esm of 1.171.55. With them, steps 4 and 5 pass with the config above plus sitemap: { enabled: true, host }, all output lands in project/dist/client/, and nothing is written to repro/. Step 2 still passes, and the Nitro variant passes without root.
Related. #7593 and #8615 forward configLoader in the same vite.preview() call. #8430 moves that call into a worker, and it still passes only configFile. Neither passes root.
Separately, @tanstack/router-generator (src/config.ts, resolveTmpDir) resolves tmpDir against process.cwd(), so an empty .tanstack/tmp folder appears in the working directory. It does not break the build.
This report was drafted with AI assistance. Each run above was made on the versions listed.
- Ngôn ngữ chính
- TypeScript
- Star
- 15.3k
- Fork
- 1.9k
- Merge trung bình
- 1 ngày 5 giờ
- Pull request đã merge (30 ngày)
- 46
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/router
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Masked navigation drops the first character of the hash each time (__tempLocation hash missing '#')Có thể đã có người làm @hassanabrar202 đã nhận 8 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
router-generator: lazy-load Prettier (costs ~300–450ms per build even when formatting is off)Có thể đã có người làm @Sheraff đã nhận 27 ngày trước. Đang mởpackage: router-generator performance
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
router-generator: sibling `physical()` subtrees in a virtual route config generate duplicate identifiersCó thể đã có người làm @Sheraff đã nhận 27 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
docs(router): clarify static title fallback in SPA modeCó thể đã có người làm @Rohan-Dedhia đã nhận 17 ngày trước. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của TanStack/router
Issue tương tự
-
[BUG]: Shared links drop the configured basePath on GitHub PagesCó thể đã có người làm @anshulchikhale30-p đã nhận hôm nay. Đang mởbug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
StabilityNexus/IdentityTokens-EVM-Frontend#182 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Add: GTrekkerĐang mởcheck:passed streams:add
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Koenkk/zigbee-herdsman-converters#13900 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area: ui Bug Payload OSS
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
payloadcms/payload#19839 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
jeanibarz/knowledge-base-mcp-server#1291 ·
Maintainer thường phản hồi trong vòng 1 ngày