Skip to content

Commit a3e2cae

Browse files
committed
add support for injected page-ssr scripts
1 parent 2a13e43 commit a3e2cae

File tree

7 files changed

+32
-1
lines changed

7 files changed

+32
-1
lines changed

.changeset/great-insects-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/mdx': patch
3+
---
4+
5+
Add support for injected "page-ssr" scripts

packages/astro/test/fixtures/tailwindcss/astro.config.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { defineConfig } from 'astro/config';
22
import tailwind from '@astrojs/tailwind';
3+
import mdx from '@astrojs/mdx';
34

45
// https://astro.build/config
56
export default defineConfig({
67
legacy: {
78
astroFlavoredMarkdown: true,
89
},
9-
integrations: [tailwind()],
10+
integrations: [tailwind(), mdx()],
1011
vite: {
1112
build: {
1213
assetsInlineLimit: 0,

packages/astro/test/fixtures/tailwindcss/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"@astrojs/tailwind": "workspace:*",
7+
"@astrojs/mdx": "workspace:*",
78
"astro": "workspace:*",
89
"autoprefixer": "^10.4.7",
910
"postcss": "^8.4.14",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Button from '../components/Button.astro';
2+
import Complex from '../components/Complex.astro';
3+
4+
<div class="grid place-items-center h-screen content-center">
5+
<Button>Tailwind Button in Markdown!</Button>
6+
<Complex />
7+
</div>

packages/astro/test/tailwindcss.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,13 @@ describe('Tailwind', () => {
6565
const mdBundledCSS = await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/'));
6666
expect(mdBundledCSS, 'includes used component classes').to.match(/\.bg-purple-600{/);
6767
});
68+
69+
it('handles MDX pages (with integration)', async () => {
70+
const html = await fixture.readFile('/mdx-page/index.html');
71+
const $md = cheerio.load(html);
72+
const bundledCSSHREF = $md('link[rel=stylesheet][href^=/assets/]').attr('href');
73+
const mdBundledCSS = await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/'));
74+
expect(mdBundledCSS, 'includes used component classes').to.match(/\.bg-purple-600{/);
75+
});
6876
});
6977
});

packages/integrations/mdx/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
8989
if (!id.endsWith('.mdx')) return;
9090
const [, moduleExports] = parseESM(code);
9191

92+
// This adds support for injected "page-ssr" scripts in MDX files.
93+
// TODO: This should only be happening on page entrypoints, not all imported MDX.
94+
// TODO: This code is copy-pasted across all Astro/Vite plugins that deal with page
95+
// entrypoints (.astro, .md, .mdx). This should be handled in some centralized place,
96+
// or otherwise refactored to not require copy-paste handling logic.
97+
code += `\nimport "${'astro:scripts/page-ssr.js'}";`;
98+
9299
if (!moduleExports.includes('url')) {
93100
const { fileUrl } = getFileInfo(id, config);
94101
code += `\nexport const url = ${JSON.stringify(fileUrl)};`;

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)