Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer babel loading #100

Merged
merged 8 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,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 {
render() {
let css;
Expand All @@ -37,8 +32,6 @@ export default class HTML extends Component {
);
}

const js = JS_NPM_URLS.map(url => <script key={url} src={url} />);

return (
<html lang="en">
<head>
Expand All @@ -50,7 +43,6 @@ export default class HTML extends Component {
/>
<link rel="icon" href="/favicon.ico" />
{this.props.headComponents}
{js}
{css}
</head>
<body>
Expand Down
18 changes: 11 additions & 7 deletions src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Flex from 'components/Flex';
import Footer from 'components/LayoutFooter';
import Header from 'components/LayoutHeader';
import {media} from 'theme';
import loadScript from 'utils/loadScript';
import {algoliaURL} from 'site-constants';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports can be removed ^


// Import global styles
import '../prism-styles';
Expand All @@ -28,13 +30,15 @@ import 'css/algolia.css';

class Template extends Component {
componentDidMount() {
// Initialize Algolia search.
// TODO Is this expensive? Should it be deferred until a user is about to search?
// eslint-disable-next-line no-undef
docsearch({
apiKey: '36221914cce388c46d0420343e0bb32e',
indexName: 'react',
inputSelector: '#algolia-doc-search',
loadScript(algoliaURL).then(() => {
// Initialize Algolia search.
// TODO Is this expensive? Should it be deferred until a user is about to search?
// eslint-disable-next-line no-undef
docsearch({
apiKey: '36221914cce388c46d0420343e0bb32e',
indexName: 'react',
inputSelector: '#algolia-doc-search',
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will load the Algolia JS for every page. Even though the request will be cached, the browser might still end up wasting cycles processing/reprocessing the script.

Also, ideally, if we're going to defer loading Algolia- we should defer loading it until the user actually starts to search (eg clicks to focus in the search bar). So maybe we could do this async step there instead? Happy to (a) talk more about this or (b) split that part off into a separate, follow-up issue or task.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should be a separate issue.

});
}

Expand Down
4 changes: 3 additions & 1 deletion src/site-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
// 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';
const algoliaURL = '//unpkg.com/docsearch.js@2.4.1/dist/cdn/docsearch.min.js';

export {urlRoot, version};
export {urlRoot, version, babelURL, algoliaURL};
12 changes: 8 additions & 4 deletions src/templates/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ 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';

class Home extends Component {
componentDidMount() {
mountCodeExample('helloExample', HELLO_COMPONENT);
mountCodeExample('timerExample', TIMER_COMPONENT);
mountCodeExample('todoExample', TODO_COMPONENT);
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
loadScript(babelURL).then(() => {
mountCodeExample('helloExample', HELLO_COMPONENT);
mountCodeExample('timerExample', TIMER_COMPONENT);
mountCodeExample('todoExample', TODO_COMPONENT);
mountCodeExample('markdownExample', MARKDOWN_COMPONENT);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts about showing a loading placeholder in the 4 divs in question until Babel has finished loading?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea, I can see that it is flashing once it loads. To fix this should I style the divs at content/index.md?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not. (We actually need to clean up index.md, see #97)

I would try maybe temporarily rendering a placeholder into the container from the Home template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean that I should use ReactDOM.render like mountCodeExample does?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably 😄

}

render() {
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,
}),
),
);