diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index 3de3f9d79e..ec9d8fd757 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -55,6 +55,7 @@ "express": "^4.17.1", "fs-extra": "^8.1.0", "globby": "^10.0.1", + "html-minifier-terser": "^5.0.2", "html-tags": "^3.1.0", "html-webpack-plugin": "^4.0.0-beta.11", "import-fresh": "^3.2.1", diff --git a/packages/docusaurus/src/client/serverEntry.js b/packages/docusaurus/src/client/serverEntry.js index 0e936541f9..e6cbaa3289 100644 --- a/packages/docusaurus/src/client/serverEntry.js +++ b/packages/docusaurus/src/client/serverEntry.js @@ -13,6 +13,7 @@ import {Helmet} from 'react-helmet'; import {getBundles} from 'react-loadable-ssr-addon'; import Loadable from 'react-loadable'; +import {minify} from 'html-minifier-terser'; import path from 'path'; import fs from 'fs-extra'; import routes from '@generated/routes'; @@ -68,7 +69,7 @@ export default async function render(locals) { const scripts = (bundles.js || []).map(b => b.file); const {baseUrl} = locals; - return ejs.render( + const renderedHtml = ejs.render( ssrTemplate.trim(), { appHtml, @@ -87,4 +88,17 @@ export default async function render(locals) { rmWhitespace: true, }, ); + + // minify html with https://github.com/DanielRuf/html-minifier-terser + return minify(renderedHtml, { + collapseWhitespace: true, + removeComments: true, + removeRedundantAttributes: true, + removeEmptyAttributes: true, + removeScriptTypeAttributes: true, + removeStyleLinkTypeAttributes: true, + useShortDoctype: true, + minifyJS: true, + minifyCSS: true, + }); } diff --git a/yarn.lock b/yarn.lock index af61e867d7..16396e6f9f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7553,7 +7553,7 @@ html-entities@^1.2.1: resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-1.2.1.tgz#0df29351f0721163515dfb9e5543e5f6eed5162f" integrity sha1-DfKTUfByEWNRXfueVUPl9u7VFi8= -html-minifier-terser@^5.0.1: +html-minifier-terser@^5.0.1, html-minifier-terser@^5.0.2: version "5.0.2" resolved "https://registry.yarnpkg.com/html-minifier-terser/-/html-minifier-terser-5.0.2.tgz#0e67a0b062ae1dd0719fc73199479298f807ae16" integrity sha512-VAaitmbBuHaPKv9bj47XKypRhgDxT/cDLvsPiiF7w+omrN3K0eQhpigV9Z1ilrmHa9e0rOYcD6R/+LCDADGcnQ==