Skip to content

Commit

Permalink
fix(history-fallback): properly redirect urls with dot
Browse files Browse the repository at this point in the history
close #130
  • Loading branch information
yyx990803 committed May 13, 2020
1 parent e62473a commit 7f5e459
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</head>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
<script type="module" src="/main.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export const moduleRewritePlugin: ServerPlugin = ({
} else if (html) {
ctx.body = await rewriteIndex(html)
rewriteCache.set(html, ctx.body)
return
}
return
}

// we are doing the js rewrite after all other middlewares have finished;
Expand Down
24 changes: 11 additions & 13 deletions src/node/server/serverPluginServeStatic.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import { ServerPlugin } from '.'

const send = require('koa-send')
Expand Down Expand Up @@ -26,31 +27,28 @@ export const serveStaticPlugin: ServerPlugin = ({
return next()
}

if (ctx.path.includes('.')) {
debug(`not redirecting ${ctx.url} (relative url)`)
return next()
}

if (!ctx.headers || typeof ctx.headers.accept !== 'string') {
const accept = ctx.headers && ctx.headers.accept
if (typeof accept !== 'string') {
debug(`not redirecting ${ctx.url} (no headers.accept)`)
return next()
}

if (ctx.headers.accept.includes('application/json')) {
if (accept.includes('application/json')) {
debug(`not redirecting ${ctx.url} (json)`)
return next()
}

if (
!(
ctx.headers.accept.includes('text/html') ||
ctx.headers.accept.includes('*/*')
)
) {
if (!(accept.includes('text/html') || accept.includes('*/*'))) {
debug(`not redirecting ${ctx.url} (not accepting html)`)
return next()
}

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

debug(`redirecting ${ctx.url} to /index.html`)
ctx.url = '/index.html'
return next()
Expand Down

0 comments on commit 7f5e459

Please sign in to comment.