Skip to content

Commit

Permalink
Support the Markdown component in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Apr 8, 2022
1 parent cde2c44 commit db77891
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
adapter: AstroAdapter | undefined;
renderers: AstroRenderer[];
scripts: { stage: InjectedScriptStage; content: string }[];
projectFlags: number;
};
}

Expand Down
15 changes: 13 additions & 2 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { eachPageData } from './internal.js';
import { addRollupInput } from './add-rollup-input.js';
import { virtualModuleId as pagesVirtualModuleId } from './vite-plugin-pages.js';
import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { hasUserConfig } from '../project-flags.js';

export const virtualModuleId = '@astrojs-ssr-virtual-entry';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
Expand All @@ -34,11 +35,21 @@ export function vitePluginSSR(
load(id) {
if (id === resolvedVirtualModuleId) {
return `import * as adapter from '${adapter.serverEntrypoint}';
import _astroRemark from '@astrojs/markdown-remark';
import * as _main from '${pagesVirtualModuleId}';
import { deserializeManifest as _deserializeManifest } from 'astro/app';
const _manifest = Object.assign(_deserializeManifest('${manifestReplace}'), {
${hasUserConfig(buildOpts.astroConfig) ?
`import _astroConfig from '/astro.config';` :
`const _astroConfig = undefined;`
}
const _markdownRender = _astroConfig?.markdown?.render ? _astroConfig.markdown.render[0] : _astroRemark;
let _manifest = _deserializeManifest('${manifestReplace}');
Object.assign(_manifest, {
pageMap: _main.pageMap,
renderers: _main.renderers
renderers: _main.renderers,
markdown: {
render: [_markdownRender, _manifest.markdown.render[1]],
},
});
const _args = ${adapter.args ? JSON.stringify(adapter.args) : 'undefined'};
Expand Down
19 changes: 13 additions & 6 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import loadTypeScript from '@proload/plugin-tsm';
import postcssrc from 'postcss-load-config';
import { arraify, isObject } from './util.js';
import { appendForwardSlash, trimSlashes } from './path.js';
import { flags as projectFlags } from './project-flags.js';

load.use([loadTypeScript]);

Expand Down Expand Up @@ -175,7 +176,8 @@ export const AstroConfigSchema = z.object({
export async function validateConfig(
userConfig: any,
root: string,
cmd: string
cmd: string,
hasUserConfig: boolean
): Promise<AstroConfig> {
const fileProtocolRoot = pathToFileURL(root + path.sep);
// Manual deprecation checks
Expand Down Expand Up @@ -277,8 +279,11 @@ export async function validateConfig(
// First-Pass Validation
const result = {
...(await AstroConfigRelativeSchema.parseAsync(userConfig)),
_ctx: { scripts: [], renderers: [], adapter: undefined },
_ctx: { scripts: [], renderers: [], adapter: undefined, projectFlags: 0 },
};
if(hasUserConfig) {
result._ctx.projectFlags |= projectFlags.USER_CONFIG;
}
// Final-Pass Validation (perform checks that require the full config object)
if (
!result.experimental?.integrations &&
Expand Down Expand Up @@ -386,7 +391,7 @@ export async function loadConfig(configOptions: LoadConfigOptions): Promise<Astr

// Automatically load config file using Proload
// If `userConfigPath` is `undefined`, Proload will search for `astro.config.[cm]?[jt]s`
let config;
let config, hasUserConfig = false;
try {
config = await load('astro', {
mustExist: !!userConfigPath,
Expand All @@ -400,20 +405,22 @@ export async function loadConfig(configOptions: LoadConfigOptions): Promise<Astr
throw err;
}
if (config) {
hasUserConfig = true;
userConfig = config.value;
}
return resolveConfig(userConfig, root, flags, configOptions.cmd);
return resolveConfig(userConfig, root, flags, configOptions.cmd, hasUserConfig);
}

/** Attempt to resolve an Astro configuration object. Normalize, validate, and return. */
export async function resolveConfig(
userConfig: AstroUserConfig,
root: string,
flags: CLIFlags = {},
cmd: string
cmd: string,
hasUserConfig: boolean
): Promise<AstroConfig> {
const mergedConfig = mergeCLIFlags(userConfig, flags, cmd);
const validatedConfig = await validateConfig(mergedConfig, root, cmd);
const validatedConfig = await validateConfig(mergedConfig, root, cmd, hasUserConfig);

return validatedConfig;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/src/core/project-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { AstroConfig } from '../@types/astro';

export const flags = {
USER_CONFIG: 1 << 0
};

export function hasFlag(config: AstroConfig, flag: number): boolean {
return !!(config._ctx.projectFlags & flag);
}

export function hasUserConfig(config: AstroConfig): boolean {
return hasFlag(config, flags.USER_CONFIG);
}
5 changes: 5 additions & 0 deletions packages/astro/test/fixtures/ssr-markdown/fastro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'astro/config';

export default defineConfig({

});
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
43 changes: 43 additions & 0 deletions packages/astro/test/ssr-markdown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';
import testAdapter from './test-adapter.js';

// Asset bundling
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);
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.only('Renders the Markdown component correctly', async () => {
const html = await fetchHTML('/page');
console.log(html);
const $ = cheerioLoad(html);
});
});
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.

0 comments on commit db77891

Please sign in to comment.