Skip to content

Commit

Permalink
fix: fix history fallback redirect for html files
Browse files Browse the repository at this point in the history
fix #160
  • Loading branch information
yyx990803 committed May 18, 2020
1 parent 0cc57c8 commit d16f567
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ export const moduleRewritePlugin: ServerPlugin = ({
const scriptRE = /(<script\b[^>]*>)([\s\S]*?)<\/script>/gm
const srcRE = /\bsrc=(?:"([^"]+)"|'([^']+)'|([^'"\s]+)\b)/

async function rewriteIndex(html: string) {
async function rewriteIndex(importer: string, html: string) {
await initLexer
let hasInjectedDevFlag = false
const importer = '/index.html'
return html!.replace(scriptRE, (matched, openTag, script) => {
const devFlag = hasInjectedDevFlag ? `` : devInjectionCode
hasInjectedDevFlag = true
Expand Down Expand Up @@ -94,13 +93,13 @@ export const moduleRewritePlugin: ServerPlugin = ({
return
}

if (ctx.path === '/index.html') {
if (ctx.path.endsWith('.html')) {
let html = await readBody(ctx.body)
if (html && rewriteCache.has(html)) {
debug('/index.html: serving from cache')
debug(`${ctx.path}: serving from cache`)
ctx.body = rewriteCache.get(html)
} else if (html) {
ctx.body = await rewriteIndex(html)
ctx.body = await rewriteIndex(ctx.path, html)
rewriteCache.set(html, ctx.body)
}
return
Expand Down
5 changes: 5 additions & 0 deletions src/node/server/serverPluginServeStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export const serveStaticPlugin: ServerPlugin = ({
}

const ext = path.extname(ctx.path)
if (ext === '.html') {
debug(`not redirecting ${ctx.url} (is html page)`)
return next()
}

if (ext && !accept.includes('text/html')) {
debug(`not redirecting ${ctx.url} (has file extension)`)
return next()
Expand Down

0 comments on commit d16f567

Please sign in to comment.