From e0740477efa16201bfbce5c2a2ce86ab4f11acf7 Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Thu, 30 Jan 2020 08:55:56 -0600 Subject: [PATCH 1/6] fix(gatsby): Ensure that EnsureResources ensures resources --- packages/gatsby/cache-dir/ensure-resources.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/gatsby/cache-dir/ensure-resources.js b/packages/gatsby/cache-dir/ensure-resources.js index 2c67ce2619857..dad8e573d69cf 100644 --- a/packages/gatsby/cache-dir/ensure-resources.js +++ b/packages/gatsby/cache-dir/ensure-resources.js @@ -40,6 +40,12 @@ class EnsureResources extends React.Component { }) } + componentDidMount() { + if (!this.state.pageResources) { + this.loadResources(this.props.location.pathname) + } + } + shouldComponentUpdate(nextProps, nextState) { // Always return false if we're missing resources. if (!nextState.pageResources) { From 178aad6466b771a7009fab2c809ec0b19f5e092b Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Thu, 30 Jan 2020 09:11:26 -0600 Subject: [PATCH 2/6] test --- .../cache-dir/__tests__/ensure-resources.tsx | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/gatsby/cache-dir/__tests__/ensure-resources.tsx diff --git a/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx b/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx new file mode 100644 index 0000000000000..3c71845e7f6a0 --- /dev/null +++ b/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx @@ -0,0 +1,32 @@ +import React from "react" +import EnsureResources from "../ensure-resources" +import { render, getNodeText, cleanup } from "@testing-library/react" + +jest.mock("../loader", () => ({ + loadPageSync(path) { + return { loadPageSync: true, path } + }, + loadPage(path) { + return Promise.resolve({ loadPage: true, path }) + }, +})) + +afterAll(cleanup) + +describe("EnsureResources", () => { + it("works", () => { + const location = { + pathname: "/", + } + const { container } = render( + + {data => JSON.stringify(data.pageResources)} + + ) + console.log(container) + + expect(getNodeText(container)).toMatchInlineSnapshot( + `"{\\"loadPageSync\\":true,\\"path\\":\\"/\\"}"` + ) + }) +}) From 96ab5d59982dd1ad81422824ea15aa258ad77b2a Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Thu, 13 Feb 2020 10:31:20 -0600 Subject: [PATCH 3/6] Throw error if the pageResource fails to load --- .../cache-dir/__tests__/ensure-resources.tsx | 3 +-- packages/gatsby/cache-dir/ensure-resources.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx b/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx index 3c71845e7f6a0..5384205304d16 100644 --- a/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx +++ b/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx @@ -14,7 +14,7 @@ jest.mock("../loader", () => ({ afterAll(cleanup) describe("EnsureResources", () => { - it("works", () => { + it("loads pages synchronously", () => { const location = { pathname: "/", } @@ -23,7 +23,6 @@ describe("EnsureResources", () => { {data => JSON.stringify(data.pageResources)} ) - console.log(container) expect(getNodeText(container)).toMatchInlineSnapshot( `"{\\"loadPageSync\\":true,\\"path\\":\\"/\\"}"` diff --git a/packages/gatsby/cache-dir/ensure-resources.js b/packages/gatsby/cache-dir/ensure-resources.js index dad8e573d69cf..f0de01ce04a2a 100644 --- a/packages/gatsby/cache-dir/ensure-resources.js +++ b/packages/gatsby/cache-dir/ensure-resources.js @@ -40,12 +40,6 @@ class EnsureResources extends React.Component { }) } - componentDidMount() { - if (!this.state.pageResources) { - this.loadResources(this.props.location.pathname) - } - } - shouldComponentUpdate(nextProps, nextState) { // Always return false if we're missing resources. if (!nextState.pageResources) { @@ -80,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}"` + ) + } + return this.props.children(this.state) } } From fd7234e7a9a51494bc6e3c3f244f2923827cdd0f Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Sat, 15 Feb 2020 15:29:01 -0600 Subject: [PATCH 4/6] fix lint --- .../cache-dir/__tests__/ensure-resources.tsx | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx b/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx index 5384205304d16..0613effcbdb04 100644 --- a/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx +++ b/packages/gatsby/cache-dir/__tests__/ensure-resources.tsx @@ -2,25 +2,27 @@ import React from "react" import EnsureResources from "../ensure-resources" import { render, getNodeText, cleanup } from "@testing-library/react" -jest.mock("../loader", () => ({ - loadPageSync(path) { - return { loadPageSync: true, path } - }, - loadPage(path) { - return Promise.resolve({ loadPage: true, path }) - }, -})) +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", () => { +describe(`EnsureResources`, () => { + it(`loads pages synchronously`, () => { const location = { - pathname: "/", + pathname: `/`, } const { container } = render( - {data => JSON.stringify(data.pageResources)} + {(data: any): string => JSON.stringify(data.pageResources)} ) From daf29c1ffbbacd6afdb5ab24e4325c06f960a41e Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Sat, 15 Feb 2020 16:44:11 -0600 Subject: [PATCH 5/6] add jsx flag to allow test to pass --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 0b0873225b647..866b23e3c91b6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,8 @@ "strictPropertyInitialization": true, "noFallthroughCasesInSwitch": true, "resolveJsonModule": true, - "esModuleInterop": true + "esModuleInterop": true, + "jsx": "preserve" }, "exclude": ["peril/*", "examples/*"] } From a57cd0fa13c812ce4dd44502235febfbd2da1f57 Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Fri, 28 Feb 2020 08:50:42 -0600 Subject: [PATCH 6/6] Update packages/gatsby/cache-dir/ensure-resources.js Co-Authored-By: Ward Peeters --- packages/gatsby/cache-dir/ensure-resources.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/cache-dir/ensure-resources.js b/packages/gatsby/cache-dir/ensure-resources.js index f0de01ce04a2a..e67c6b1423b9e 100644 --- a/packages/gatsby/cache-dir/ensure-resources.js +++ b/packages/gatsby/cache-dir/ensure-resources.js @@ -74,13 +74,11 @@ 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` - ) + if (process.env.NODE_ENV !== `production` && !this.state.pageResources) { throw new Error( - `EnsureResources was not able to find resources for path: "${this.props.location.pathname}"` + `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. +Run \`gatsby clean\` to remove any cached elements.` ) }