Skip to content

Commit 2b4800a

Browse files
authored
feat(gatsby-source-filesystem): env flag to placeholder remote assets (#27663)
* feat(gatsby-source-filesystem): add experimental env flag to use placeholder for remote asset * Split up placeholder from remote * reduce patch
1 parent ae2c3da commit 2b4800a

1 file changed

Lines changed: 74 additions & 14 deletions

File tree

packages/gatsby-source-filesystem/src/create-remote-file-node.js

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ let bar
2121
// Keep track of the total number of jobs we push in the queue
2222
let totalJobs = 0
2323

24+
let showFlagWarning = !!process.env.GATSBY_EXPERIMENTAL_REMOTE_FILE_PLACEHOLDER
25+
2426
/********************
2527
* Type Definitions *
2628
********************/
@@ -205,6 +207,48 @@ async function processRemoteNode({
205207
createNodeId,
206208
ext,
207209
name,
210+
}) {
211+
let filename
212+
if (process.env.GATSBY_EXPERIMENTAL_REMOTE_FILE_PLACEHOLDER) {
213+
filename = await fetchPlaceholder({
214+
fromPath: process.env.GATSBY_EXPERIMENTAL_REMOTE_FILE_PLACEHOLDER,
215+
url,
216+
cache,
217+
ext,
218+
name,
219+
})
220+
} else {
221+
filename = await fetchRemoteNode({
222+
url,
223+
cache,
224+
auth,
225+
httpHeaders,
226+
ext,
227+
name,
228+
})
229+
}
230+
231+
// Create the file node.
232+
const fileNode = await createFileNode(filename, createNodeId, {})
233+
fileNode.internal.description = `File "${url}"`
234+
fileNode.url = url
235+
fileNode.parent = parentNodeId
236+
// Override the default plugin as gatsby-source-filesystem needs to
237+
// be the owner of File nodes or there'll be conflicts if any other
238+
// File nodes are created through normal usages of
239+
// gatsby-source-filesystem.
240+
await createNode(fileNode, { name: `gatsby-source-filesystem` })
241+
242+
return fileNode
243+
}
244+
245+
async function fetchRemoteNode({
246+
url,
247+
cache,
248+
auth = {},
249+
httpHeaders = {},
250+
ext,
251+
name,
208252
}) {
209253
const pluginCacheDir = cache.directory
210254
// See if there's response headers for this url
@@ -237,7 +281,7 @@ async function processRemoteNode({
237281
// Fetch the file.
238282
const response = await requestRemoteNode(url, headers, tmpFilename, httpOpts)
239283

240-
if (response.statusCode == 200) {
284+
if (response.statusCode === 200) {
241285
// Save the response headers for future requests.
242286
await cache.set(cacheIdForHeaders(url), response.headers)
243287
}
@@ -267,18 +311,20 @@ async function processRemoteNode({
267311
await fs.remove(tmpFilename)
268312
}
269313

270-
// Create the file node.
271-
const fileNode = await createFileNode(filename, createNodeId, {})
272-
fileNode.internal.description = `File "${url}"`
273-
fileNode.url = url
274-
fileNode.parent = parentNodeId
275-
// Override the default plugin as gatsby-source-filesystem needs to
276-
// be the owner of File nodes or there'll be conflicts if any other
277-
// File nodes are created through normal usages of
278-
// gatsby-source-filesystem.
279-
await createNode(fileNode, { name: `gatsby-source-filesystem` })
314+
return filename
315+
}
280316

281-
return fileNode
317+
async function fetchPlaceholder({ fromPath, url, cache, ext, name }) {
318+
const pluginCacheDir = cache.directory
319+
const digest = createContentDigest(url)
320+
321+
if (!ext) {
322+
ext = getRemoteFileExtension(url)
323+
}
324+
325+
const filename = createFilePath(path.join(pluginCacheDir, digest), name, ext)
326+
fs.copySync(fromPath, filename)
327+
return filename
282328
}
283329

284330
/**
@@ -321,7 +367,7 @@ const pushTask = task =>
321367
* @param {CreateRemoteFileNodePayload} options
322368
* @return {Promise<Object>} Returns the created node
323369
*/
324-
module.exports = ({
370+
module.exports = function createRemoteFileNode({
325371
url,
326372
cache,
327373
createNode,
@@ -333,7 +379,21 @@ module.exports = ({
333379
ext = null,
334380
name = null,
335381
reporter,
336-
}) => {
382+
}) {
383+
if (showFlagWarning) {
384+
showFlagWarning = false
385+
// Note: This will use a placeholder image as the default for every file that is downloaded through this API.
386+
// That may break certain cases, in particular when the file is not meant to be an image or when the image
387+
// is expected to be of a particular type that is other than the placeholder. This API is meant to bypass
388+
// the remote download for local testing only.
389+
console.info(
390+
`GATSBY_EXPERIMENTAL_REMOTE_FILE_PLACEHOLDER: Any file downloaded by \`createRemoteFileNode\` will use the same placeholder image and skip the remote fetch. Note: This is an experimental flag that can change/disappear at any point.`
391+
)
392+
console.info(
393+
`GATSBY_EXPERIMENTAL_REMOTE_FILE_PLACEHOLDER: File to use: \`${process.env.GATSBY_EXPERIMENTAL_REMOTE_FILE_PLACEHOLDER}\``
394+
)
395+
}
396+
337397
// validation of the input
338398
// without this it's notoriously easy to pass in the wrong `createNodeId`
339399
// see gatsbyjs/gatsby#6643

0 commit comments

Comments
 (0)