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: processNodeUrl for srcset #14870

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 29 additions & 27 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,43 +114,45 @@ const processNodeUrl = (
originalUrl?: string,
server?: ViteDevServer,
): string | undefined => {
if (server?.moduleGraph) {
const mod = server.moduleGraph.urlToModuleMap.get(url)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => {
if (server?.moduleGraph) {
const mod = server.moduleGraph.urlToModuleMap.get(url)
if (mod && mod.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
}
}
}

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.
//
// skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace.
//
// rewrite `./index.js` -> `localhost:5173/a/index.js`.
// rewrite `../index.js` -> `localhost:5173/index.js`.
// rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
((url[0] === '.' || (wordCharRE.test(url[0]) && !url.includes(':'))) &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html')
) {
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => {
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.
//
// skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace.
//
// rewrite `./index.js` -> `localhost:5173/a/index.js`.
// rewrite `../index.js` -> `localhost:5173/index.js`.
// rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
((url[0] === '.' || (wordCharRE.test(url[0]) && !url.includes(':'))) &&
originalUrl &&
originalUrl !== '/' &&
htmlPath === '/index.html')
) {
const devBase = config.base
const fullUrl = path.posix.join(devBase, url)
if (server && shouldPreTransform(url, config)) {
preTransformRequest(server, fullUrl, devBase)
}
return fullUrl
} else {
return url
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved

const processedUrl = useSrcSetReplacer
? processSrcSetSync(url, ({ url }) => replacer(url))
: replacer(url)
return processedUrl
}

const processedUrl = useSrcSetReplacer
? processSrcSetSync(url, ({ url }) => replacer(url))
: replacer(url)
return processedUrl
}
const devHtmlHook: IndexHtmlTransformHook = async (
html,
Expand Down
12 changes: 12 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ describe('image', () => {
expect(s).toMatch(/\/foo\/bar\/icon\.png \dx/)
})
})

// TODO: fix build
test.runIf(!isBuild)('srcset (mixed)', async () => {
const img = await page.$('.img-src-set-mixed')
const srcset = await img.getAttribute('srcset')
const srcs = srcset.split(', ')
expect(srcs[1]).toMatch(
isBuild
? /\/foo\/bar\/assets\/asset-[-\w]{8}\.png \dx/
: /\/foo\/bar\/nested\/asset.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 @@ -151,6 +151,12 @@ <h2>Image Src Set</h2>
srcset="/icon.png 1x, /icon.png 2x"
alt=""
/>
<img
class="img-src-set-mixed"
src="/icon.png"
srcset="https://vitejs.dev/logo-with-shadow.png 1x, ./nested/asset.png 2x"
alt=""
/>
</div>

<h2>HTML only asset</h2>
Expand Down