Skip to content

Commit

Permalink
fix: add vitestUiHtmlMiddleware after viteHostCheckMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 22, 2025
1 parent 3604f2e commit 7f91828
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/ui/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export default (ctx: Vitest): Plugin => {
const clientDist = resolve(fileURLToPath(import.meta.url), '../client')
const clientIndexHtml = fs.readFileSync(resolve(clientDist, 'index.html'), 'utf-8')

server.middlewares.use(
base,
sirv(clientDist, {
dev: true,
extensions: [], // don't serve index.html
}),
)

// serve index.html with api token
// eslint-disable-next-line prefer-arrow-callback
server.middlewares.use(function vitestUiHtmlMiddleware(req, res, next) {
Expand All @@ -75,13 +83,15 @@ export default (ctx: Vitest): Plugin => {
next()
})

server.middlewares.use(
base,
sirv(clientDist, {
single: true,
dev: true,
}),
)
return () => {
// move vitestUiHtmlMiddleware after viteHostCheckMiddleware if exists
const i1 = server.middlewares.stack.findIndex(h => 'name' in h.handle && h.handle.name === 'vitestUiHtmlMiddleware')
const i2 = server.middlewares.stack.findIndex(h => 'name' in h.handle && h.handle.name === 'viteHostCheckMiddleware')
if (i1 !== -1 && i2 !== -1 && i1 < i2) {
const [item] = server.middlewares.stack.splice(i1, 1)
server.middlewares.stack.splice(i2, 0, item)
}
}
},
},
}
Expand Down

0 comments on commit 7f91828

Please sign in to comment.