Skip to content

Commit

Permalink
fix: check if build exists so preview doesn't show 404s due to nonexi…
Browse files Browse the repository at this point in the history
…stent build (#10564)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
  • Loading branch information
fc and bluwy committed Dec 10, 2022
1 parent f897b64 commit 0a1db8c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/vite/src/node/preview.ts
@@ -1,3 +1,4 @@
import fs from 'node:fs'
import path from 'node:path'
import type * as http from 'node:http'
import sirv from 'sirv'
Expand Down Expand Up @@ -83,6 +84,13 @@ export async function preview(
'production',
)

const distDir = path.resolve(config.root, config.build.outDir)
if (!fs.existsSync(distDir)) {
throw new Error(
`"${config.build.outDir}" does not exist. Did you build your project?`
)
}

const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
config.preview,
Expand Down Expand Up @@ -115,7 +123,6 @@ export async function preview(
config.base === './' || config.base === '' ? '/' : config.base

// static assets
const distDir = path.resolve(config.root, config.build.outDir)
const headers = config.preview.headers
const assetServer = sirv(distDir, {
etag: true,
Expand Down

0 comments on commit 0a1db8c

Please sign in to comment.