Skip to content

Commit 752740c

Browse files
authoredNov 22, 2022
fix(html): transform relative path with long base in /index.html (#10990)
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> fix #10989
1 parent fae0e23 commit 752740c

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed
 

‎packages/vite/src/node/__tests__/utils.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs'
2+
import path from 'node:path'
23
import { describe, expect, test } from 'vitest'
34
import {
45
asyncFlatten,
@@ -9,6 +10,7 @@ import {
910
isFileReadable,
1011
isWindows,
1112
posToNumber,
13+
processSrcSetSync,
1214
resolveHostname,
1315
shouldServe
1416
} from '../utils'
@@ -270,3 +272,15 @@ describe('isFileReadable', () => {
270272
})
271273
}
272274
})
275+
276+
describe('processSrcSetSync', () => {
277+
test('prepend base URL to srcset', async () => {
278+
const devBase = '/base/'
279+
expect(
280+
processSrcSetSync(
281+
'./nested/asset.png 1x, ./nested/asset.png 2x',
282+
({ url }) => path.posix.join(devBase, url)
283+
)
284+
).toBe('/base/nested/asset.png 1x, /base/nested/asset.png 2x')
285+
})
286+
})

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,16 @@ const processNodeUrl = (
9494
const devBase = config.base
9595
if (startsWithSingleSlashRE.test(url)) {
9696
// prefix with base (dev only, base is never relative)
97-
const fullUrl = joinUrlSegments(devBase, url)
97+
const fullUrl = path.posix.join(devBase, url)
9898
overwriteAttrValue(s, sourceCodeLocation, fullUrl)
9999
} else if (
100100
url.startsWith('.') &&
101101
originalUrl &&
102102
originalUrl !== '/' &&
103103
htmlPath === '/index.html'
104104
) {
105-
const replacer = (url: string) =>
106-
path.posix.join(
107-
devBase,
108-
path.posix.relative(originalUrl, devBase),
109-
url.slice(1)
110-
)
105+
// prefix with base (dev only, base is never relative)
106+
const replacer = (url: string) => path.posix.join(devBase, url)
111107

112108
// #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
113109
// path will add `/a/` prefix, it will caused 404.

0 commit comments

Comments
 (0)
Please sign in to comment.