Skip to content

Commit

Permalink
Add <doctype HTML> to HTML pages (#566)
Browse files Browse the repository at this point in the history
* Add doctype to static rendering

* Set content-type for CSS
  • Loading branch information
yangshun committed Apr 15, 2018
1 parent 0593ea0 commit 946e2ce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
18 changes: 9 additions & 9 deletions lib/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function execute() {
const CWD = process.cwd();
const fs = require('fs-extra');
const readMetadata = require('./readMetadata.js');
const renderToStaticMarkup = require('react-dom/server').renderToStaticMarkup;
const path = require('path');
const color = require('color');
const toSlug = require('../core/toSlug.js');
Expand All @@ -28,6 +27,7 @@ function execute() {
const join = path.join;
const sep = path.sep;
const escapeStringRegexp = require('escape-string-regexp');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');

// create the folder path for a file if it does not exist, then write the file
function writeFileAndCreateFolder(file, content) {
Expand Down Expand Up @@ -180,7 +180,7 @@ function execute() {
{rawContent}
</DocsLayout>
);
const str = renderToStaticMarkup(docComp);
const str = renderToStaticMarkupWithDoctype(docComp);

const targetFile = join(buildDir, metadata.permalink);
writeFileAndCreateFolder(targetFile, str);
Expand All @@ -198,7 +198,7 @@ function execute() {
redirect={siteConfig.baseUrl + metadata.permalink}
/>
);
const redirectStr = renderToStaticMarkup(redirectComp);
const redirectStr = renderToStaticMarkupWithDoctype(redirectComp);

// create a redirects page for doc files
const redirectFile = join(
Expand Down Expand Up @@ -264,7 +264,7 @@ function execute() {
{rawContent}
</BlogPostLayout>
);
const str = renderToStaticMarkup(blogPostComp);
const str = renderToStaticMarkupWithDoctype(blogPostComp);

let targetFile = join(buildDir, 'blog', filePath);
writeFileAndCreateFolder(targetFile, str);
Expand All @@ -282,7 +282,7 @@ function execute() {
config={siteConfig}
/>
);
const str = renderToStaticMarkup(blogPageComp);
const str = renderToStaticMarkupWithDoctype(blogPageComp);

let targetFile = join(
buildDir,
Expand Down Expand Up @@ -456,7 +456,7 @@ function execute() {
continue;
}
translate.setLanguage(language);
const str = renderToStaticMarkup(
const str = renderToStaticMarkupWithDoctype(
<Site
language={language}
config={siteConfig}
Expand All @@ -474,7 +474,7 @@ function execute() {
// write to base level
let language = env.translation.enabled ? 'en' : '';
translate.setLanguage(language);
const str = renderToStaticMarkup(
const str = renderToStaticMarkupWithDoctype(
<Site language={language} config={siteConfig} metadata={{id: pageID}}>
<ReactComp language={language} />
</Site>
Expand All @@ -487,7 +487,7 @@ function execute() {
// allow for rendering of other files not in pages/en folder
let language = env.translation.enabled ? 'en' : '';
translate.setLanguage(language);
const str = renderToStaticMarkup(
const str = renderToStaticMarkupWithDoctype(
<Site language={language} config={siteConfig} metadata={{id: pageID}}>
<ReactComp language={language} />
</Site>
Expand All @@ -499,7 +499,7 @@ function execute() {
const pageID = path.basename(normalizedFile, '.html');
const parts = normalizedFile.split('pages');
const targetFile = join(buildDir, parts[1]);
const str = renderToStaticMarkup(
const str = renderToStaticMarkupWithDoctype(
<Site language="en" config={siteConfig} metadata={{id: pageID}}>
<div
dangerouslySetInnerHTML={{
Expand Down
21 changes: 21 additions & 0 deletions lib/server/renderUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

const renderToStaticMarkup = require('react-dom/server').renderToStaticMarkup;

/**
* Custom function that wraps renderToStaticMarkup so that we can inject
* doctype before React renders the contents. All instance of full-page
* rendering within Docusaurus should use this function instead.
*/
function renderToStaticMarkupWithDoctype(...args) {
return '<!DOCTYPE html>' + renderToStaticMarkup(...args);
}

module.exports = {
renderToStaticMarkupWithDoctype,
};
27 changes: 13 additions & 14 deletions lib/server/server.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

function execute(port) {
const extractTranslations = require('../write-translations.js');
const extractTranslations = require('../write-translations');

const env = require('./env.js');
const translation = require('./translation.js');
const translation = require('./translation');
const express = require('express');
const React = require('react');
const request = require('request');
const renderToStaticMarkup = require('react-dom/server').renderToStaticMarkup;
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const color = require('color');
const toSlug = require('../core/toSlug.js');
const toSlug = require('../core/toSlug');
const mkdirp = require('mkdirp');
const glob = require('glob');
const chalk = require('chalk');
const translate = require('./translate.js');
const translate = require('./translate');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');

const feed = require('./feed.js');
const sitemap = require('./sitemap.js');
// const sitemap = require("sitemap");
const feed = require('./feed');
const sitemap = require('./sitemap');

const CWD = process.cwd();

Expand Down Expand Up @@ -250,7 +248,7 @@ function execute(port) {
</DocsLayout>
);

res.send(renderToStaticMarkup(docComp));
res.send(renderToStaticMarkupWithDoctype(docComp));
});

app.get('/sitemap.xml', function(req, res) {
Expand Down Expand Up @@ -293,7 +291,7 @@ function execute(port) {
config={siteConfig}
/>
);
const str = renderToStaticMarkup(blogPageComp);
const str = renderToStaticMarkupWithDoctype(blogPageComp);

let path = (page > 0 ? 'page' + (page + 1) : '') + '/index.html';
blogPages[path] = str;
Expand Down Expand Up @@ -344,7 +342,7 @@ function execute(port) {
{rawContent}
</BlogPostLayout>
);
res.send(renderToStaticMarkup(blogPostComp));
res.send(renderToStaticMarkupWithDoctype(blogPostComp));
}
});

Expand All @@ -365,7 +363,7 @@ function execute(port) {
if (siteConfig.wrapPagesHTML) {
removeModuleAndChildrenFromCache(join('..', 'core', 'Site.js'));
const Site = require(join('..', 'core', 'Site.js'));
const str = renderToStaticMarkup(
const str = renderToStaticMarkupWithDoctype(
<Site
language="en"
config={siteConfig}
Expand Down Expand Up @@ -436,7 +434,7 @@ function execute(port) {
removeModuleAndChildrenFromCache(join('..', 'core', 'Site.js'));
const Site = require(join('..', 'core', 'Site.js'));
translate.setLanguage(language);
const str = renderToStaticMarkup(
const str = renderToStaticMarkupWithDoctype(
<Site
language={language}
config={siteConfig}
Expand Down Expand Up @@ -507,6 +505,7 @@ function execute(port) {
});
}

res.header('Content-Type', 'text/css');
res.send(cssContent);
});

Expand Down

0 comments on commit 946e2ce

Please sign in to comment.