Skip to content

Commit

Permalink
Fix asset propagation regression in 3.5 (#9069)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Nov 12, 2023
1 parent 44bfc9b commit 50164f5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-parrots-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fix a regression introduced in 3.5.0 related to content collection styles
2 changes: 1 addition & 1 deletion packages/astro/src/content/vite-plugin-content-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function astroConfigBuildPlugin(
const pageData = getPageDataByViteID(internals, pageViteID);
if (!pageData) continue;

const _entryCss = internals.propagatedStylesMap?.get(id);
const _entryCss = pageData.propagatedStyles?.get(id);
const _entryScripts = pageData.propagatedScripts?.get(id);
if (_entryCss) {
for (const value of _entryCss) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function astroContentVirtualModPlugin({
hydratedComponents: [],
clientOnlyComponents: [],
scripts: [],
containsHead: true,
containsHead: false,
propagation: 'in-tree',
pageOptions: {},
},
Expand Down
20 changes: 13 additions & 7 deletions packages/astro/src/core/build/plugins/plugin-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
const pagesToCss: Record<string, Record<string, { order: number; depth: number }>> = {};
const pagesToPropagatedCss: Record<string, Record<string, Set<string>>> = {};

const isContentCollectionCache = options.buildOptions.settings.config.output === 'static' && options.buildOptions.settings.config.experimental.contentCollectionCache;

const cssBuildPlugin: VitePlugin = {
name: 'astro:rollup-plugin-build-css',

Expand Down Expand Up @@ -93,10 +95,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
// so they can be injected where needed
const chunkId = assetName.createNameHash(id, [id]);
internals.cssModuleToChunkIdMap.set(id, chunkId);
if (
options.buildOptions.settings.config.output === 'static' &&
options.buildOptions.settings.config.experimental.contentCollectionCache
) {
if (isContentCollectionCache) {
// TODO: Handle inlining?
const propagatedStyles =
internals.propagatedStylesMap.get(pageInfo.id) ?? new Set();
Expand Down Expand Up @@ -251,9 +250,16 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
// return early if the stylesheet needing propagation has already been included
if (pageData.styles.some((s) => s.sheet === sheet)) return;

const propagatedStyles =
internals.propagatedStylesMap.get(pageInfoId) ??
internals.propagatedStylesMap.set(pageInfoId, new Set()).get(pageInfoId)!;
let propagatedStyles: Set<StylesheetAsset>;
if (isContentCollectionCache) {
propagatedStyles =
internals.propagatedStylesMap.get(pageInfoId) ??
internals.propagatedStylesMap.set(pageInfoId, new Set()).get(pageInfoId)!;
} else {
propagatedStyles =
pageData.propagatedStyles.get(pageInfoId) ??
pageData.propagatedStyles.set(pageInfoId, new Set()).get(pageInfoId)!;
}

propagatedStyles.add(sheet);
sheetAddedToPage = true;
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ describe('Content Collections', () => {
});
});

describe('Propagation', () => {
it('Applies styles', async () => {
const html = await fixture.readFile('/propagation/index.html');
const $ = cheerio.load(html);
expect($('style').text()).to.include('content:"works!"');
});
})

describe('Entry', () => {
let json;
before(async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ describe('astro:image', () => {
await fixture.build();
});

it('dynamic route images are built at response time sss', async () => {
it('dynamic route images are built at response time', async () => {
const app = await fixture.loadTestAdapterApp();
let request = new Request('http://example.com/');
let response = await app.render(request);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
import { getCollection } from "astro:content";
const posts = await getCollection("with-schema-config");
---

<html>
<body>
<div class="foo">
<div>Hello World</div>
<span>Styles?</span>
</div>
</body>
</html>

<style>
.foo {
background-color: blue;
}
span::after {
content: "works!";
}
</style>

0 comments on commit 50164f5

Please sign in to comment.