Skip to content

Commit

Permalink
fix: do not error on failed load for SPA html requests
Browse files Browse the repository at this point in the history
close #2051
  • Loading branch information
yyx990803 committed Feb 17, 2021
1 parent 3deba82 commit 44a30d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ export function transformMiddleware(
}

// resolve, load and transform using the plugin container
const result = await transformRequest(url, server)
const result = await transformRequest(url, server, {
html: req.headers.accept?.includes('text/html')
})
if (result) {
const type = isDirectCSSRequest(url) ? 'css' : 'js'
const isDep =
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface TransformResult {

export interface TransformOptions {
ssr?: boolean
html?: boolean
}

export async function transformRequest(
Expand Down Expand Up @@ -69,6 +70,11 @@ export async function transformRequest(
code = await fs.readFile(file, 'utf-8')
isDebug && debugLoad(`${timeFrom(loadStart)} [fs] ${prettyUrl}`)
} catch (e) {
// if this is an html request and there is no load result, skip ahead to
// SPA fallback.
if (options.html) {
return null
}
if (e.code !== 'ENOENT') {
throw e
}
Expand Down

0 comments on commit 44a30d5

Please sign in to comment.