-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Defer babel loading #100
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
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Deploy preview ready! Built with commit ba6a786 |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
src/layouts/index.js
Outdated
apiKey: '36221914cce388c46d0420343e0bb32e', | ||
indexName: 'react', | ||
inputSelector: '#algolia-doc-search', | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
mountCodeExample('timerExample', TIMER_COMPONENT); | ||
mountCodeExample('todoExample', TODO_COMPONENT); | ||
mountCodeExample('markdownExample', MARKDOWN_COMPONENT); | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed 2 small things. Sorry if you're still working on these files and my comments are premature.
src/layouts/index.js
Outdated
@@ -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'; |
There was a problem hiding this comment.
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 ^
src/site-constants.js
Outdated
|
||
export {urlRoot, version}; | ||
export {urlRoot, version, babelURL,}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run yarn prettier
to fix this formatting ^ (the trailing comma should be removed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about all of these changes to CodeEditor
markup/styles. Why are they needed for deferred Babel loading?
I'm a bit reluctant to change this markup much since we've fixed a lot of edge-cases with scrolling and responsive behavior for this specific component. Is there a way we could do the deferred loading that didn't require so many changes?
I had envisioned something like this:
- When the page initially mounts, render some placeholder "Loading code example..." type string into the 4 container
<div>
s - Lazy-load Babel (like this PR does)
- Then mount the
CodeEditor
component and compiled results into the 4 container<div>
s, replacing the placeholder content.
Well, most of the changes are not related to Babel, I was having trouble trying to render the placeholders because of the way that Anyways, if you're unsure about the changes I could do what you envisioned, but I think that the transition would still look rough once Babel loads. |
Thanks for explaining. 😄 I'd like to avoid making any changes (at least bigs ones) to We don't have to use the Let's do with a simple placeholder right now (just a string is fine) and myself and/or @joecritch will follow up on it. |
49cf1f7
to
a02139b
Compare
Apologies for forgetting about the hackiness we were doing inside of The home page content and markdown is all pretty janky until #97 gets resolved unfortunately, so Ideally we'd show a nice placeholder when loading is in progress and a nice error if loading failed. When I say "nice" I don't mean "fancy" just- something that will prevent the page from re-flowing text drastically when things load in. Unfortunately this isn't possible without more hacking b'c of how our content is put together and what we're doing in How do you feel about (for now) further simplifying this to: diff --git a/src/templates/home.js b/src/templates/home.js
index 5131557e3..cb32c683c 100644
--- a/src/templates/home.js
+++ b/src/templates/home.js
@@ -21,10 +21,10 @@ import ReactDOM from 'react-dom';
class Home extends Component {
componentDidMount() {
- this.renderExamplePlaceholder('helloExample');
- this.renderExamplePlaceholder('timerExample');
- this.renderExamplePlaceholder('todoExample');
- this.renderExamplePlaceholder('markdownExample');
+ renderExamplePlaceholder('helloExample');
+ renderExamplePlaceholder('timerExample');
+ renderExamplePlaceholder('todoExample');
+ renderExamplePlaceholder('markdownExample');
loadScript(babelURL).then(() => {
mountCodeExample('helloExample', HELLO_COMPONENT);
@@ -34,25 +34,6 @@ class Home extends Component {
});
}
- renderExamplePlaceholder(containerId) {
- ReactDOM.render(
- <strong
- css={{
- transform: 'translate(65%, -5em)',
- display: 'block',
-
- [media.lessThan('xlarge')]: {
- transform: 'none',
- marginTop: 15,
- textAlign: 'center',
- },
- }}>
- Loading code example...
- </strong>,
- document.getElementById(containerId),
- );
- }
-
render() {
const {data} = this.props;
const title = 'React - A JavaScript library for building user interfaces';
@@ -341,6 +322,16 @@ const markdownStyles = {
},
};
+// 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),
+ );
+}
+
// TODO Move these hard-coded examples into example files and out of the template?
// Alternately, move them into the markdown and transform them during build?
// This could be done via a new Gatsby transform plug-in that auto-converts to runnable REPLs? The reasons I propose these changes are:
|
Sure thing! Oh, and thanks for all the patience since this is my first PR with actual code. |
No thanks needed! Thanks for contributing and being so easy to work with and iterate on this stuff 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dig it
* docs: Translation of Forms page * docs: Small corrections in "Forms" page Co-Authored-By: Halian Vilela <[email protected]> * docs: Removing accent from an example on the Forms page Co-Authored-By: glaucia86 <[email protected]>
…hinese (reactjs#100) translate content/docs/reference-react-dom-server.md into Chinese
Created an utility function to asynchronously load Babel and Algolia attempting to fix #2.