Skip to content
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
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 @@ -4,6 +4,7 @@ import {
browserLogs,
editFile,
getColor,
getCssRuleBg,
isBuild,
isServe,
listAssets,
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 getCssRuleBg('.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 getCssRuleBg('.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');
}
19 changes: 19 additions & 0 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ export async function getBg(
return el.evaluate((el) => getComputedStyle(el as Element).backgroundImage)
}

/**
* Unlike `getBg`, this function returns the raw value of the `background-image` CSS property.
*
* `getBg` returns the resolved value, which has the hostname and port prepended due to `computedStyle` call.
*/
export async function getCssRuleBg(selector: string): Promise<string> {
return page.evaluate((sel) => {
for (const sheet of document.styleSheets) {
try {
for (const rule of sheet.cssRules) {
if (rule instanceof CSSStyleRule && rule.selectorText === sel) {
return rule.style.backgroundImage
}
}
} catch (_e) {}
}
}, selector)
}

export async function getBgColor(
el: string | ElementHandle | Locator,
): Promise<string> {
Expand Down