diff --git a/packages/gatsby/src/internal-plugins/query-runner/redirects-writer.js b/packages/gatsby/src/internal-plugins/query-runner/redirects-writer.js index 8a12f9923d7e5..b31e3c7b8560c 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/redirects-writer.js +++ b/packages/gatsby/src/internal-plugins/query-runner/redirects-writer.js @@ -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 @@ -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) diff --git a/packages/gatsby/src/redux/reducers/redirects.js b/packages/gatsby/src/redux/reducers/redirects.js index aa6cca9ecdf3f..9cdd8eabcdb1e 100644 --- a/packages/gatsby/src/redux/reducers/redirects.js +++ b/packages/gatsby/src/redux/reducers/redirects.js @@ -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 }