Skip to content

Commit

Permalink
fix(gatsby-source-filesystem): fix extension guessing on warm cache (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh authored Jun 2, 2020
1 parent 565895b commit 93da8e1
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/gatsby-source-filesystem/src/create-remote-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const {
getRemoteFileName,
createFilePath,
} = require(`./utils`)
const cacheId = url => `create-remote-file-node-${url}`
const cacheIdForHeaders = url => `create-remote-file-node-headers-${url}`
const cacheIdForExtensions = url => `create-remote-file-node-extension-${url}`

let bar
// Keep track of the total number of jobs we push in the queue
Expand Down Expand Up @@ -208,7 +209,7 @@ async function processRemoteNode({
const pluginCacheDir = cache.directory
// See if there's response headers for this url
// from a previous request.
const cachedHeaders = await cache.get(cacheId(url))
const cachedHeaders = await cache.get(cacheIdForHeaders(url))

const headers = { ...httpHeaders }
if (cachedHeaders && cachedHeaders.etag) {
Expand Down Expand Up @@ -238,15 +239,22 @@ async function processRemoteNode({

if (response.statusCode == 200) {
// Save the response headers for future requests.
await cache.set(cacheId(url), response.headers)
await cache.set(cacheIdForHeaders(url), response.headers)
}

// If the user did not provide an extension and we couldn't get one from remote file, try and guess one
if (ext === ``) {
const buffer = readChunk.sync(tmpFilename, 0, fileType.minimumBytes)
const filetype = fileType(buffer)
if (filetype) {
ext = `.${filetype.ext}`
if (response.statusCode === 200) {
// if this is fresh response - try to guess extension and cache result for future
const buffer = readChunk.sync(tmpFilename, 0, fileType.minimumBytes)
const filetype = fileType(buffer)
if (filetype) {
ext = `.${filetype.ext}`
await cache.set(cacheIdForExtensions(url), ext)
}
} else if (response.statusCode === 304) {
// if file on server didn't change - grab cached extension
ext = await cache.get(cacheIdForExtensions(url))
}
}

Expand Down

0 comments on commit 93da8e1

Please sign in to comment.