Skip to content

Commit 0a1db8c

Browse files
fcbluwy
andauthored
fix: check if build exists so preview doesn't show 404s due to nonexistent build (vitejs#10564)
Co-authored-by: Bjorn Lu <[email protected]>
1 parent f897b64 commit 0a1db8c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/vite/src/node/preview.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs'
12
import path from 'node:path'
23
import type * as http from 'node:http'
34
import sirv from 'sirv'
@@ -83,6 +84,13 @@ export async function preview(
8384
'production',
8485
)
8586

87+
const distDir = path.resolve(config.root, config.build.outDir)
88+
if (!fs.existsSync(distDir)) {
89+
throw new Error(
90+
`"${config.build.outDir}" does not exist. Did you build your project?`
91+
)
92+
}
93+
8694
const app = connect() as Connect.Server
8795
const httpServer = await resolveHttpServer(
8896
config.preview,
@@ -115,7 +123,6 @@ export async function preview(
115123
config.base === './' || config.base === '' ? '/' : config.base
116124

117125
// static assets
118-
const distDir = path.resolve(config.root, config.build.outDir)
119126
const headers = config.preview.headers
120127
const assetServer = sirv(distDir, {
121128
etag: true,

0 commit comments

Comments
 (0)