Skip to content

Commit

Permalink
fix(html): transform relative path with long base in /index.html (#10990
Browse files Browse the repository at this point in the history
)

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
fix #10989
  • Loading branch information
candy-Tong committed Nov 22, 2022
1 parent fae0e23 commit 752740c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 14 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import {
asyncFlatten,
Expand All @@ -9,6 +10,7 @@ import {
isFileReadable,
isWindows,
posToNumber,
processSrcSetSync,
resolveHostname,
shouldServe
} from '../utils'
Expand Down Expand Up @@ -270,3 +272,15 @@ describe('isFileReadable', () => {
})
}
})

describe('processSrcSetSync', () => {
test('prepend base URL to srcset', async () => {
const devBase = '/base/'
expect(
processSrcSetSync(
'./nested/asset.png 1x, ./nested/asset.png 2x',
({ url }) => path.posix.join(devBase, url)
)
).toBe('/base/nested/asset.png 1x, /base/nested/asset.png 2x')
})
})
10 changes: 3 additions & 7 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Expand Up @@ -94,20 +94,16 @@ const processNodeUrl = (
const devBase = config.base
if (startsWithSingleSlashRE.test(url)) {
// prefix with base (dev only, base is never relative)
const fullUrl = joinUrlSegments(devBase, url)
const fullUrl = path.posix.join(devBase, url)
overwriteAttrValue(s, sourceCodeLocation, fullUrl)
} else if (
url.startsWith('.') &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html'
) {
const replacer = (url: string) =>
path.posix.join(
devBase,
path.posix.relative(originalUrl, devBase),
url.slice(1)
)
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => path.posix.join(devBase, url)

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

0 comments on commit 752740c

Please sign in to comment.