Skip to content

Commit b28db28

Browse files
fix(server): match static aliases on path boundaries (#23643)
Co-authored-by: sapphi-red <[email protected]>
1 parent bc0f21c commit b28db28

2 files changed

Lines changed: 89 additions & 12 deletions

File tree

‎packages/vite/src/node/server/middlewares/__tests__/static.spec.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
11
import { describe, expect, test } from 'vitest'
2-
import { isFileInTargetPath, looksLikeWindowsShortNamePath } from '../static'
2+
import { normalizeAlias } from '../../../utils'
3+
import {
4+
applyStaticAlias,
5+
isFileInTargetPath,
6+
looksLikeWindowsShortNamePath,
7+
} from '../static'
8+
9+
describe('static file aliases', () => {
10+
const cases = {
11+
'prefix sibling': {
12+
pathname: '/images-extra/file.txt',
13+
find: '/images',
14+
replacement: '/root/aliased',
15+
expected: undefined,
16+
},
17+
'nested alias': {
18+
pathname: '/images/file.txt',
19+
find: '/images',
20+
replacement: '/root/aliased',
21+
expected: '/root/aliased/file.txt',
22+
},
23+
'exact alias': {
24+
pathname: '/file.txt',
25+
find: '/file.txt',
26+
replacement: '/root/aliased/file.txt',
27+
expected: '/root/aliased/file.txt',
28+
},
29+
'regex alias': {
30+
pathname: '/regex/file.txt',
31+
find: /^\/regex\//,
32+
replacement: '/root/aliased/',
33+
expected: '/root/aliased/file.txt',
34+
},
35+
'trailing slash find': {
36+
pathname: '/trailing/file.txt',
37+
find: '/trailing/',
38+
replacement: '/root/aliased',
39+
expected: undefined,
40+
},
41+
'normalized trailing slashes': {
42+
pathname: '/normalized/file.txt',
43+
find: '/normalized/',
44+
replacement: '/root/aliased/',
45+
expected: '/root/aliased/file.txt',
46+
},
47+
}
48+
49+
for (const [
50+
name,
51+
{ pathname, find, replacement, expected },
52+
] of Object.entries(cases)) {
53+
test(name, () => {
54+
expect(
55+
applyStaticAlias(pathname, normalizeAlias([{ find, replacement }])),
56+
).toBe(expected)
57+
})
58+
}
59+
60+
test('uses the first matching alias', () => {
61+
const aliases = normalizeAlias([
62+
{ find: '/other', replacement: '/root/other' },
63+
{ find: '/images', replacement: '/root/first' },
64+
{ find: '/images', replacement: '/root/second' },
65+
])
66+
expect(applyStaticAlias('/images/file.txt', aliases)).toBe(
67+
'/root/first/file.txt',
68+
)
69+
})
70+
})
371

472
describe('isFileInTargetPath', () => {
573
const cases = {

‎packages/vite/src/node/server/middlewares/static.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path'
33
import escapeHtml from 'escape-html'
44
import type { Options } from 'sirv'
55
import sirv from 'sirv'
6+
import type { Alias } from '#dep-types/alias'
67
import type { Connect } from '#dep-types/connect'
78
import {
89
cleanUrl,
@@ -158,17 +159,10 @@ export function serveStaticMiddleware(
158159
}
159160

160161
// apply aliases to static requests as well
161-
let redirectedPathname: string | undefined
162-
for (const { find, replacement } of server.config.resolve.alias) {
163-
const matches =
164-
typeof find === 'string'
165-
? pathname.startsWith(find)
166-
: find.test(pathname)
167-
if (matches) {
168-
redirectedPathname = pathname.replace(find, replacement)
169-
break
170-
}
171-
}
162+
let redirectedPathname = applyStaticAlias(
163+
pathname,
164+
server.config.resolve.alias,
165+
)
172166
if (redirectedPathname) {
173167
// dir is pre-normalized to posix style
174168
if (redirectedPathname.startsWith(withTrailingSlash(dir))) {
@@ -198,6 +192,21 @@ export function serveStaticMiddleware(
198192
}
199193
}
200194

195+
export function applyStaticAlias(
196+
pathname: string,
197+
aliases: Alias[],
198+
): string | undefined {
199+
for (const { find, replacement } of aliases) {
200+
const matches =
201+
typeof find === 'string'
202+
? pathname === find || pathname.startsWith(`${find}/`)
203+
: find.test(pathname)
204+
if (matches) {
205+
return pathname.replace(find, replacement)
206+
}
207+
}
208+
}
209+
201210
export function serveRawFsMiddleware(
202211
server: ViteDevServer,
203212
): Connect.NextHandleFunction {

0 commit comments

Comments
 (0)