Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit d97fa3c

Browse files
committed
Improve TTFB by responding asap
1 parent 208b984 commit d97fa3c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

iris/renderer/get-html.js

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export const getHTML = ({ styleTags, metaTags, state, content }: Arguments) => {
2020
const sanitizedState = JSON.stringify(state).replace(/</g, '\\u003c');
2121
return (
2222
html
23+
// Get everything after <head>
24+
.split('<head>')[1]
2325
// Inject the state and the content instead of <div id="root">
2426
.replace(
2527
'<div id="root"></div>',
@@ -29,3 +31,8 @@ export const getHTML = ({ styleTags, metaTags, state, content }: Arguments) => {
2931
.replace('</head>', `${metaTags}${styleTags}</head>`)
3032
);
3133
};
34+
35+
export const getStart = () => {
36+
// Get everything before (and including) <head>
37+
return html.substr(0, html.indexOf('<head>') + '<head>'.length);
38+
};

iris/renderer/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as graphql from 'graphql';
1717

1818
import schema from '../schema';
1919
import createLoaders from '../loaders';
20-
import { getHTML } from './get-html';
20+
import { getHTML, getStart } from './get-html';
2121

2222
// Browser shim has to come before any client imports
2323
import './browser-shim';
@@ -58,6 +58,7 @@ const renderer = (req, res) => {
5858
</StaticRouter>
5959
</ApolloProvider>
6060
);
61+
res.write(getStart());
6162
// Initialise the styled-components stylesheet and wrap the app with it
6263
const sheet = new ServerStyleSheet();
6364
renderToStringWithData(sheet.collectStyles(frontend))
@@ -72,7 +73,7 @@ const renderer = (req, res) => {
7273
const helmet = Helmet.renderStatic();
7374
// Compile the HTML and send it down
7475
res.status(200);
75-
res.send(
76+
res.write(
7677
getHTML({
7778
content,
7879
state,

0 commit comments

Comments
 (0)