Skip to content

Commit

Permalink
fix: $ vite preview 404 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Apr 11, 2022
1 parent eb57627 commit 209c337
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -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 209c337

Please sign in to comment.