Skip to content

Commit

Permalink
Merge pull request #100 from renanpvaz/defer-babel-loading
Browse files Browse the repository at this point in the history
Defer babel loading
  • Loading branch information
bvaughn committed Oct 11, 2017
2 parents 42372b2 + ba6a786 commit 85a70b7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ if (process.env.NODE_ENV === `production`) {

const JS_NPM_URLS = [
'//unpkg.com/docsearch.js@2.4.1/dist/cdn/docsearch.min.js',
'//unpkg.com/babel-standalone@6.26.0/babel.min.js',
];

export default class HTML extends Component {
Expand Down
3 changes: 2 additions & 1 deletion src/site-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
// the SSR part in node.js we won't have a proper location.
const urlRoot = 'https://reactjs.org';
const version = '16.0.0';
const babelURL = '//unpkg.com/babel-standalone@6.26.0/babel.min.js';

export {urlRoot, version};
export {urlRoot, version, babelURL};
28 changes: 24 additions & 4 deletions src/templates/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ import React, {Component} from 'react';
import TitleAndMetaTags from 'components/TitleAndMetaTags';
import {colors, media, sharedStyles} from 'theme';
import createOgUrl from 'utils/createOgUrl';
import loadScript from 'utils/loadScript';
import {babelURL} from 'site-constants';
import ReactDOM from 'react-dom';

class Home extends Component {
componentDidMount() {
mountCodeExample('helloExample', HELLO_COMPONENT);
mountCodeExample('timerExample', TIMER_COMPONENT);
mountCodeExample('todoExample', TODO_COMPONENT);
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
renderExamplePlaceholder('helloExample');
renderExamplePlaceholder('timerExample');
renderExamplePlaceholder('todoExample');
renderExamplePlaceholder('markdownExample');

loadScript(babelURL).then(() => {
mountCodeExample('helloExample', HELLO_COMPONENT);
mountCodeExample('timerExample', TIMER_COMPONENT);
mountCodeExample('todoExample', TODO_COMPONENT);
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
});
}

render() {
Expand Down Expand Up @@ -163,6 +173,16 @@ Home.propTypes = {
location: PropTypes.object.isRequired,
};

// TODO Improve this temporarily placeholder as part of
// converting the home page from markdown template to a Gatsby
// page (see issue #2)
function renderExamplePlaceholder(containerId) {
ReactDOM.render(
<h4>Loading code example...</h4>,
document.getElementById(containerId),
);
}

const CtaItem = ({children, primary = false}) => (
<div
css={{
Expand Down
22 changes: 22 additions & 0 deletions src/utils/loadScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

export default url =>
new Promise((resolve, reject) =>
document.head.appendChild(
Object.assign(document.createElement('script'), {
async: true,
src: url,
onload: resolve,
onerror: reject,
}),
),
);

0 comments on commit 85a70b7

Please sign in to comment.