Skip to content

Commit 1d48f75

Browse files
committed
load babel only when needed
1 parent 9c68ee4 commit 1d48f75

File tree

5 files changed

+43
-20
lines changed

5 files changed

+43
-20
lines changed

src/html.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ if (process.env.NODE_ENV === `production`) {
2020
}
2121
}
2222

23-
const JS_NPM_URLS = [
24-
'//unpkg.com/docsearch.js@2.4.1/dist/cdn/docsearch.min.js',
25-
'//unpkg.com/babel-standalone@6.26.0/babel.min.js',
26-
];
27-
2823
export default class HTML extends Component {
2924
render() {
3025
let css;
@@ -37,8 +32,6 @@ export default class HTML extends Component {
3732
);
3833
}
3934

40-
const js = JS_NPM_URLS.map(url => <script key={url} src={url} />);
41-
4235
return (
4336
<html lang="en">
4437
<head>
@@ -50,7 +43,6 @@ export default class HTML extends Component {
5043
/>
5144
<link rel="icon" href="/favicon.ico" />
5245
{this.props.headComponents}
53-
{js}
5446
{css}
5547
</head>
5648
<body>

src/layouts/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Flex from 'components/Flex';
1919
import Footer from 'components/LayoutFooter';
2020
import Header from 'components/LayoutHeader';
2121
import {media} from 'theme';
22+
import loadScript from 'utils/loadScript';
23+
import {docsearchURL} from 'site-constants';
2224

2325
// Import global styles
2426
import '../prism-styles';
@@ -28,13 +30,15 @@ import 'css/algolia.css';
2830

2931
class Template extends Component {
3032
componentDidMount() {
31-
// Initialize Algolia search.
32-
// TODO Is this expensive? Should it be deferred until a user is about to search?
33-
// eslint-disable-next-line no-undef
34-
docsearch({
35-
apiKey: '36221914cce388c46d0420343e0bb32e',
36-
indexName: 'react',
37-
inputSelector: '#algolia-doc-search',
33+
loadScript(docsearchURL).then(() => {
34+
// Initialize Algolia search.
35+
// TODO Is this expensive? Should it be deferred until a user is about to search?
36+
// eslint-disable-next-line no-undef
37+
docsearch({
38+
apiKey: '36221914cce388c46d0420343e0bb32e',
39+
indexName: 'react',
40+
inputSelector: '#algolia-doc-search',
41+
});
3842
});
3943
}
4044

src/site-constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@
1414
// the SSR part in node.js we won't have a proper location.
1515
const urlRoot = 'https://reactjs.org';
1616
const version = '16.0.0';
17+
const babelURL = '//unpkg.com/babel-standalone@6.26.0/babel.min.js';
18+
const docsearchURL = '//unpkg.com/docsearch.js@2.4.1/dist/cdn/docsearch.min.js';
1719

18-
export {urlRoot, version};
20+
export {urlRoot, version, babelURL, docsearchURL};

src/templates/home.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ import React, {Component} from 'react';
1818
import TitleAndMetaTags from 'components/TitleAndMetaTags';
1919
import {colors, media, sharedStyles} from 'theme';
2020
import createOgUrl from 'utils/createOgUrl';
21+
import loadScript from 'utils/loadScript';
22+
import {babelURL} from 'site-constants';
2123

2224
class Home extends Component {
2325
componentDidMount() {
24-
mountCodeExample('helloExample', HELLO_COMPONENT);
25-
mountCodeExample('timerExample', TIMER_COMPONENT);
26-
mountCodeExample('todoExample', TODO_COMPONENT);
27-
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
26+
loadScript(babelURL).then(() => {
27+
mountCodeExample('helloExample', HELLO_COMPONENT);
28+
mountCodeExample('timerExample', TIMER_COMPONENT);
29+
mountCodeExample('todoExample', TODO_COMPONENT);
30+
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
31+
})
2832
}
2933

3034
render() {

src/utils/loadScript.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @emails react-core
8+
*/
9+
10+
'use strict';
11+
12+
export default url => new Promise((resolve, reject) =>
13+
document.head.appendChild(
14+
Object.assign(document.createElement('script'), {
15+
async: true,
16+
src: url,
17+
onload: resolve,
18+
onerror: reject
19+
})
20+
)
21+
);

0 commit comments

Comments
 (0)