-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby-core-utils): improve fetch-remote-file #34758
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1aca67a
feat(gatsby-core-utils): improve fetch-remote-file
wardpeet 3aafa76
feat: use cacheKey in fetch-remote-file
wardpeet b07e303
use async file operations
wardpeet 32605ce
Improve tests + low hanging fruit
wardpeet 567d12b
revert wordpress
wardpeet a71064b
feat: make sure 304 works
wardpeet ea511db
fix jest config wordpress
wardpeet cfa9a86
add integration test
wardpeet 794761e
update tests
wardpeet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
integration-tests/gatsby-pipeline/__tests__/fetch-remote-file/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* We want to make sure that fetch-remote-file is working with multi workers. | ||
*/ | ||
|
||
const execa = require(`execa`) | ||
const path = require(`path`) | ||
const glob = require(`glob`) | ||
const fs = require(`fs-extra`) | ||
const md5File = require(`md5-file`) | ||
const basePath = path.resolve(__dirname, `../../`) | ||
|
||
const cleanDirs = () => | ||
Promise.all([ | ||
fs.emptyDir(`${basePath}/public`), | ||
fs.emptyDir(`${basePath}/.cache`), | ||
]) | ||
|
||
describe(`fetch-remote-file`, () => { | ||
beforeAll(async () => { | ||
await cleanDirs() | ||
await execa(`yarn`, [`build`], { | ||
cwd: basePath, | ||
// we want to force 1 query per worker | ||
env: { NODE_ENV: `production`, GATSBY_PARALLEL_QUERY_CHUNK_SIZE: `1` }, | ||
}) | ||
}, 60 * 1000) | ||
|
||
it("should have the correct md5", async () => { | ||
expect( | ||
await md5File( | ||
path.join( | ||
__dirname, | ||
"../..", | ||
"public/images/50c58a791de3c2303e62084d731799eb/photoA.jpg" | ||
) | ||
) | ||
).toEqual("a9e57a66a10b2d26a1999a4685d7c9ef") | ||
expect( | ||
await md5File( | ||
path.join( | ||
__dirname, | ||
"../..", | ||
"public/images/4910e745c3c453b8795d6ba65c79d99b/photoB.jpg" | ||
) | ||
) | ||
).toEqual("c305dc5c5db45cc773231a507af5116d") | ||
expect( | ||
await md5File( | ||
path.join( | ||
__dirname, | ||
"../..", | ||
"public/images/fb673e75e9534b3cc2d2e24085386d48/photoC.jpg" | ||
) | ||
) | ||
).toEqual("4ba953ba27236727d7abe7d5b8916432") | ||
}) | ||
|
||
/** | ||
* this is a bit of a cheeky test but we just want to make sure we're actually running on multiple workers | ||
*/ | ||
it("should have conflict between workers", async () => { | ||
const files = await fs.readdir(path.join(__dirname, "../../.cache/workers")) | ||
|
||
expect(files.length).toBeGreaterThan(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { fetchRemoteFile } = require("gatsby-core-utils/fetch-remote-file") | ||
const { slash } = require("gatsby-core-utils") | ||
const path = require("path") | ||
const fs = require("fs-extra") | ||
|
||
/** @type{import('gatsby').createSchemaCustomization} */ | ||
exports.createSchemaCustomization = ({ actions, schema, cache, reporter }) => { | ||
actions.createTypes( | ||
schema.buildObjectType({ | ||
name: "MyRemoteFile", | ||
fields: { | ||
url: "String!", | ||
publicUrl: { | ||
type: "String!", | ||
async resolve(source) { | ||
const filePath = await fetchRemoteFile({ | ||
name: path.basename(source.name, path.extname(source.name)), | ||
ext: path.extname(source.name), | ||
url: source.url, | ||
directory: "./public/images", | ||
}) | ||
|
||
const dir = path.join(global.__GATSBY.root, ".cache", "workers") | ||
await fs.ensureDir(dir) | ||
await fs.createFile( | ||
`${path.join(dir, `worker-${process.env.GATSBY_WORKER_ID}`)}` | ||
) | ||
|
||
const workers = (await cache.get("workers")) ?? [] | ||
workers.push(process.env.GATSBY_WORKER_ID) | ||
|
||
return `${slash(filePath.replace(/^public/, ""))}` | ||
}, | ||
}, | ||
}, | ||
interfaces: ["Node"], | ||
}) | ||
) | ||
} | ||
|
||
/** @type {imporg('gatsby').sourceNodes} */ | ||
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => { | ||
const items = [ | ||
{ | ||
name: "photoA.jpg", | ||
url: "https://images.unsplash.com/photo-1517849845537-4d257902454a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=300&q=80", | ||
}, | ||
{ | ||
name: "photoB.jpg", | ||
url: "https://images.unsplash.com/photo-1552053831-71594a27632d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=300&q=80", | ||
}, | ||
{ | ||
name: "photoC.jpg", | ||
url: "https://images.unsplash.com/photo-1561037404-61cd46aa615b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=300&q=80", | ||
}, | ||
] | ||
|
||
items.forEach((item, index) => { | ||
actions.createNode({ | ||
id: createNodeId(`remote-file-${index}`), | ||
name: item.name, | ||
url: item.url, | ||
internal: { | ||
type: "MyRemoteFile", | ||
contentDigest: createContentDigest(item.url), | ||
}, | ||
}) | ||
}) | ||
} |
31 changes: 31 additions & 0 deletions
31
integration-tests/gatsby-pipeline/src/pages/fetch-remote-a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react" | ||
import { graphql, Link } from "gatsby" | ||
|
||
import Layout from "../components/layout" | ||
import SEO from "../components/seo" | ||
|
||
const FetchRemoteA = ({ data }) => { | ||
return ( | ||
<Layout> | ||
<SEO title="Fetch Remote A" /> | ||
|
||
<pre | ||
dangerouslySetInnerHTML={{ __html: JSON.stringify(data, null, 2) }} | ||
/> | ||
<Link to="/">Go back to the homepage</Link> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default FetchRemoteA | ||
|
||
export const pageQuery = graphql` | ||
{ | ||
allMyRemoteFile { | ||
nodes { | ||
url | ||
publicUrl | ||
} | ||
} | ||
} | ||
` |
32 changes: 32 additions & 0 deletions
32
integration-tests/gatsby-pipeline/src/pages/fetch-remote-b.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from "react" | ||
import { graphql, Link } from "gatsby" | ||
|
||
import Layout from "../components/layout" | ||
import SEO from "../components/seo" | ||
|
||
const FetchRemoteB = ({ data }) => { | ||
return ( | ||
<Layout> | ||
<SEO title="Fetch Remote B" /> | ||
|
||
<pre | ||
dangerouslySetInnerHTML={{ __html: JSON.stringify(data, null, 2) }} | ||
/> | ||
|
||
<Link to="/">Go back to the homepage</Link> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default FetchRemoteB | ||
|
||
export const pageQuery = graphql` | ||
{ | ||
allMyRemoteFile { | ||
nodes { | ||
url | ||
publicUrl | ||
} | ||
} | ||
} | ||
` |
31 changes: 31 additions & 0 deletions
31
integration-tests/gatsby-pipeline/src/pages/fetch-remote-c.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react" | ||
import { graphql, Link } from "gatsby" | ||
|
||
import Layout from "../components/layout" | ||
import SEO from "../components/seo" | ||
|
||
const FetchRemoteB = ({ data }) => { | ||
return ( | ||
<Layout> | ||
<SEO title="Fetch Remote C" /> | ||
|
||
<pre | ||
dangerouslySetInnerHTML={{ __html: JSON.stringify(data, null, 2) }} | ||
/> | ||
<Link to="/">Go back to the homepage</Link> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default FetchRemoteB | ||
|
||
export const pageQuery = graphql` | ||
{ | ||
allMyRemoteFile { | ||
nodes { | ||
url | ||
publicUrl | ||
} | ||
} | ||
} | ||
` |
31 changes: 31 additions & 0 deletions
31
integration-tests/gatsby-pipeline/src/pages/fetch-remote-d.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react" | ||
import { graphql, Link } from "gatsby" | ||
|
||
import Layout from "../components/layout" | ||
import SEO from "../components/seo" | ||
|
||
const FetchRemoteB = ({ data }) => { | ||
return ( | ||
<Layout> | ||
<SEO title="Fetch Remote B" /> | ||
|
||
<pre | ||
dangerouslySetInnerHTML={{ __html: JSON.stringify(data, null, 2) }} | ||
/> | ||
<Link to="/">Go back to the homepage</Link> | ||
</Layout> | ||
) | ||
} | ||
|
||
export default FetchRemoteB | ||
|
||
export const pageQuery = graphql` | ||
{ | ||
allMyRemoteFile { | ||
nodes { | ||
url | ||
publicUrl | ||
} | ||
} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
module.exports = { | ||
testPathIgnorePatterns: [`/node_modules/`, `__tests__/fixtures`, `.cache`], | ||
bail: true, | ||
moduleNameMapper: { | ||
"^gatsby-core-utils/(.*)$": `gatsby-core-utils/dist/$1`, // Workaround for https://github.com/facebook/jest/issues/9771 | ||
}, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@pieh any better tips to test concurrency stuff 😂