Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7527993

Browse files
blainekastenwardpeetGatsbyJS Bot
authored
fix(gatsby): Improve error message when EnsureResources fails to ensure a resource (gatsbyjs#21429)
* fix(gatsby): Ensure that EnsureResources ensures resources * test * Throw error if the pageResource fails to load * fix lint * add jsx flag to allow test to pass * Update packages/gatsby/cache-dir/ensure-resources.js Co-Authored-By: Ward Peeters <[email protected]> Co-authored-by: Ward Peeters <[email protected]> Co-authored-by: GatsbyJS Bot <[email protected]>
1 parent 678aa87 commit 7527993

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from "react"
2+
import EnsureResources from "../ensure-resources"
3+
import { render, getNodeText, cleanup } from "@testing-library/react"
4+
5+
jest.mock(`../loader`, () => {
6+
return {
7+
loadPageSync(path: string): { loadPageSync: boolean; path: string } {
8+
return { loadPageSync: true, path }
9+
},
10+
loadPage(path: string): Promise<{ loadPage: boolean; path: string }> {
11+
return Promise.resolve({ loadPage: true, path })
12+
},
13+
}
14+
})
15+
16+
afterAll(cleanup)
17+
18+
describe(`EnsureResources`, () => {
19+
it(`loads pages synchronously`, () => {
20+
const location = {
21+
pathname: `/`,
22+
}
23+
const { container } = render(
24+
<EnsureResources location={location}>
25+
{(data: any): string => JSON.stringify(data.pageResources)}
26+
</EnsureResources>
27+
)
28+
29+
expect(getNodeText(container)).toMatchInlineSnapshot(
30+
`"{\\"loadPageSync\\":true,\\"path\\":\\"/\\"}"`
31+
)
32+
})
33+
})

packages/gatsby/cache-dir/ensure-resources.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ class EnsureResources extends React.Component {
7474
}
7575

7676
render() {
77+
if (process.env.NODE_ENV !== `production` && !this.state.pageResources) {
78+
throw new Error(
79+
`EnsureResources was not able to find resources for path: "${this.props.location.pathname}"
80+
This typically means that an issue occurred building components for that path.
81+
Run \`gatsby clean\` to remove any cached elements.`
82+
)
83+
}
84+
7785
return this.props.children(this.state)
7886
}
7987
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"strictPropertyInitialization": true,
1313
"noFallthroughCasesInSwitch": true,
1414
"resolveJsonModule": true,
15-
"esModuleInterop": true
15+
"esModuleInterop": true,
16+
"jsx": "preserve"
1617
},
1718
"exclude": ["peril/*", "examples/*"]
1819
}

0 commit comments

Comments
 (0)