Skip to content

Commit

Permalink
fix: correctly handle head propagation in content layer deferred rend…
Browse files Browse the repository at this point in the history
…ering (#12014)

* chore: run MDX tests against content layer

* Handle head propagation in deferred rendering

* Add changeset

* Update test
  • Loading branch information
ascorbic authored Sep 18, 2024
1 parent 8214114 commit 53cb41e
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-points-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where component styles were not correctly included in rendered MDX
13 changes: 6 additions & 7 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,12 @@ export async function renderEntry(
try {
// @ts-expect-error virtual module
const { default: contentModules } = await import('astro:content-module-imports');
const module = contentModules.get(entry.filePath);
const deferredMod = await module();
return {
Content: deferredMod.Content,
headings: deferredMod.getHeadings?.() ?? [],
remarkPluginFrontmatter: deferredMod.frontmatter ?? {},
};
const renderEntryImport = contentModules.get(entry.filePath);
return render({
collection: '',
id: entry.id,
renderEntryImport,
});
} catch (e) {
// eslint-disable-next-line
console.error(e);
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ export function astroContentVirtualModPlugin({
const [, query] = id.split('?');
const params = new URLSearchParams(query);
const fileName = params.get('fileName');
let importerPath = undefined;
let importPath = undefined;
if (fileName && URL.canParse(fileName, settings.config.root.toString())) {
importerPath = fileURLToPath(new URL(fileName, settings.config.root));
importPath = fileURLToPath(new URL(fileName, settings.config.root))
}
if (importerPath) {
return await this.resolve(importerPath);
if (importPath) {
return await this.resolve(`${importPath}?${CONTENT_RENDER_FLAG}`);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/integrations/mdx/test/css-head-mdx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Head injection w/ MDX', () => {
integrations: [mdx()],
// test suite was authored when inlineStylesheets defaulted to never
build: { inlineStylesheets: 'never' },
experimental: { contentLayer: true }
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
import { getEntryBySlug } from 'astro:content';
import { getEntry, render } from 'astro:content';
const launchWeek = await getEntryBySlug('blog', 'using-mdx');
const { Content } = await launchWeek.render();
const launchWeek = await getEntry('blog', 'using-mdx');
const { Content } = await render(launchWeek);
---

<Content />
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineCollection } from "astro:content";
import { glob } from "astro/loaders"

const posts = defineCollection({
loader: glob({
pattern: "*.mdx",
base: "src/data/posts",
})
});

const blog = defineCollection({
loader: glob({
pattern: "*.mdx",
base: "src/data/blog",
})
});

export const collections = { posts, blog };
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---
import { getCollection } from 'astro:content';
import { getCollection, render } from 'astro:content';
import SmallCaps from '../../components/SmallCaps.astro';
import Layout from '../../layouts/ContentLayout.astro';
export async function getStaticPaths() {
const entries = await getCollection('posts');
return entries.map(entry => {
return {params: { post: entry.slug }, props: { entry },
}});
return { params: { post: entry.id }, props: { entry }};
});
}
const { entry } = Astro.props;
const { Content } = await entry.render();
const { Content } = await render(entry);
---
<Layout title="">
<Content components={{ SmallCaps }} />
Expand Down

0 comments on commit 53cb41e

Please sign in to comment.