Skip to content

Commit

Permalink
feat(v2): allow users to specify a custom ssr HTML template (facebook…
Browse files Browse the repository at this point in the history
…#3387)

* feat(v2): allow users to specify a custom ssr HTML template

* docs(v2): add ssrTemplate property to docusaurus.config.js page
  • Loading branch information
mpsq committed Sep 2, 2020
1 parent 9857f7b commit 3ace600
Show file tree
Hide file tree
Showing 6 changed files with 60 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 @@ -110,6 +111,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 @@ -43,14 +44,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 @@ -72,7 +74,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 @@ -237,6 +239,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
44 changes: 44 additions & 0 deletions website/docs/api/docusaurus.config.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,50 @@ module.exports = {
};
```

### `ssrTemplate`

An HTML template that will be used to render your application. This can be used to set custom attributes on the `body` tags, additional `meta` tags, customize the `viewport`, etc. Please note that Docusaurus will rely on the template to be correctly structured in order to function properly, once you do customize it, you will have to make sure that your template is compliant with the requirements from `upstream`.

- Type: `string`

Example:

```js title="docusaurus.config.js"
module.exports = {
ssrTemplate: `<!DOCTYPE html>
<html <%~ it.htmlAttributes %>>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=0.86, maximum-scale=3.0, minimum-scale=0.86">
<meta name="generator" content="Docusaurus v<%= it.version %>">
<%~ it.headTags %>
<% it.metaAttributes.forEach((metaAttribute) => { %>
<%~ metaAttribute %>
<% }); %>
<% it.stylesheets.forEach((stylesheet) => { %>
<link rel="stylesheet" type="text/css" href="<%= it.baseUrl %><%= stylesheet %>" />
<% }); %>
<% it.scripts.forEach((script) => { %>
<link rel="preload" href="<%= it.baseUrl %><%= script %>" as="script">
<% }); %>
</head>
<body <%~ it.bodyAttributes %> itemscope="" itemtype="http://schema.org/Organization">
<%~ it.preBodyTags %>
<div id="__docusaurus">
<%~ it.appHtml %>
</div>
<div id="outside-docusaurus">
<span>Custom markup</span>
</div>
<% it.scripts.forEach((script) => { %>
<script type="text/javascript" src="<%= it.baseUrl %><%= script %>"></script>
<% }); %>
<%~ it.postBodyTags %>
</body>
</html>
};
```

### `stylesheets`

An array of CSS sources to load. The values can be either strings or plain objects of attribute-value maps. The `<link>` tags will be inserted in the HTML `<head>`.
Expand Down

0 comments on commit 3ace600

Please sign in to comment.