Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leverage renderToNodeStream #220

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions modules/actions/serveBrowsePage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import { renderToNodeStream } from 'react-dom/server';
import semver from 'semver';

import BrowseApp from '../client/browse/App.js';
import MainTemplate from '../templates/MainTemplate.js';
import asyncHandler from '../utils/asyncHandler.js';
import getScripts from '../utils/getScripts.js';
import { createElement, createHTML } from '../utils/markup.js';
import { createElement } from '../utils/markup.js';
import { getVersionsAndTags } from '../utils/npm.js';

const doctype = '<!DOCTYPE html>';
Expand Down Expand Up @@ -43,27 +43,28 @@ async function serveBrowsePage(req, res) {
filename: req.filename,
target: req.browseTarget
};
const content = createHTML(renderToString(createElement(BrowseApp, data)));
const content = createElement(BrowseApp, data);
const elements = getScripts('browse', 'iife', globalURLs);

const html =
doctype +
renderToStaticMarkup(
createElement(MainTemplate, {
title: `UNPKG - ${req.packageName}`,
description: `The CDN for ${req.packageName}`,
data,
content,
elements
})
);

res
.set({
'Cache-Control': 'public, max-age=14400', // 4 hours
'Cache-Tag': 'browse'
const stream = renderToNodeStream(
createElement(MainTemplate, {
title: `UNPKG - ${req.packageName}`,
description: `The CDN for ${req.packageName}`,
data,
content,
elements
})
.send(html);
);

res.set({
'Content-Type': 'text/html',
'Cache-Control': 'public, max-age=14400', // 4 hours
'Cache-Tag': 'browse'
});

res.write(doctype);

stream.pipe(res);
}

export default asyncHandler(serveBrowsePage);
27 changes: 15 additions & 12 deletions modules/actions/serveMainPage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import { renderToNodeStream } from 'react-dom/server';

import MainApp from '../client/main/App.js';
import MainTemplate from '../templates/MainTemplate.js';
import getScripts from '../utils/getScripts.js';
import { createElement, createHTML } from '../utils/markup.js';
import { createElement } from '../utils/markup.js';

const doctype = '<!DOCTYPE html>';
const globalURLs =
Expand All @@ -20,17 +20,20 @@ const globalURLs =
};

export default function serveMainPage(req, res) {
const content = createHTML(renderToString(createElement(MainApp)));
const content = createElement(MainApp);
const elements = getScripts('main', 'iife', globalURLs);

const html =
doctype +
renderToStaticMarkup(createElement(MainTemplate, { content, elements }));
const stream = renderToNodeStream(
createElement(MainTemplate, { content, elements })
);

res
.set({
'Cache-Control': 'public, max-age=14400', // 4 hours
'Cache-Tag': 'main'
})
.send(html);
res.set({
'Content-Type': 'text/html',
'Cache-Control': 'public, max-age=14400', // 4 hours
'Cache-Tag': 'main'
});

res.write(doctype);

stream.pipe(res);
}
6 changes: 3 additions & 3 deletions modules/templates/MainTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ gtag('config', 'UA-140352188-1');`),
favicon && e('link', { rel: 'shortcut icon', href: favicon }),
e('title', null, title),
x(promiseShim),
x(fetchShim),
data && x(`window.__DATA__ = ${encodeJSONForScript(data)}`)
x(fetchShim)
),
e(
'body',
null,
e('div', { id: 'root', dangerouslySetInnerHTML: content }),
e('div', { id: 'root' }, content),
data && x(`window.__DATA__ = ${encodeJSONForScript(data)}`),
...elements
)
);
Expand Down