Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the Markdown component in SSR #3036

Merged
merged 5 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SSRManifest as Manifest, RouteInfo } from './types';
import type { LogOptions } from '../logger/core.js';

import mime from 'mime';
import astroRemark from '@astrojs/markdown-remark';
import { consoleLogDestination } from '../logger/console.js';
export { deserializeManifest } from './common.js';
import { matchRoute } from '../routing/match.js';
Expand All @@ -19,7 +20,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 Expand Up @@ -82,7 +82,7 @@ export class App {
legacyBuild: false,
links,
logging: this.#logging,
markdownRender: manifest.markdown.render,
markdownRender: [astroRemark, this.#manifest.markdown ?? {}],
mod,
origin: url.origin,
pathname: url.pathname,
Expand Down
6 changes: 2 additions & 4 deletions packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
AstroUserConfig,
RouteData,
SerializedRouteData,
MarkdownRenderOptions,
ComponentInstance,
SSRLoadedRenderer,
} from '../../@types/astro';
Expand All @@ -22,9 +22,7 @@ export type SerializedRouteInfo = Omit<RouteInfo, 'routeData'> & {
export interface SSRManifest {
routes: RouteInfo[];
site?: string;
markdown: {
render: MarkdownRenderOptions;
};
markdown: AstroUserConfig['markdown'];
pageMap: Map<ComponentPath, ComponentInstance>;
renderers: SSRLoadedRenderer[];
entryModules: Record<string, string>;
Expand Down
5 changes: 1 addition & 4 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import astroRemark from '@astrojs/markdown-remark';
import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from './internal.js';
import type { AstroAdapter } from '../../@types/astro';
Expand Down Expand Up @@ -110,9 +109,7 @@ function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): Ser
const ssrManifest: SerializedSSRManifest = {
routes,
site: astroConfig.site,
markdown: {
render: [astroRemark, astroConfig.markdown],
},
markdown: astroConfig.markdown,
pageMap: null as any,
renderers: [],
entryModules,
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 @@ -175,7 +175,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 @@ -410,7 +410,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
42 changes: 42 additions & 0 deletions packages/astro/test/ssr-markdown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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:https://example.com' + path);
debugger;
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);
});
});
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.