Skip to content

Commit

Permalink
fix: revert trailing slash handling + improve dev base usage
Browse files Browse the repository at this point in the history
fix #1664
  • Loading branch information
yyx990803 committed Jan 23, 2021
1 parent 2824d06 commit 01e9ac0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/playground/html/index.html
@@ -1,7 +1,7 @@
<link rel="stylesheet" href="./main.css" />
<link rel="stylesheet" href="/main.css" />

<!-- comment one -->
<h1>Hello</h1>
<!-- comment two -->

<script type="module" src="./main.js"></script>
<script type="module" src="/main.js"></script>
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/index.ts
Expand Up @@ -406,7 +406,12 @@ export async function createServer(
{
from: /\/$/,
to({ parsedUrl }: any) {
return parsedUrl.pathname + 'index.html'
const rewritten = parsedUrl.pathname + 'index.html'
if (fs.existsSync(path.join(root, rewritten))) {
return rewritten
} else {
return `/index.html`
}
}
}
]
Expand Down
17 changes: 14 additions & 3 deletions packages/vite/src/node/server/middlewares/base.ts
Expand Up @@ -15,13 +15,24 @@ export function baseMiddleware({
const path = parsed.pathname || '/'

if (path.startsWith(base)) {
// rewrite url to remove base.. this ensures that other middleware does not need to consider base being prepended or not
// rewrite url to remove base.. this ensures that other middleware does
// not need to consider base being prepended or not
req.url = url.replace(base, '/')
} else if (path === '/' || path === '/index.html') {
// to prevent confusion, do not allow access at / if we have specified a base path
res.statusCode = 404
// redirect root visit to based url
res.writeHead(302, {
Location: base
})
res.end()
return
} else if (req.headers.accept?.includes('text/html')) {
// non-based page visit
res.statusCode = 404
res.end(
`The server is configured with a public base URL of ${base} - ` +
`did you mean to visit ${base}${url.slice(1)} instead?`
)
return
}

next()
Expand Down

0 comments on commit 01e9ac0

Please sign in to comment.