Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: $ vite preview 404 handling (#7665)
  • Loading branch information
brillout committed Apr 12, 2022
1 parent dfce283 commit 66b6dc5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/vite/src/node/preview.ts
@@ -1,4 +1,5 @@
import path from 'path'
import fs from 'fs'
import sirv from 'sirv'
import connect from 'connect'
import compression from './server/middlewares/compression'
Expand Down Expand Up @@ -90,11 +91,20 @@ export async function preview(
config.base,
sirv(distDir, {
etag: true,
dev: true,
single: true
dev: true
})
)

app.use(config.base, (_, res, next) => {
const file = path.join(distDir, './404.html')
if (fs.existsSync(file)) {
res.statusCode = 404
res.end(fs.readFileSync(file))
} else {
next()
}
})

const options = config.preview
const hostname = resolveHostname(options.host)
const port = options.port ?? 4173
Expand Down

0 comments on commit 66b6dc5

Please sign in to comment.