Skip to content

Commit 12e709c

Browse files
kakiuwang-uikakiuwang-ui
andauthored
fix(html): don't inline preload link targets (fix #13355) (#23387)
Co-authored-by: kakiuwang-ui <[email protected]>
1 parent 3b8c45b commit 12e709c

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

‎packages/vite/src/node/plugins/html.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,24 @@ const noInlineLinkRels = new Set([
168168
'apple-touch-icon',
169169
'apple-touch-startup-image',
170170
'manifest',
171+
'modulepreload',
172+
'preload',
173+
'prefetch',
171174
])
172175

176+
// If the node is a link, check if it can be inlined. If not, return `false` to
177+
// force no inline. `undefined` leaves it to the default heuristics.
178+
function getLinkShouldInline(
179+
node: DefaultTreeAdapterMap['element'],
180+
attributes: Record<string, string>,
181+
): false | undefined {
182+
const isNoInlineLink =
183+
node.nodeName === 'link' &&
184+
attributes.rel &&
185+
parseRelAttr(attributes.rel).some((v) => noInlineLinkRels.has(v))
186+
return isNoInlineLink ? false : undefined
187+
}
188+
173189
export const isAsyncScriptMap: WeakMap<
174190
ResolvedConfig,
175191
Map<string, boolean>
@@ -636,7 +652,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
636652
decodedUrl !== undefined &&
637653
!isExcludedUrl(decodedUrl)
638654
) {
639-
const result = await processAssetUrl(url)
655+
const result = await processAssetUrl(
656+
url,
657+
getLinkShouldInline(node, attr.attributes),
658+
)
640659
return result !== decodedUrl
641660
? encodeURIPath(result)
642661
: url
@@ -675,20 +694,11 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
675694
})
676695
js += importExpression
677696
} else {
678-
// If the node is a link, check if it can be inlined. If not, set `shouldInline`
679-
// to `false` to force no inline. If `undefined`, it leaves to the default heuristics.
680-
const isNoInlineLink =
681-
node.nodeName === 'link' &&
682-
attr.attributes.rel &&
683-
parseRelAttr(attr.attributes.rel).some((v) =>
684-
noInlineLinkRels.has(v),
685-
)
686-
const shouldInline = isNoInlineLink ? false : undefined
687697
assetUrlsPromises.push(
688698
(async () => {
689699
const processedUrl = await processAssetUrl(
690700
url,
691-
shouldInline,
701+
getLinkShouldInline(node, attr.attributes),
692702
)
693703
if (processedUrl !== url) {
694704
overwriteAttrValue(

‎playground/assets/__tests__/assets.spec.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,36 @@ describe('css url() references', () => {
328328
expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match)
329329
})
330330

331+
test('no base64 inline for modulepreload links', async () => {
332+
const el = await page.$(`link[rel="modulepreload"]`)
333+
const href = await el.getAttribute('href')
334+
expect(href).toMatch(
335+
isBundled
336+
? /\/foo\/bar\/assets\/preload-module-[-\w]{8}\.js/
337+
: 'preload-module.js',
338+
)
339+
})
340+
341+
test('no base64 inline for preload and prefetch links', async () => {
342+
const preloadAssetMatch = isBundled
343+
? /\/foo\/bar\/assets\/preload-asset-[-\w]{8}\.png/
344+
: '/foo/bar/nested/preload-asset.png'
345+
346+
const preloadEl = await page.$('link.preload-href')
347+
expect(await preloadEl.getAttribute('href')).toMatch(preloadAssetMatch)
348+
349+
const prefetchEl = await page.$('link.prefetch-href')
350+
expect(await prefetchEl.getAttribute('href')).toMatch(preloadAssetMatch)
351+
352+
// `imagesrcset` goes through the srcset branch, which has to honour the
353+
// same no-inline decision as `href`
354+
const imageSrcSetEl = await page.$('link.preload-imagesrcset')
355+
const imageSrcSet = await imageSrcSetEl.getAttribute('imagesrcset')
356+
imageSrcSet.split(', ').forEach((s) => {
357+
expect(s).toMatch(preloadAssetMatch)
358+
})
359+
})
360+
331361
test('no base64 inline for icon and manifest links', async () => {
332362
const iconEl = await page.$(`link.ico`)
333363
const href = await iconEl.getAttribute('href')

‎playground/assets/index.html‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
<meta charset="UTF-8" />
55
<link class="ico" rel="icon" type="image/svg+xml" href="favicon.ico" />
66
<link rel="manifest" href="manifest.json" />
7+
<link rel="modulepreload" href="preload-module.js" />
8+
<link
9+
class="preload-href"
10+
rel="preload"
11+
as="image"
12+
href="./nested/preload-asset.png"
13+
/>
14+
<link
15+
class="preload-imagesrcset"
16+
rel="preload"
17+
as="image"
18+
imagesrcset="./nested/preload-asset.png 1x, ./nested/preload-asset.png 2x"
19+
/>
20+
<link
21+
class="prefetch-href"
22+
rel="prefetch"
23+
href="./nested/preload-asset.png"
24+
/>
725
<meta
826
class="meta-og-image"
927
property="og:image"
3.32 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// referenced via <link rel="modulepreload">, small enough to hit assetsInlineLimit
2+
export const preloadedModule = 'preloaded'

0 commit comments

Comments
 (0)