Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function baseMiddleware(
const pathname = cleanUrl(url)
const base = rawBase

if (pathname.startsWith(base)) {
if (pathname === base || pathname.startsWith(withTrailingSlash(base))) {
// rewrite url to remove base. this ensures that other middleware does
// not need to consider base being prepended or not
req.url = stripBase(url, base)
Expand Down
35 changes: 35 additions & 0 deletions playground/base/__tests__/base.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, test } from 'vitest'
import { isBundledDev, isServe, viteTestUrl } from '~utils'

// this playground uses `base: '/foo'` (no trailing slash), so the base
// middleware receives a `rawBase` that does not end with `/`

test('serves the base root as index.html', async () => {
const res = await fetch(viteTestUrl)
expect(res.status).toBe(200)
})

test.runIf(isServe && !isBundledDev)(
'serves a file through the base',
async () => {
const res = await fetch(new URL('/foo/foobar/a.js', viteTestUrl))
expect(res.status).toBe(200)
expect(await res.text()).toContain('foobar-file')
},
)

test('404s a path that only shares a partial segment with the base', async () => {
const res = await fetch(new URL('/foobar/a.js', viteTestUrl))
expect(res.status).toBe(404)
expect(await res.text()).toContain(
'did you mean to visit /foo/foobar/a.js instead?',
)
})

test('404s a path outside the base', async () => {
const res = await fetch(new URL('/not-based.js', viteTestUrl))
expect(res.status).toBe(404)
expect(await res.text()).toContain(
'The server is configured with a public base URL of /foo',
)
})
1 change: 1 addition & 0 deletions playground/base/foobar/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foobar = 'foobar-file'
9 changes: 9 additions & 0 deletions playground/base/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>base playground</title>
</head>
<body>
<div>base playground</div>
</body>
</html>
12 changes: 12 additions & 0 deletions playground/base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@vitejs/test-base",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
}
}
7 changes: 7 additions & 0 deletions playground/base/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vite'

export default defineConfig({
// no trailing slash on purpose: `rawBase` keeps the value as-is, so the
// base middleware must only match whole path segments
base: '/foo',
})
Loading