Skip to content

Commit

Permalink
feat(v2): allow users to specify a custom ssr HTML template
Browse files Browse the repository at this point in the history
  • Loading branch information
mpsq committed Sep 1, 2020
1 parent 0d018a8 commit 27e4f32
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface DocusaurusConfig {
[key: string]: unknown;
}
)[];
ssrTemplate?: string;
stylesheets?: (
| string
| {
Expand Down Expand Up @@ -106,6 +107,7 @@ export interface LoadContext {
siteConfig: DocusaurusConfig;
outDir: string;
baseUrl: string;
ssrTemplate?: string;
}

export interface InjectedHtmlTags {
Expand Down
12 changes: 6 additions & 6 deletions packages/docusaurus/src/client/serverEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ import {
createStatefulLinksCollector,
ProvideLinksCollector,
} from './LinksCollector';
import ssrTemplate from './templates/ssr.html.template';

// eslint-disable-next-line no-restricted-imports
import {memoize} from 'lodash';

const getCompiledSSRTemplate = memoize(() => {
return eta.compile(ssrTemplate.trim(), {
const getCompiledSSRTemplate = memoize((template) => {
return eta.compile(template.trim(), {
rmWhitespace: true,
});
});

function renderSSRTemplate(data) {
const compiled = getCompiledSSRTemplate();
function renderSSRTemplate(ssrTemplate, data) {
const compiled = getCompiledSSRTemplate(ssrTemplate);
return compiled(data, eta.defaultConfig);
}

Expand All @@ -51,6 +50,7 @@ export default async function render(locals) {
postBodyTags,
onLinksCollected,
baseUrl,
ssrTemplate,
} = locals;
const location = routesLocation[locals.path];
await preload(routes, location);
Expand Down Expand Up @@ -90,7 +90,7 @@ export default async function render(locals) {
const stylesheets = (bundles.css || []).map((b) => b.file);
const scripts = (bundles.js || []).map((b) => b.file);

const renderedHtml = renderSSRTemplate({
const renderedHtml = renderSSRTemplate(ssrTemplate, {
appHtml,
baseUrl,
htmlAttributes: htmlAttributes || '',
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const ConfigSchema = Joi.object({
// See https://github.com/facebook/docusaurus/issues/3378
.unknown(),
),
ssrTemplate: Joi.string(),
stylesheets: Joi.array().items(
Joi.string(),
Joi.object({
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {generate} from '@docusaurus/utils';
import path, {join} from 'path';
import ssrDefaultTemplate from '../client/templates/ssr.html.template';
import {
BUILD_DIR_NAME,
CONFIG_FILE_NAME,
Expand Down Expand Up @@ -42,14 +43,15 @@ export function loadContext(
const outDir = customOutDir
? path.resolve(customOutDir)
: path.resolve(siteDir, BUILD_DIR_NAME);
const {baseUrl} = siteConfig;
const {baseUrl, ssrTemplate} = siteConfig;

return {
siteDir,
generatedFilesDir,
siteConfig,
outDir,
baseUrl,
ssrTemplate,
};
}

Expand All @@ -71,7 +73,7 @@ export async function load(
): Promise<Props> {
// Context.
const context: LoadContext = loadContext(siteDir, customOutDir);
const {generatedFilesDir, siteConfig, outDir, baseUrl} = context;
const {generatedFilesDir, siteConfig, outDir, baseUrl, ssrTemplate} = context;

// Plugins.
const pluginConfigs: PluginConfig[] = loadPluginConfigs(context);
Expand Down Expand Up @@ -235,6 +237,7 @@ ${Object.keys(registry)
headTags,
preBodyTags,
postBodyTags,
ssrTemplate: ssrTemplate || ssrDefaultTemplate,
};

return props;
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus/src/webpack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default function createServerConfig({
headTags,
preBodyTags,
postBodyTags,
ssrTemplate,
} = props;
const config = createBaseConfig(props, true, minify);

Expand Down Expand Up @@ -70,6 +71,7 @@ export default function createServerConfig({
preBodyTags,
postBodyTags,
onLinksCollected,
ssrTemplate,
},
paths: ssgPaths,
}),
Expand Down
2 changes: 2 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const path = require('path');
const versions = require('./versions.json');
const ssrTemplate = require('./test').default;

const allDocHomesPaths = [
'/docs/',
Expand Down Expand Up @@ -37,6 +38,7 @@ module.exports = {
description:
'An optimized site generator in React. Docusaurus helps you to move fast and write content. Build documentation websites, blogs, marketing pages, and more.',
},
ssrTemplate,
themes: ['@docusaurus/theme-live-codeblock'],
plugins: [
[
Expand Down

0 comments on commit 27e4f32

Please sign in to comment.