Skip to content

Commit f4c5c48

Browse files
authored
fix(gatsby): update async requires in dev loader (#32189)
* add failing e2e test * change test setup to not be flaky with retries * fix stale asyncRequires in dev loader
1 parent cdfc23f commit f4c5c48

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

e2e-tests/development-runtime/cypress/integration/hot-reloading/new-file.js

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
describe(`client-side navigation to the new page`, () => {
2+
before(() => {
3+
cy.visit(`/`).waitForRouteChange()
4+
cy.exec(`npm run update -- --file src/pages/new-page.js`)
5+
cy.wait(1000)
6+
})
7+
8+
it(`can navigate to newly created page using link`, () => {
9+
cy.findByTestId(`hot-reloading-new-file`).click().waitForRouteChange()
10+
cy.getTestElement(`message`).invoke(`text`).should(`contain`, `Hello`)
11+
})
12+
})
13+
114
describe(`hot reloading new page component`, () => {
215
before(() => {
316
cy.exec(`npm run update -- --file src/pages/sample.js`)

e2e-tests/development-runtime/src/pages/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const IndexPage = ({ data }) => (
4444
>
4545
Go to client route splat (splat: blah/blah/blah)
4646
</Link>
47+
<Link to="/new-page" data-testid="hot-reloading-new-file">
48+
Created by hot-reloading/new-file.js
49+
</Link>
4750
<h2>Blog posts</h2>
4851
<ul>
4952
{data.posts.edges.map(({ node }) => (

packages/gatsby/cache-dir/app.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ import "./blank.css"
2222
// Enable fast-refresh for virtual sync-requires, gatsby-browser & navigation
2323
// To ensure that our <Root /> component can hot reload in case anything below doesn't
2424
// satisfy fast-refresh constraints
25-
module.hot.accept([
26-
`$virtual/async-requires`,
27-
`./api-runner-browser`,
28-
`./navigation`,
29-
])
25+
module.hot.accept(
26+
[`$virtual/async-requires`, `./api-runner-browser`, `./navigation`],
27+
() => {
28+
// asyncRequires should be automatically updated here (due to ESM import and webpack HMR spec),
29+
// but loader doesn't know that and needs to be manually nudged
30+
loader.updateAsyncRequires(asyncRequires)
31+
}
32+
)
3033

3134
window.___emitter = emitter
3235

packages/gatsby/cache-dir/dev-loader.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,21 @@ function mergePageEntry(cachedPage, newPageData) {
2626
class DevLoader extends BaseLoader {
2727
constructor(asyncRequires, matchPaths) {
2828
const loadComponent = chunkName => {
29-
if (!asyncRequires.components[chunkName]) {
29+
if (!this.asyncRequires.components[chunkName]) {
3030
throw new Error(
3131
`We couldn't find the correct component chunk with the name "${chunkName}"`
3232
)
3333
}
3434

3535
return (
36-
asyncRequires.components[chunkName]()
36+
this.asyncRequires.components[chunkName]()
3737
.then(preferDefault)
3838
// loader will handle the case when component is error
3939
.catch(err => err)
4040
)
4141
}
4242
super(loadComponent, matchPaths)
43+
this.asyncRequires = asyncRequires
4344

4445
const socket = getSocket()
4546

@@ -60,6 +61,10 @@ class DevLoader extends BaseLoader {
6061
}
6162
}
6263

64+
updateAsyncRequires(asyncRequires) {
65+
this.asyncRequires = asyncRequires
66+
}
67+
6368
loadPage(pagePath) {
6469
const realPath = findPath(pagePath)
6570
return super.loadPage(realPath).then(result => {

0 commit comments

Comments
 (0)