Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): lazy bundle page components in dev server (#27884)
* feat(gatsby): lazy compile page components in dev server This means we only compile the core runtime code initially — page components are only compiled when the user visits the page. * Fix lint * Cleanup logs * cleanups * fix tests * Don't bother with navigating — if webpack doesn't update — the user probably has errors in their code or something bigger is wrong so let them just refresh, etc. * Update packages/gatsby/src/bootstrap/requires-writer.ts Co-authored-by: Ward Peeters <ward@coding-tech.com> * Update packages/gatsby/cache-dir/dev-loader.js Co-authored-by: Ward Peeters <ward@coding-tech.com> * Fix lint error * Wait until we know staticQueryHashes are written to page-data.json files We only discover static queries for pages after the component is compiled. Which means when we lazily add page components, we initially don't know what static queries should be loaded. To track this, we mark page-data.json files as not bundled & only after the staticQueryHashes is written do we accept the page as loaded. * Disable check in production * Only trigger compilation/page writing once * Also pull xhr loaded static queries for <StaticQuery> * instance isn't defined in builds apparently * Work around webpack hot reloading bug * For static queries, data pushed from websocket is the most fresh * Centralize logic and prevent it from repeatedly hitting the backend * fix ordering of merge * Comment and add timeout to ensure page-data writes happen after the webpack compilation * Only add notInDevBundle during development * Add back mistakenly removed code * Didn't need this * Implement @pieh's idea for telling runtime that dev bundle is loaded * Fix tests * Remove logs * Put changes behind GATSBY_EXPERIMENT_LAZY_DEVJS flag * Add temporary fork of development-runtime tests for lazy devjs PR * Remove duplicated tests w/ cleaner method @pieh suggested * Address @pieh feedback * Update packages/gatsby/src/utils/start-server.ts Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com> * Remove global middleware * This isn't true * fix lint Co-authored-by: Ward Peeters <ward@coding-tech.com> Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
- Loading branch information
04349a0
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.
if (process.env.GATSBY_EXPERIMENT_LAZY_DEVJS) {
const bodyParser = require(
body-parser
)const { boundActionCreators } = require(
../redux/actions
)const { createClientVisitedPage } = boundActionCreators
// Listen for the client marking a page as visited (meaning we need to
// compile its page component.
const chunkCalls = new Set()
app.post(
/___client-page-visited
, bodyParser.json(), (req, res, next) => {if (req.body?.chunkName) {
// Ignore all but the first POST.
if (!chunkCalls.has(req.body.chunkName)) {
// Tell Gatsby there's a new page component to trigger it
// being added to the bundle.
createClientVisitedPage(req.body.chunkName)
}
/**
**/