Skip to content

Commit

Permalink
Fix MDX style imports when layout is not applied (#4443)
Browse files Browse the repository at this point in the history
* fix: add "astro.needsHeadRendering" to MDX

* test: style imports in pages without layout

* chore: changeset
  • Loading branch information
bholmesdev committed Aug 23, 2022
1 parent ca0c7e8 commit adb2079
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-pens-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---

Fix MDX style imports when layout is not applied
5 changes: 5 additions & 0 deletions packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
// These transforms must happen *after* JSX runtime transformations
transform(code, id) {
if (!id.endsWith('.mdx')) return;

// Ensures styles and scripts are injected into a `<head>`
// When a layout is not applied
code += `\nMDXContent[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout);`;

const [, moduleExports] = parseESM(code);

const { fileUrl, fileId } = getFileInfo(id, config);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import '../styles.css'

# Hello page!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: red;
}
9 changes: 9 additions & 0 deletions packages/integrations/mdx/test/mdx-page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ describe('MDX Page', () => {

expect(h1.textContent).to.equal('Hello page!');
});

it('injects style imports when layout is not applied', async () => {
const html = await fixture.readFile('/index.html');
const { document } = parseHTML(html);

const stylesheet = document.querySelector('link[rel="stylesheet"]');

expect(stylesheet).to.not.be.null;
})
});

describe('dev', () => {
Expand Down

0 comments on commit adb2079

Please sign in to comment.