Skip to content
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

fix(gatsby): Improve error message when EnsureResources fails to ensure a resource #21429

Merged
merged 9 commits into from
Feb 29, 2020
33 changes: 33 additions & 0 deletions packages/gatsby/cache-dir/__tests__/ensure-resources.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react"
import EnsureResources from "../ensure-resources"
import { render, getNodeText, cleanup } from "@testing-library/react"

jest.mock(`../loader`, () => {
return {
loadPageSync(path: string): { loadPageSync: boolean; path: string } {
return { loadPageSync: true, path }
},
loadPage(path: string): Promise<{ loadPage: boolean; path: string }> {
return Promise.resolve({ loadPage: true, path })
},
}
})

afterAll(cleanup)

describe(`EnsureResources`, () => {
it(`loads pages synchronously`, () => {
const location = {
pathname: `/`,
}
const { container } = render(
<EnsureResources location={location}>
{(data: any): string => JSON.stringify(data.pageResources)}
</EnsureResources>
)

expect(getNodeText(container)).toMatchInlineSnapshot(
`"{\\"loadPageSync\\":true,\\"path\\":\\"/\\"}"`
)
})
})
10 changes: 10 additions & 0 deletions packages/gatsby/cache-dir/ensure-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ class EnsureResources extends React.Component {
}

render() {
if (!this.state.pageResources) {
console.warn(
`EnsureResources was not able to find resources for path: "${this.props.location.pathname}"`,
`This typically means that an issue occurred building components for that path`
)
throw new Error(
`EnsureResources was not able to find resources for path: "${this.props.location.pathname}"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not duplicate this line in warning and error message - I would vote to get rid of warn completely and just put this whole text in thrown error - sounds reasonable?

Copy link
Contributor

@pieh pieh Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I was mostly thinking about production builds here - maybe we should have conditional logic for gatsby develop (using process.env.NODE_ENV === `production` to show short(ish) message, but for gatsby develop case we can show cleaning up .cache (but it probably should be both .cache and public given stale page-data being on of potential reasons for this)

)
}
blainekasten marked this conversation as resolved.
Show resolved Hide resolved

return this.props.children(this.state)
}
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"strictPropertyInitialization": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"esModuleInterop": true
"esModuleInterop": true,
"jsx": "preserve"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this flag is so we can do react stuff. it was throwing an error and breaking builds

},
"exclude": ["peril/*", "examples/*"]
}