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:
parent
ca0c7e8b83
commit
adb2079796
5 changed files with 24 additions and 0 deletions
5
.changeset/hungry-pens-develop.md
Normal file
5
.changeset/hungry-pens-develop.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/mdx': patch
|
||||
---
|
||||
|
||||
Fix MDX style imports when layout is not applied
|
|
@ -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);
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
import '../styles.css'
|
||||
|
||||
# Hello page!
|
||||
|
|
3
packages/integrations/mdx/test/fixtures/mdx-page/src/styles.css
vendored
Normal file
3
packages/integrations/mdx/test/fixtures/mdx-page/src/styles.css
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
p {
|
||||
color: red;
|
||||
}
|
|
@ -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', () => {
|
||||
|
|
Loading…
Reference in a new issue