Repository: gatsbyjs/gatsby
File: packages/gatsby/cache-dir/loader.js (JavaScript)

Code before:
            )

            // TODO: Get from cache

            // TODO: Get in flight Promise

            return doFetch(`/static/d/${staticQueryHash}.json`).then(req => {
              const jsonPayload = JSON.parse(req.responseText)
              console.log(
                `Static query with hash ${staticQueryHash} was fetched`,
                jsonPayload
              )
              // this.staticQueryDb.set(staticQueryHash, jsonPayload)
              return { staticQueryHash, jsonPayload }
            })
          })
        ).then(staticQueryResults => {
          console.log({
            staticQueryResults,
          })
          const staticQueryResultsMap = staticQueryResults.reduce(
            (map, { staticQueryHash, jsonPayload }) => {
              map[staticQueryHash] = jsonPayload
              return map
            },
            {}
          )
          // emitter.emit(`onPostLoadStaticQueryResults`, {
          //   staticQueryResultsMap,
          // })
          this.staticQueryDb.set(pagePath, staticQueryResultsMap)
          return staticQueryResultsMap
        })

        return Promise.all([
          componentChunkPromise,
          staticQueryBatchPromise,
        ]).then(([pageResources, staticQueryResults]) => {
          return {
            ...pageResources,
            staticQueryResults,
          }
        })
      })
      // prefer duplication with then + catch over .finally to prevent problems in ie11 + firefox
      .then(response => {
        this.inFlightDb.delete(pagePath)
        return response
      })
      .catch(err => {
        this.inFlightDb.delete(pagePath)

Review comment (suggestion):
Mandatory "reduce" suggestion here
```suggestion
          const staticQueryResultsMap = {}
          staticQueryResults.forEach(
            ({ staticQueryHash, jsonPayload }) =>
              staticQueryResultsMap[staticQueryHash] = jsonPayload
```

Code after:
          let pageResources
          if (!component) {
            finalResult.status = PageResourceStatus.Error
          } else {
            finalResult.status = PageResourceStatus.Success
            if (result.notFound === true) {
              finalResult.notFound = true
            }
            pageData = Object.assign(pageData, {
              webpackCompilationHash: allData[0]
                ? allData[0].webpackCompilationHash
                : ``,
            })
            pageResources = toPageResources(pageData, component)
          }
          // undefined if final result is an error
          return pageResources
        }
      )

      const staticQueryBatchPromise = Promise.all(
        staticQueryHashes.map(staticQueryHash => {
          // Check for cache in case this static query result has already been loaded
          if (this.staticQueryDb.has(staticQueryHash)) {
            const jsonPayload = this.staticQueryDb.get(staticQueryHash)
            return { staticQueryHash, jsonPayload }
          }

          return this.memoizedGet(
            `${__PATH_PREFIX__}/static/d/${staticQueryHash}.json`
          ).then(req => {
            const jsonPayload = JSON.parse(req.responseText)
            return { staticQueryHash, jsonPayload }
          })
        })
      ).then(staticQueryResults => {
        const staticQueryResultsMap = {}

        staticQueryResults.forEach(({ staticQueryHash, jsonPayload }) => {
          staticQueryResultsMap[staticQueryHash] = jsonPayload
          this.staticQueryDb.set(staticQueryHash, jsonPayload)
        })

        return staticQueryResultsMap
      })

      return Promise.all([componentChunkPromise, staticQueryBatchPromise]).then(
        ([pageResources, staticQueryResults]) => {
          let payload
          if (pageResources) {
            payload = { ...pageResources, staticQueryResults }