Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 14 additions & 10 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ const noInlineLinkRels = new Set([
'apple-touch-icon',
'apple-touch-startup-image',
'manifest',
'modulepreload',
'preload',
'prefetch',
])

export const isAsyncScriptMap: WeakMap<
Expand Down Expand Up @@ -622,6 +625,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
config.html?.additionalAssetSources,
)
for (const attr of assetAttributes) {
// If the node is a link, check if it can be inlined. If not, set `shouldInline`
// to `false` to force no inline. If `undefined`, it leaves to the default heuristics.
const isNoInlineLink =
node.nodeName === 'link' &&
attr.attributes.rel &&
parseRelAttr(attr.attributes.rel).some((v) =>
noInlineLinkRels.has(v),
)
const shouldInline = isNoInlineLink ? false : undefined
Comment on lines +628 to +636

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract this as an outer function so that we only call it when needed? Some blocks don't seem to need this.


if (attr.type === 'remove') {
s.remove(attr.location.startOffset, attr.location.endOffset)
continue
Expand All @@ -636,7 +649,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
decodedUrl !== undefined &&
!isExcludedUrl(decodedUrl)
) {
const result = await processAssetUrl(url)
const result = await processAssetUrl(url, shouldInline)
return result !== decodedUrl
? encodeURIPath(result)
: url
Expand Down Expand Up @@ -675,15 +688,6 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
})
js += importExpression
} else {
// If the node is a link, check if it can be inlined. If not, set `shouldInline`
// to `false` to force no inline. If `undefined`, it leaves to the default heuristics.
const isNoInlineLink =
node.nodeName === 'link' &&
attr.attributes.rel &&
parseRelAttr(attr.attributes.rel).some((v) =>
noInlineLinkRels.has(v),
)
const shouldInline = isNoInlineLink ? false : undefined
assetUrlsPromises.push(
(async () => {
const processedUrl = await processAssetUrl(
Expand Down
30 changes: 30 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,36 @@ describe('css url() references', () => {
expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match)
})

test('no base64 inline for modulepreload links', async () => {
const el = await page.$(`link[rel="modulepreload"]`)
const href = await el.getAttribute('href')
expect(href).toMatch(
isBundled
? /\/foo\/bar\/assets\/preload-module-[-\w]{8}\.js/
: 'preload-module.js',
)
})

test('no base64 inline for preload and prefetch links', async () => {
const preloadAssetMatch = isBundled
? /\/foo\/bar\/assets\/preload-asset-[-\w]{8}\.png/
: '/foo/bar/nested/preload-asset.png'

const preloadEl = await page.$('link.preload-href')
expect(await preloadEl.getAttribute('href')).toMatch(preloadAssetMatch)

const prefetchEl = await page.$('link.prefetch-href')
expect(await prefetchEl.getAttribute('href')).toMatch(preloadAssetMatch)

// `imagesrcset` goes through the srcset branch, which has to honour the
// same no-inline decision as `href`
const imageSrcSetEl = await page.$('link.preload-imagesrcset')
const imageSrcSet = await imageSrcSetEl.getAttribute('imagesrcset')
imageSrcSet.split(', ').forEach((s) => {
expect(s).toMatch(preloadAssetMatch)
})
})

test('no base64 inline for icon and manifest links', async () => {
const iconEl = await page.$(`link.ico`)
const href = await iconEl.getAttribute('href')
Expand Down
18 changes: 18 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
<meta charset="UTF-8" />
<link class="ico" rel="icon" type="image/svg+xml" href="favicon.ico" />
<link rel="manifest" href="manifest.json" />
<link rel="modulepreload" href="preload-module.js" />
<link
class="preload-href"
rel="preload"
as="image"
href="./nested/preload-asset.png"
/>
<link
class="preload-imagesrcset"
rel="preload"
as="image"
imagesrcset="./nested/preload-asset.png 1x, ./nested/preload-asset.png 2x"
/>
<link
class="prefetch-href"
rel="prefetch"
href="./nested/preload-asset.png"
/>
<meta
class="meta-og-image"
property="og:image"
Expand Down
Binary file added playground/assets/nested/preload-asset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions playground/assets/preload-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// referenced via <link rel="modulepreload">, small enough to hit assetsInlineLimit
export const preloadedModule = 'preloaded'