Skip to content

fix(assets): ensure ?no-inline is not included in the asset url in the production environment #19496

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

Merged
merged 2 commits into from
Apr 23, 2025
Merged
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
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ async function fileToBuiltUrl(
return cached
}

const { file, postfix } = splitFileAndPostfix(id)
let { file, postfix } = splitFileAndPostfix(id)
const content = await fsp.readFile(file)

let url: string
Expand All @@ -401,6 +401,11 @@ async function fileToBuiltUrl(
originalFileName,
source: content,
})

if (environment.config.command === 'build' && noInlineRE.test(postfix)) {
postfix = postfix.replace(noInlineRE, '').replace(/^&/, '?')
}

url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
}

Expand Down
10 changes: 9 additions & 1 deletion playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,19 @@ test('?raw import', async () => {
test('?no-inline svg import', async () => {
expect(await page.textContent('.no-inline-svg')).toMatch(
isBuild
? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg\?no-inline/
? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg/
: '/foo/bar/nested/fragment.svg?no-inline',
)
})

test('?no-inline svg import -- multiple postfix', async () => {
expect(await page.textContent('.no-inline-svg-mp')).toMatch(
isBuild
? /\/foo\/bar\/assets\/fragment-[-\w]{8}\.svg\?foo=bar/
: '/foo/bar/nested/fragment.svg?no-inline&foo=bar',
)
})

test('?inline png import', async () => {
expect(await page.textContent('.inline-png')).toMatch(
/^data:image\/png;base64,/,
Expand Down
6 changes: 6 additions & 0 deletions playground/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ <h2>?raw import</h2>
<h2>?no-inline svg import</h2>
<code class="no-inline-svg"></code>

<h2>?no-inline svg import -- multiple postfix</h2>
<code class="no-inline-svg-mp"></code>

<h2>?inline png import</h2>
<code class="inline-png"></code>

Expand Down Expand Up @@ -546,6 +549,9 @@ <h3>assets in template</h3>
import noInlineSvg from './nested/fragment.svg?no-inline'
text('.no-inline-svg', noInlineSvg)

import noInlineSvgMP from './nested/fragment.svg?no-inline&foo=bar'
text('.no-inline-svg-mp', noInlineSvgMP)

import inlinePng from './nested/asset.png?inline'
text('.inline-png', inlinePng)

Expand Down