Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): transform relative path with long base in /index.html #10990

Merged
merged 6 commits into from
Nov 22, 2022
Merged
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
14 changes: 14 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import {
asyncFlatten,
@@ -9,6 +10,7 @@ import {
isFileReadable,
isWindows,
posToNumber,
processSrcSetSync,
resolveHostname,
shouldServe
} from '../utils'
@@ -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
Original file line number Diff line number Diff line change
@@ -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.