Skip to content

Commit

Permalink
feat(gatsby): use json-stream-stringify to serialize redux state (#9370)
Browse files Browse the repository at this point in the history
This PR changes the way the internal gatsby store is saved to a disk to account for sites that have very large payloads.

Closes issue #9362
  • Loading branch information
rametta authored and pieh committed Oct 29, 2018
1 parent a0f404d commit c334075
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Expand Up @@ -75,7 +75,7 @@
"jest-worker": "^23.2.0",
"joi": "12.x.x",
"json-loader": "^0.5.7",
"json-stringify-safe": "^5.0.1",
"json-stream-stringify": "^2.0.1",
"kebab-hash": "^0.1.2",
"lodash": "^4.17.10",
"md5": "^2.2.1",
Expand Down
21 changes: 14 additions & 7 deletions packages/gatsby/src/redux/index.js
Expand Up @@ -2,7 +2,7 @@ const Redux = require(`redux`)
const _ = require(`lodash`)
const fs = require(`fs`)
const mitt = require(`mitt`)
const stringify = require(`json-stringify-safe`)
const stringify = require(`json-stream-stringify`)

// Create event emitter for actions
const emitter = mitt()
Expand Down Expand Up @@ -78,12 +78,19 @@ const saveState = state => {
)
pickedState.components = mapToObject(pickedState.components)
pickedState.nodes = mapToObject(pickedState.nodes)
const stringified = stringify(pickedState, null, 2)
fs.writeFile(
`${process.cwd()}/.cache/redux-state.json`,
stringified,
() => {}
)

const writeStream = fs.createWriteStream(`${process.cwd()}/.cache/redux-state.json`)

new stringify(pickedState, null, 2, true)
.pipe(writeStream)
.on(`finish`, () => {
writeStream.destroy()
writeStream.end()
})
.on(`error`, () => {
writeStream.destroy()
writeStream.end()
})
}
const saveStateDebounced = _.debounce(saveState, 1000)

Expand Down
13 changes: 5 additions & 8 deletions yarn.lock
Expand Up @@ -2936,14 +2936,6 @@ babel-plugin-macros@^2.4.2:
cosmiconfig "^5.0.5"
resolve "^1.8.1"

babel-plugin-macros@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/babel-plugin-macros/-/babel-plugin-macros-2.4.2.tgz#21b1a2e82e2130403c5ff785cba6548e9b644b28"
integrity sha512-NBVpEWN4OQ/bHnu1fyDaAaTPAjnhXCEPqr1RwqxrU7b6tZ2hypp+zX4hlNfmVGfClD5c3Sl6Hfj5TJNF5VG5aA==
dependencies:
cosmiconfig "^5.0.5"
resolve "^1.8.1"

babel-plugin-react-css-modules@^3.2.1:
version "3.4.2"
resolved "https://registry.yarnpkg.com/babel-plugin-react-css-modules/-/babel-plugin-react-css-modules-3.4.2.tgz#4c1db8d4bc8b2973f6c689da7dbd56b0cb8f099c"
Expand Down Expand Up @@ -11331,6 +11323,11 @@ json-stable-stringify@^1.0.0:
dependencies:
jsonify "~0.0.0"

json-stream-stringify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/json-stream-stringify/-/json-stream-stringify-2.0.1.tgz#8bc0e65ff94567d9010e14c27c043a951cb14939"
integrity sha512-5XymtJXPmzRWZ1UdLQQQXbjHV/E7NAanSClikEqORbkZKOYLSYLNHqRuooyju9W90kJUzknFhX2xvWn4cHluHQ==

json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
Expand Down

0 comments on commit c334075

Please sign in to comment.