Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if build exists so preview doesn't show 404s due to nonexistent build #10564

Merged
merged 5 commits into from Dec 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -78,6 +79,13 @@ export async function preview(
): Promise<PreviewServer> {
const config = await resolveConfig(inlineConfig, 'serve', '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 @@ -110,7 +118,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
app.use(
previewBase,
Expand Down