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
5 changes: 5 additions & 0 deletions .changeset/lucky-kiwis-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where page-level CSS could leak between unrelated pages when traversing style parents across top-level route boundaries
10 changes: 8 additions & 2 deletions packages/astro/src/core/build/plugins/plugin-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ interface PluginOptions {
buildOptions: StaticBuildOptions;
}

function isBuildCssBoundary(id: string, ctx: { getModuleInfo: GetModuleInfo }): boolean {
if (isPropagatedAssetBoundary(id)) return true;
const info = ctx.getModuleInfo(id);
return info ? moduleIsTopLevelPage(info) : false;
}

function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
const { internals, buildOptions } = options;
const { settings } = buildOptions;
Expand Down Expand Up @@ -158,7 +164,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
const parentModuleInfos = getParentExtendedModuleInfos(
scopedToModule,
this,
isPropagatedAssetBoundary,
(moduleId) => isBuildCssBoundary(moduleId, this),
);
for (const { info: pageInfo, depth, order } of parentModuleInfos) {
if (moduleIsTopLevelPage(pageInfo)) {
Expand Down Expand Up @@ -230,7 +236,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
const parentModuleInfos = getParentExtendedModuleInfos(
id,
this,
isPropagatedAssetBoundary,
(importer) => isBuildCssBoundary(importer, this),
);
for (const { info: pageInfo, depth, order } of parentModuleInfos) {
if (isPropagatedAssetBoundary(pageInfo.id)) {
Expand Down
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/i18n-css-leak-basic/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
build: {
inlineStylesheets: 'never',
},
i18n: {
locales: ['en'],
defaultLocale: 'en',
routing: {
redirectToDefaultLocale: false,
},
},
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/i18n-css-leak-basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/i18n-css-leak-basic",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
import { getRelativeLocaleUrl } from 'astro:i18n';

const docsHref = getRelativeLocaleUrl('en', 'docs');
---

<header>
<a href={docsHref}>Docs</a>
</header>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
import '../styles/docs.css';
---

<html>
<head>
<title>Docs</title>
</head>
<body>
<slot />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
import Header from '../components/Header.astro';
import '../styles/site.css';
---

<html>
<head>
<title>Site</title>
</head>
<body>
<Header />
<slot />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import DocsLayout from '../../layouts/DocsLayout.astro';
---

<DocsLayout>
<h1 id="docs-heading">Docs</h1>
</DocsLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import SiteLayout from '../layouts/SiteLayout.astro';
---

<SiteLayout>
<h1 id="site-heading">Home</h1>
</SiteLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
background: black;
}

h1 {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: white;
}
40 changes: 40 additions & 0 deletions packages/astro/test/i18n-css-leak.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('CSS graph boundaries with astro:i18n', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/i18n-css-leak-basic/',
build: { inlineStylesheets: 'never' },
});
await fixture.build();
});

async function getPageCss(pathname) {
const html = await fixture.readFile(pathname);
const $ = cheerioLoad(html);
const hrefs = $('link[rel=stylesheet]')
.map((_index, el) => $(el).attr('href'))
.get();
const stylesheets = await Promise.all(hrefs.map((href) => fixture.readFile(href)));
return stylesheets.join('\n');
}

it('does not attach docs-only CSS to unrelated pages', async () => {
const css = await getPageCss('/index.html');
assert.match(css, /background:#fff/);
assert.doesNotMatch(css, /background:#000/);
assert.doesNotMatch(css, /color:red/);
});

it('keeps docs-only CSS on the docs page', async () => {
const css = await getPageCss('/docs/index.html');
assert.match(css, /background:#000/);
assert.match(css, /color:red/);
assert.doesNotMatch(css, /background:#fff/);
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading