Skip to content

Commit

Permalink
fix(html): srcset pointing image in public dir wasn't working during …
Browse files Browse the repository at this point in the history
…dev (#14663)
  • Loading branch information
sapphi-red authored Oct 17, 2023
1 parent 3c16b71 commit 4496ae7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
30 changes: 12 additions & 18 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,28 @@ const processNodeUrl = (
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
}
}
const devBase = config.base
if (url[0] === '/' && url[1] !== '/') {
// prefix with base (dev only, base is never relative)
const fullUrl = path.posix.join(devBase, url)
if (server && shouldPreTransform(url, config)) {
preTransformRequest(server, fullUrl, devBase)
}
return fullUrl
} else if (
(url[0] === '.' || startsWithWordCharRE.test(url)) &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html'

if (
(url[0] === '/' && url[1] !== '/') ||
// #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.
// rewrite before `./index.js` -> `localhost:5173/a/index.js`.
// rewrite after `../index.js` -> `localhost:5173/index.js`.
((url[0] === '.' || startsWithWordCharRE.test(url)) &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html')
) {
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => {
const devBase = config.base
const fullUrl = path.posix.join(devBase, url)
if (server && shouldPreTransform(url, config)) {
preTransformRequest(server, fullUrl, devBase)
}
return fullUrl
}

// #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.
// rewrite before `./index.js` -> `localhost:5173/a/index.js`.
// rewrite after `../index.js` -> `localhost:5173/index.js`.

const processedUrl = useSrcSetReplacer
? processSrcSetSync(url, ({ url }) => replacer(url))
: replacer(url)
Expand Down
16 changes: 6 additions & 10 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,23 +682,19 @@ interface ImageCandidate {
}
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g
const imageSetUrlRE = /^(?:[\w\-]+\(.*?\)|'.*?'|".*?"|\S*)/
function reduceSrcset(ret: { url: string; descriptor: string }[]) {
return ret.reduce((prev, { url, descriptor }, index) => {
descriptor ??= ''
return (prev +=
url + ` ${descriptor}${index === ret.length - 1 ? '' : ', '}`)
}, '')
function joinSrcset(ret: ImageCandidate[]) {
return ret.map(({ url, descriptor }) => `${url} ${descriptor}`).join(', ')
}

function splitSrcSetDescriptor(srcs: string): ImageCandidate[] {
return splitSrcSet(srcs)
.map((s) => {
const src = s.replace(escapedSpaceCharacters, ' ').trim()
const [url] = imageSetUrlRE.exec(src) || ['']
const url = imageSetUrlRE.exec(src)?.[0] ?? ''

return {
url,
descriptor: src?.slice(url.length).trim(),
descriptor: src.slice(url.length).trim(),
}
})
.filter(({ url }) => !!url)
Expand All @@ -713,14 +709,14 @@ export function processSrcSet(
url: await replacer({ url, descriptor }),
descriptor,
})),
).then((ret) => reduceSrcset(ret))
).then(joinSrcset)
}

export function processSrcSetSync(
srcs: string,
replacer: (arg: ImageCandidate) => string,
): string {
return reduceSrcset(
return joinSrcset(
splitSrcSetDescriptor(srcs).map(({ url, descriptor }) => ({
url: replacer({ url, descriptor }),
descriptor,
Expand Down
8 changes: 8 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ describe('image', () => {
)
})
})

test('srcset (public)', async () => {
const img = await page.$('.img-src-set-public')
const srcset = await img.getAttribute('srcset')
srcset.split(', ').forEach((s) => {
expect(s).toMatch(/\/foo\/bar\/icon\.png \dx/)
})
})
})

describe('svg fragments', () => {
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ <h2>Image Src Set</h2>
srcset="./nested/asset.png 1x, ./nested/asset.png 2x"
alt=""
/>
<img
class="img-src-set-public"
src="/icon space.png"
srcset="/icon.png 1x, /icon.png 2x"
alt=""
/>
</div>

<h2>SVG Fragments</h2>
Expand Down

0 comments on commit 4496ae7

Please sign in to comment.