Skip to content

Commit

Permalink
[json-loader] dont recompile on data change - part 2 (#4837)
Browse files Browse the repository at this point in the history
* prevent adding duplicate redirects

* don't write new `redirects.json` if redirects didn't change

prevents webpack recompilation on data change
  • Loading branch information
pieh authored and KyleAMathews committed Apr 5, 2018
1 parent e374818 commit 62754de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import _ from "lodash"
import crypto from "crypto"
import fs from "fs-extra"
import { store, emitter } from "../../redux/"
import { joinPath } from "../../utils/path"

let lastHash = null

const writeRedirects = async () => {
bootstrapFinished = true

Expand All @@ -11,6 +14,17 @@ const writeRedirects = async () => {
// Filter for redirects that are meant for the browser.
const browserRedirects = redirects.filter(r => r.redirectInBrowser)

const newHash = crypto
.createHash(`md5`)
.update(JSON.stringify(browserRedirects))
.digest(`hex`)

if (newHash === lastHash) {
return Promise.resolve()
}

lastHash = newHash

await fs.writeFile(
joinPath(program.directory, `.cache/redirects.json`),
JSON.stringify(browserRedirects, null, 2)
Expand Down
12 changes: 10 additions & 2 deletions packages/gatsby/src/redux/reducers/redirects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const _ = require(`lodash`)

module.exports = (state = [], action) => {
switch (action.type) {
case `CREATE_REDIRECT`:
return [...state, action.payload]
case `CREATE_REDIRECT`: {
if (!state.some(redirect => _.isEqual(redirect, action.payload))) {
// Add redirect only if it wasn't yet added to prevent duplicates
return [...state, action.payload]
}
return state
}

default:
return state
}
Expand Down

0 comments on commit 62754de

Please sign in to comment.