Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-prerender-root.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/start-plugin-core': patch
---

Resolve the prerender paths against Vite's resolved `root` instead of `process.cwd()`. When the root is not the working directory, `vite build <dir>` and `createBuilder({ root })` previously failed to start the preview server or wrote `index.html`, `pages.json` and `sitemap.xml` outside the project.
3 changes: 2 additions & 1 deletion packages/start-plugin-core/src/vite/post-server-build.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'pathe'
import { postBuild } from '../post-build'
import { getClientOutputDirectory } from './output-directory'
import { prerenderWithVite } from './prerender'
Expand All @@ -15,7 +16,7 @@ export async function postServerBuild({
startConfig,
adapter: {
getClientOutputDirectory() {
return getClientOutputDirectory(builder.config)
return resolve(builder.config.root, getClientOutputDirectory(builder.config))
},
prerender(startConfig) {
return prerenderWithVite({
Expand Down
9 changes: 8 additions & 1 deletion packages/start-plugin-core/src/vite/prerender.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'pathe'
import { VITE_ENVIRONMENT_NAMES } from '../constants'
import { prerender } from '../prerender'
import type { PrerenderHandler } from '../prerender'
Expand Down Expand Up @@ -26,7 +27,10 @@ export async function prerenderWithVite({
)
}

const outputDir = clientEnv.config.build.outDir
const outputDir = resolve(
clientEnv.config.root,
clientEnv.config.build.outDir,
)

process.env.TSS_PRERENDERING = 'true'
process.env.TSS_CLIENT_OUTPUT_DIR = outputDir
Expand Down Expand Up @@ -66,6 +70,9 @@ async function startPreviewServer(

try {
return await vite.preview({
// Reuse the root the build resolved, so the preview config is not
// re-resolved against `process.cwd()` when the root is not the cwd.
root: viteConfig.root,
configFile: viteConfig.configFile,
preview: {
port: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pathToFileURL } from 'node:url'
import { basename, extname, join } from 'pathe'
import { basename, extname, join, resolve } from 'pathe'
import { NodeRequest, sendNodeResponse } from 'srvx/node'
import { joinURL } from 'ufo'
import { VITE_ENVIRONMENT_NAMES } from '../../constants'
Expand Down Expand Up @@ -36,7 +36,10 @@ export function previewServerPlugin(): Plugin {

// Get basename without extension and add .js
const outputFilename = `${basename(serverInput, extname(serverInput))}.js`
const serverOutputDir = getServerOutputDirectory(server.config)
const serverOutputDir = resolve(
server.config.root,
getServerOutputDirectory(server.config),
)
const serverEntryPath = join(serverOutputDir, outputFilename)
const imported = await import(
pathToFileURL(serverEntryPath).toString()
Expand Down
27 changes: 27 additions & 0 deletions packages/start-plugin-core/tests/prerender-vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,31 @@ describe('Vite prerender network sink', () => {
'http://127.0.0.1:4173/next/?from=about',
])
})

it('resolves the client output directory and the preview root against the Vite root', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response(null, { status: 200 })),
)
const builder = {
environments: {
ssr: {
config: { root: '/repo/project', configFile: '/vite.config.ts' },
},
client: {
config: { root: '/repo/project', build: { outDir: 'dist/client' } },
},
},
} as any

await prerenderWithVite({
startConfig: { ...makeStartConfig(), pages: [] },
builder,
})

expect(preview).toHaveBeenCalledWith(
expect.objectContaining({ root: '/repo/project' }),
)
expect(process.env.TSS_CLIENT_OUTPUT_DIR).toBe('/repo/project/dist/client')
})
})