Skip to content

Commit 8105be6

Browse files
oorestisimeDSchau
authored andcommitted
feat(gatsby-source-filesystem): add optional name parameter to createRemoteFileNode (#11054)
Fix: #11037 <!-- Have any questions? Check out the contributing docs at https://gatsby.app/contribute, or ask in this Pull Request and a Gatsby maintainer will be happy to help :) --> ## Description Take an optional name parameter to fix issue with temporary urls having no "guessable" name <!-- Write a brief description of the changes introduced by this PR --> ## Related Issues <!-- Link to the issue that is fixed by this PR (if there is one) e.g. Fixes #1234, Addresses #1234, Related to #1234, etc. -->
1 parent 549b8ac commit 8105be6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/gatsby-source-filesystem/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ exports.downloadMediaFiles = ({
227227

228228
The file node can then be queried using GraphQL. See an example of this in the [gatsby-source-wordpress README](/packages/gatsby-source-wordpress/#image-processing) where downloaded images are queried using [gatsby-transformer-sharp](/packages/gatsby-transformer-sharp/) to use in the component [gatsby-image](/packages/gatsby-image/).
229229

230-
#### Retrieving the remote file extension
230+
#### Retrieving the remote file name and extension
231231

232-
The helper tries first to retrieve the file extension by parsing the url and the path provided (e.g. if the url is https://example.com/image.jpg, the extension will be inferred as `.jpg`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the extension _can_ be explicitly passed, like so:
232+
The helper tries first to retrieve the file name and extension by parsing the url and the path provided (e.g. if the url is https://example.com/image.jpg, the extension will be inferred as `.jpg` and the name as `image`). If the url does not contain an extension, we use the [`file-type`](https://www.npmjs.com/package/file-type) package to infer the file type. Finally, the name and the extension _can_ be explicitly passed, like so:
233233

234234
```javascript
235235
createRemoteFileNode({
@@ -241,5 +241,6 @@ createRemoteFileNode({
241241
createNodeId,
242242
// if necessary!
243243
ext: ".jpg",
244+
name: "image",
244245
})
245246
```

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ async function processRemoteNode({
185185
auth = {},
186186
createNodeId,
187187
ext,
188+
name,
188189
}) {
189190
// Ensure our cache directory exists.
190191
const pluginCacheDir = path.join(
@@ -211,7 +212,9 @@ async function processRemoteNode({
211212

212213
// Create the temp and permanent file names for the url.
213214
const digest = createHash(url)
214-
const name = getRemoteFileName(url)
215+
if (!name) {
216+
name = getRemoteFileName(url)
217+
}
215218
if (!ext) {
216219
ext = getRemoteFileExtension(url)
217220
}
@@ -313,6 +316,7 @@ module.exports = ({
313316
auth = {},
314317
createNodeId,
315318
ext = null,
319+
name = null,
316320
}) => {
317321
// validation of the input
318322
// without this it's notoriously easy to pass in the wrong `createNodeId`
@@ -355,6 +359,7 @@ module.exports = ({
355359
createNodeId,
356360
auth,
357361
ext,
362+
name,
358363
})
359364

360365
fileDownloadPromise.then(() => bar.tick())

0 commit comments

Comments
 (0)