diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index b504c1c697c8c5..79d1146765889f 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -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('#') diff --git a/playground/backend-integration/__tests__/backend-integration.spec.ts b/playground/backend-integration/__tests__/backend-integration.spec.ts index 7f470d06b08c11..24e0641cb8b8ba 100644 --- a/playground/backend-integration/__tests__/backend-integration.spec.ts +++ b/playground/backend-integration/__tests__/backend-integration.spec.ts @@ -3,6 +3,7 @@ import { browserErrors, browserLogs, editFile, + getBg, getColor, isBuild, isServe, @@ -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( diff --git a/playground/backend-integration/frontend/entrypoints/index.html b/playground/backend-integration/frontend/entrypoints/index.html index bc75dbc4b898e9..bda9a390db0800 100644 --- a/playground/backend-integration/frontend/entrypoints/index.html +++ b/playground/backend-integration/frontend/entrypoints/index.html @@ -25,6 +25,10 @@