Skip to content

Commit

Permalink
write out page-data in query runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Moocar committed Apr 4, 2019
1 parent 1b5f5f5 commit ea27723
Showing 2 changed files with 43 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/gatsby/src/query/query-runner.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ const { generatePathChunkName } = require(`../utils/js-chunk-names`)
const { formatErrorDetails } = require(`./utils`)
const mod = require(`hash-mod`)(999)
const { boundActionCreators } = require(`../redux/actions`)
const pageDataUtil = require(`../utils/page-data`)

const resultHashes = {}

@@ -91,20 +92,23 @@ ${formatErrorDetails(errorDetails)}`)
.createHash(`sha1`)
.update(resultJSON)
.digest(`base64`)

if (resultHashes[queryJob.id] !== resultHash) {
resultHashes[queryJob.id] = resultHash

// Remove potentially unsafe characters. This increases chances of collisions
// slightly but it should still be very safe + we get a shorter
// url vs hex.
.replace(/[^a-zA-Z0-9-_]/g, ``)
const readableResultHash = resultHash.replace(/[^a-zA-Z0-9-_]/g, ``)

let dataPath
if (queryJob.isPage) {
dataPath = `${generatePathChunkName(queryJob.jsonName)}-${resultHash}`
} else {
dataPath = queryJob.hash
}
let dataPath
if (queryJob.isPage) {
const pathChunkName = generatePathChunkName(queryJob.jsonName)
dataPath = `${pathChunkName}-${readableResultHash}`
} else {
dataPath = queryJob.hash
}

if (resultHashes[queryJob.id] !== resultHash) {
resultHashes[queryJob.id] = resultHash
let modInt = ``
// We leave StaticQuery results at public/static/d
// as the babel plugin has that path hard-coded
@@ -136,6 +140,15 @@ ${formatErrorDetails(errorDetails)}`)
value: dataPath,
},
})

// Save page-data.json. This isn't used yet but is part of
// https://github.com/gatsbyjs/gatsby/pull/13004
if (queryJob.isPage) {
const publicDir = path.join(program.directory, `public`)
const { pages } = store.getState()
const page = pages.get(queryJob.id)
await pageDataUtil.write({ publicDir }, page, result)
}
}

boundActionCreators.pageQueryRun({
21 changes: 21 additions & 0 deletions packages/gatsby/src/utils/page-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require(`fs-extra`)
const path = require(`path`)

const getFilePath = ({ publicDir }, pagePath) => {
const fixedPagePath = pagePath === `/` ? `index` : pagePath
return path.join(publicDir, `page-data`, fixedPagePath, `page-data.json`)
}

const write = async ({ publicDir }, page, result) => {
const filePath = getFilePath({ publicDir }, page.path)
const body = {
componentChunkName: page.componentChunkName,
path: page.path,
result,
}
await fs.outputFile(filePath, JSON.stringify(body))
}

module.exports = {
write,
}

0 comments on commit ea27723

Please sign in to comment.