Skip to content

Commit

Permalink
Support the Markdown component in SSR (#3036)
Browse files Browse the repository at this point in the history
* Support the Markdown component in SSR

* Adds a changeset

* Support runtime markdown in Node.js

* Remove option from test adapter
  • Loading branch information
matthewp authored Apr 14, 2022
1 parent 1b6cb6c commit 4ac0d5d
Show file tree
Hide file tree
Showing 14 changed files with 712 additions and 507 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-mayflies-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Support runtime markdown parsing
5 changes: 5 additions & 0 deletions .changeset/wild-llamas-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes usage of the Markdown component in SSR
1 change: 0 additions & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
createModuleScriptElementWithSrcSet,
} from '../render/ssr-element.js';
import { prependForwardSlash } from '../path.js';
import { createRequest } from '../request.js';

export class App {
#manifest: Manifest;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class AstroBuilder {
client: new URL('./client/', this.config.outDir),
server: new URL('./server/', this.config.outDir),
serverEntry: 'entry.mjs',
staticMode: undefined,
staticMode: undefined
};
await runHookBuildStart({ config: this.config, buildConfig });

Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
export const virtualModuleId = '@astrojs-ssr-virtual-entry';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@';
const replaceExp = new RegExp(`['"](${manifestReplace})['"]`, 'g');

export function vitePluginSSR(
buildOpts: StaticBuildOptions,
Expand Down Expand Up @@ -64,18 +65,16 @@ if(_start in adapter) {
}
return void 0;
},

generateBundle(_opts, bundle) {
const manifest = buildManifest(buildOpts, internals);

for (const [_chunkName, chunk] of Object.entries(bundle)) {
if (chunk.type === 'asset') continue;
if (chunk.modules[resolvedVirtualModuleId]) {
const exp = new RegExp(`['"]${manifestReplace}['"]`);
const code = chunk.code;
chunk.code = code.replace(exp, () => {
chunk.code = code.replace(replaceExp, () => {
return JSON.stringify(manifest);
});
})
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const AstroConfigSchema = z.object({
export async function validateConfig(
userConfig: any,
root: string,
cmd: string
cmd: string,
): Promise<AstroConfig> {
const fileProtocolRoot = pathToFileURL(root + path.sep);
// Manual deprecation checks
Expand Down Expand Up @@ -433,7 +433,7 @@ export async function resolveConfig(
userConfig: AstroUserConfig,
root: string,
flags: CLIFlags = {},
cmd: string
cmd: string,
): Promise<AstroConfig> {
const mergedConfig = mergeCLIFlags(userConfig, flags, cmd);
const validatedConfig = await validateConfig(mergedConfig, root, cmd);
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-markdown/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-markdown",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>Testing</head>
<body>
<article>
<slot />
</article>
</body>
</html>
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/ssr-markdown/src/pages/page.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
import { Markdown } from 'astro/components';
---

<html>
<head><title>Testing</title></head>
<body>
<Markdown>
# Something

else here
</Markdown>
</body>
</html>
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/ssr-markdown/src/pages/post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
layout: ../layouts/Base.astro
---

# Hello world

This is some test

## Subheading
41 changes: 41 additions & 0 deletions packages/astro/test/ssr-markdown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

describe('Markdown pages in SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-markdown/',
experimental: {
ssr: true,
},
adapter: testAdapter(),
});
await fixture.build();
});

async function fetchHTML(path) {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com' + path);
const response = await app.render(request);
const html = await response.text();
return html;
}


it('Renders markdown pages correctly', async () => {
const html = await fetchHTML('/post');
const $ = cheerioLoad(html);
expect($('#subheading').text()).to.equal('Subheading');
});

it('Renders the Markdown component correctly', async () => {
const html = await fetchHTML('/page');
const $ = cheerioLoad(html);
expect($('#something')).to.have.lengthOf(1);
});
});
2 changes: 1 addition & 1 deletion packages/astro/test/test-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function () {
serverEntrypoint: '@my-ssr',
exports: ['manifest', 'createApp'],
});
},
}
},
};
}
2 changes: 1 addition & 1 deletion packages/integrations/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function createIntegration(): AstroIntegration {
hooks: {
'astro:config:done': ({ setAdapter }) => {
setAdapter(getAdapter());
},
}
},
};
}
Loading

0 comments on commit 4ac0d5d

Please sign in to comment.