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
This commit is contained in:
Ben Holmes 2022-08-23 13:25:35 -04:00 committed by GitHub
parent ca0c7e8b83
commit adb2079796
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---
Fix MDX style imports when layout is not applied

View file

@ -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);

View file

@ -1 +1,3 @@
import '../styles.css'
# Hello page!

View file

@ -0,0 +1,3 @@
p {
color: red;
}

View file

@ -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', () => {