Skip to content

Commit 85a70b7

Browse files
authored
Merge pull request reactjs#100 from renanpvaz/defer-babel-loading
Defer babel loading
2 parents 42372b2 + ba6a786 commit 85a70b7

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

src/html.js

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ if (process.env.NODE_ENV === `production`) {
1919

2020
const JS_NPM_URLS = [
2121
'//unpkg.com/[email protected]/dist/cdn/docsearch.min.js',
22-
'//unpkg.com/[email protected]/babel.min.js',
2322
];
2423

2524
export default class HTML extends Component {

src/site-constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
// the SSR part in node.js we won't have a proper location.
1212
const urlRoot = 'https://reactjs.org';
1313
const version = '16.0.0';
14+
const babelURL = '//unpkg.com/[email protected]/babel.min.js';
1415

15-
export {urlRoot, version};
16+
export {urlRoot, version, babelURL};

src/templates/home.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ import React, {Component} from 'react';
1515
import TitleAndMetaTags from 'components/TitleAndMetaTags';
1616
import {colors, media, sharedStyles} from 'theme';
1717
import createOgUrl from 'utils/createOgUrl';
18+
import loadScript from 'utils/loadScript';
19+
import {babelURL} from 'site-constants';
20+
import ReactDOM from 'react-dom';
1821

1922
class Home extends Component {
2023
componentDidMount() {
21-
mountCodeExample('helloExample', HELLO_COMPONENT);
22-
mountCodeExample('timerExample', TIMER_COMPONENT);
23-
mountCodeExample('todoExample', TODO_COMPONENT);
24-
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
24+
renderExamplePlaceholder('helloExample');
25+
renderExamplePlaceholder('timerExample');
26+
renderExamplePlaceholder('todoExample');
27+
renderExamplePlaceholder('markdownExample');
28+
29+
loadScript(babelURL).then(() => {
30+
mountCodeExample('helloExample', HELLO_COMPONENT);
31+
mountCodeExample('timerExample', TIMER_COMPONENT);
32+
mountCodeExample('todoExample', TODO_COMPONENT);
33+
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
34+
});
2535
}
2636

2737
render() {
@@ -163,6 +173,16 @@ Home.propTypes = {
163173
location: PropTypes.object.isRequired,
164174
};
165175

176+
// TODO Improve this temporarily placeholder as part of
177+
// converting the home page from markdown template to a Gatsby
178+
// page (see issue #2)
179+
function renderExamplePlaceholder(containerId) {
180+
ReactDOM.render(
181+
<h4>Loading code example...</h4>,
182+
document.getElementById(containerId),
183+
);
184+
}
185+
166186
const CtaItem = ({children, primary = false}) => (
167187
<div
168188
css={{

src/utils/loadScript.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 =>
13+
new Promise((resolve, reject) =>
14+
document.head.appendChild(
15+
Object.assign(document.createElement('script'), {
16+
async: true,
17+
src: url,
18+
onload: resolve,
19+
onerror: reject,
20+
}),
21+
),
22+
);

0 commit comments

Comments
 (0)