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

Commit

Permalink
Defer all scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mxstbr committed Oct 13, 2017
1 parent d99082f commit 9d88392
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
8 changes: 6 additions & 2 deletions iris/renderer/get-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import serialize from 'serialize-javascript';
const html = fs
.readFileSync(path.resolve(__dirname, '..', '..', 'build', 'index.html'))
.toString()
.replace(/<script.+?bootstrap\.js\".+?<\/script>/, '');
.replace(/<script.+?bootstrap\.js\".+?<\/script>/, '')
.replace(/(<script)(.+?bundle.+?<\/script>)/, '$1 defer="defer" $2');

type Arguments = {
styleTags: string,
Expand All @@ -16,7 +17,10 @@ type Arguments = {
scriptTags: string,
};

const sentry = `<script defer src="https://cdn.ravenjs.com/3.14.0/raven.min.js" crossorigin="anonymous"></script><script>Raven.config('https://[email protected]/154812', { whitelistUrls: [/spectrum.chat/, /www.spectrum.chat/] }).install();</script>`;
export const createScriptTag = ({ src }: { src: string }) =>
`<script defer="defer" src="${src}"></script>`;

const sentry = `<script defer="defer" src="https://cdn.ravenjs.com/3.14.0/raven.min.js" crossorigin="anonymous"></script><script>Raven.config('https://[email protected]/154812', { whitelistUrls: [/spectrum.chat/, /www.spectrum.chat/] }).install();</script>`;

export const getHTML = ({
styleTags,
Expand Down
28 changes: 14 additions & 14 deletions iris/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import stats from '../../build/react-loadable.json';
import getSharedApolloClientOptions from 'shared/graphql/apollo-client-options';
import schema from '../schema';
import createLoaders from '../loaders';
import { getHTML } from './get-html';
import { getHTML, createScriptTag } from './get-html';

// Browser shim has to come before any client imports
import './browser-shim';
Expand Down Expand Up @@ -96,8 +96,19 @@ const renderer = (req, res) => {
} else {
res.status(200);
}
const bundles = getBundles(stats, modules);
const bundles = getBundles(stats, modules)
// Create <script defer> tags from bundle objects
.map(bundle =>
createScriptTag({ src: `/${bundle.file.replace(/\.map$/, '')}` })
)
// Make sure only unique bundles are included
.filter((value, index, self) => self.indexOf(value) === index);
debug('compile and send html');
const scriptTags = [
createScriptTag({ src: '/static/js/bootstrap.js' }),
...bundles,
].join('\n');
debug(`script tags: ${scriptTags}`);
// Compile the HTML and send it down
res.send(
getHTML({
Expand All @@ -108,18 +119,7 @@ const renderer = (req, res) => {
helmet.title.toString() +
helmet.meta.toString() +
helmet.link.toString(),
scriptTags: [
'<script src="/static/js/bootstrap.js"></script>',
...bundles
.map(
bundle =>
`<script src="/${bundle.file.replace(
/\.map$/,
''
)}"></script>`
)
.filter((value, index, self) => self.indexOf(value) === index),
].join('\n'),
scriptTags,
})
);
res.end();
Expand Down

0 comments on commit 9d88392

Please sign in to comment.