Skip to content

Commit adb2079

Browse files
authored
Fix MDX style imports when layout is not applied (#4443)
* fix: add "astro.needsHeadRendering" to MDX * test: style imports in pages without layout * chore: changeset
1 parent ca0c7e8 commit adb2079

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

.changeset/hungry-pens-develop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/mdx': patch
3+
---
4+
5+
Fix MDX style imports when layout is not applied

packages/integrations/mdx/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
119119
// These transforms must happen *after* JSX runtime transformations
120120
transform(code, id) {
121121
if (!id.endsWith('.mdx')) return;
122+
123+
// Ensures styles and scripts are injected into a `<head>`
124+
// When a layout is not applied
125+
code += `\nMDXContent[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout);`;
126+
122127
const [, moduleExports] = parseESM(code);
123128

124129
const { fileUrl, fileId } = getFileInfo(id, config);
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
import '../styles.css'
2+
13
# Hello page!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p {
2+
color: red;
3+
}

packages/integrations/mdx/test/mdx-page.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ describe('MDX Page', () => {
2626

2727
expect(h1.textContent).to.equal('Hello page!');
2828
});
29+
30+
it('injects style imports when layout is not applied', async () => {
31+
const html = await fixture.readFile('/index.html');
32+
const { document } = parseHTML(html);
33+
34+
const stylesheet = document.querySelector('link[rel="stylesheet"]');
35+
36+
expect(stylesheet).to.not.be.null;
37+
})
2938
});
3039

3140
describe('dev', () => {

0 commit comments

Comments
 (0)