-
Notifications
You must be signed in to change notification settings - Fork 938
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
Allow code splitting #1411
Merged
Merged
Allow code splitting #1411
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gnito
force-pushed
the
code-splitting-dataloader-api
branch
3 times, most recently
from
February 10, 2021 13:07
a7a1fc5
to
d8a8b12
Compare
Gnito
force-pushed
the
code-splitting-dataloader-api
branch
from
February 10, 2021 13:31
d8a8b12
to
ab9eda8
Compare
Gnito
force-pushed
the
code-splitting-dataloader-api
branch
from
February 15, 2021 14:58
ab9eda8
to
6ab8f06
Compare
Gnito
force-pushed
the
code-splitting-dataloader-api
branch
2 times, most recently
from
February 16, 2021 12:06
6964a7b
to
77e63ef
Compare
Gnito
changed the title
WiP: figure out how to allow code splitting
Allow code splitting
Feb 16, 2021
kpuputti
reviewed
Feb 16, 2021
Gnito
force-pushed
the
code-splitting-dataloader-api
branch
from
February 16, 2021 16:36
77e63ef
to
caab97f
Compare
These deps need to match with the same dependecies from sharetribe-scripts
Export loadData from SearchPage.duck.js file
Loadable Components become unreadable
Now that pages are imported asynchronously, we cannot fully render them without changing the test setup. If the server rendering needs specific tests, then it should probably be based on the server build and the proper Express server.
Gnito
force-pushed
the
code-splitting-dataloader-api
branch
from
February 17, 2021 10:02
caab97f
to
899ccf7
Compare
This was referenced Feb 17, 2021
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, sharetribe-scripts created one UMD build that was used on both server and frontend. I.e. all the code used in the app was bundled into a single main.bundle.js file and that was used on web app and server.
Unfortunately, this has meant that code-splitting was not supported: it didn't work with UMD build due to an old bug in Webpack.
With the upcoming update (Sharetribe-scripts v5.0.0), we are changing this behaviour: sharetribe-scripts creates 2 different builds when
yarn run build
is called. Basically, this means that build-time increases (includingyarn run dev-server
call).However, this setup makes code-splitting possible. To make this easier, we have added Loadable Components library to the setup.
What is code splitting
Instead of downloading the entire app before users can use it, code splitting allows us to split the code (from one main.bundle.js file) into smaller chunks which you can then load on demand. To familiarize yourself with the subject, you could read about code splitting from reactjs.org.
In practice, we have used route-based code splitting: page-level components are now using Loadable Components syntax to create dynamic imports functionality.
When module bundler comes across these loadable objects, they will create a new JS & CSS chunk files (e.g. AboutPage.dc3102d3.chunk.js). I.e. those code-paths are separated from the main bundle.
Previously, when you loaded
/about
page, you received main.bundle.js & main.bundle.css, which were pretty huge files containing all the code that was needed to create FTW-daily and any page inside it. Loading a single file takes time and also browsers had to evaluate the entire js-file, before it was ready to make the app fully functional.So, the main benefit of code splitting is to reduce the code that is loaded for any single page. That improves the performance, but even more importantly, it makes it possible to add more navigational paths and page-variants to the codebase. For example, adding different kind of ListingPages for different types of listings makes more sense with code-splitting. Otherwise, new pages would have performance impact on initial page load and therefore SEO performance would drop too.
How code splitting works in the new code
With code-splitting, this resource loading setup changes a bit. If you open
/about
page on top of this branch, you'll notice that there are several JS & CSS files loaded:This one takes care of loading correct JS & CSS files when you navigate to another page inside the web app.
(e.g. it loads LandingPage.6fa732d5.chunk.js && LandingPage.40c0bf91.chunk.css, when you navigate to landing page.)
So, there are several chunk files that can be loaded parallel in the first page load and also page-specific chunks that can be loaded in response to in-app navigation.
Naturally, this means that during in-app navigation there are now more things that the app needs to load: data that the next page needs and code chunk that it needs to render the data. The latter is not needed if the page-specific chunk is already loaded earlier.
How flickering is optimized with preloading
This means that there might be a fast flickering of blank page when navigation happens first time to a new page. To remedy that situation, we have forced the page-chunk to be preloaded when mouse is over NamedLink. In addition, Form and Button components can have a property
enforcePagePreloadFor="SearchPage"
. That way the specified chunk is loaded before the user has actually clicked the button or executed form submit.Changes to route configuration
To make this preloading possible, we refactored routes in routeConfiguration.js file a bit: Loadable component is directly set to "component" conf:
Change to loadData function setup
Another change is how loadData & setInitialValues static functions work. Since page component is now a Promise (since page chunk is loaded asynchronously), we had to access those functions differently. To minimize the needed changes, we ended up collecting those Redux functions through duck-files - similarly than how src/containers/reducers.js does. So, all the loadData and setInitialValues functions are collected in src/containers/pageDataLoadingAPI.js. For routing, this means changes like:
This also forced us to standardize the location of loadData functions. Some of the pages have had those is page's main file and not in the duck.js file - most notably SearchPage. Now all the loadData functions are defined in page's own modular Redux file (i.e. SomePageComponent.duck.js).
CSS chunk changes
To ensure that every page-level CSS chunks have custom media queries included, we have separated those breakpoints to src/styles/customMediaQueries.css file and imported it to every page's stylesheet.
Server-side rendering (SSR)
Because pageDataLoadingAPI.js allowed us to keep the routeConfiguration.js file pretty close to original setup, the data loading for SSR works pretty much the same as before:
However, Loadable Components setup did change the way we import the app-related code:
In practice,
renderApp
function wraps the app with webExtractor.collectChunks and with that webExtractor can figure out all the relevant loadable calls that the server uses for the current page and therefore the web-versions of those chunks can be included to rendered pages through<script>
tags.