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
6 changes: 5 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,11 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
if (encodePublicUrlsInCSS(config)) {
return [publicFileToBuiltUrl(decodedUrl, config), undefined]
} else {
return [joinUrlSegments(config.base, decodedUrl), undefined]
const base = joinUrlSegments(
config.server.origin ?? '',
config.base,
)
return [joinUrlSegments(base, decodedUrl), undefined]
}
}
const [id, fragment] = decodedUrl.split('#')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
browserErrors,
browserLogs,
editFile,
getBg,
getColor,
isBuild,
isServe,
Expand Down Expand Up @@ -120,6 +121,20 @@ describe.runIf(isServe)('serve', () => {
expect(await link.getAttribute('href')).toContain('/dev/global.css?t=')
})

test('server.origin is applied to non-public CSS url()', async () => {
const bg = await getBg('.outside-root--aliased')
expect(bg).toContain(
`http://localhost:${ports['backend-integration']}/dev/`,
)
})

test('server.origin is applied to public CSS url()', async () => {
const bg = await getBg('.public-asset')
expect(bg).toContain(
`http://localhost:${ports['backend-integration']}/dev/icon.png`,
)
})

test('CSS dependencies are tracked for HMR', async () => {
const el = await page.$('h1')
await untilBrowserLogAfter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ <h2>CSS Asset References</h2>
Background URL with Relative Path:
<div class="background-asset outside-root--relative"></div>
</li>
<li>
Background URL with Public Asset:
<div class="background-asset public-asset"></div>
</li>
</ul>

<h2>CSS imported from JS</h2>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions playground/backend-integration/frontend/styles/background.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
.outside-root--relative {
background-image: url('../images/logo.png');
}

.public-asset {
background-image: url('/icon.png');
}
Loading