From 7d13f07c9e2ee42ef719564d3d936fd8f8e4ad8a Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Sat, 18 Jul 2020 12:23:58 +0200 Subject: [PATCH] feat(socket-io): run processHotPageDataUpdate if modules changed --- packages/gatsby/cache-dir/loader.js | 9 +++++++++ packages/gatsby/cache-dir/socketIo.js | 27 +++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/cache-dir/loader.js b/packages/gatsby/cache-dir/loader.js index 983142d4a2bd6..a24e71ea790fd 100644 --- a/packages/gatsby/cache-dir/loader.js +++ b/packages/gatsby/cache-dir/loader.js @@ -555,3 +555,12 @@ export const publicLoader = { } export default publicLoader + +// separate export so it gets tree-shaken in production build +export function processHotPageDataUpdate(pageData) { + // We don't need to update `pageDB` or `pageDataDb` because modules + // are not stored in page resources. This function only triggers fetching + // and updating modules store. + + return instance.fetchModuleDependencies(pageData.moduleDependencies) +} diff --git a/packages/gatsby/cache-dir/socketIo.js b/packages/gatsby/cache-dir/socketIo.js index c645499a31e42..784d208c8ab21 100644 --- a/packages/gatsby/cache-dir/socketIo.js +++ b/packages/gatsby/cache-dir/socketIo.js @@ -1,5 +1,6 @@ import { reportError, clearError } from "./error-overlay-handler" import normalizePagePath from "./normalize-page-path" +import { processHotPageDataUpdate } from "./loader" let socket = null @@ -31,6 +32,7 @@ export default function socketIo() { } socket.on(`message`, msg => { + let shouldEmit = true if (msg.type === `staticQueryResult`) { if (didDataChange(msg, staticQueryData)) { staticQueryData = { @@ -40,9 +42,26 @@ export default function socketIo() { } } else if (msg.type === `pageQueryResult`) { if (didDataChange(msg, pageQueryData)) { - pageQueryData = { - ...pageQueryData, - [normalizePagePath(msg.payload.id)]: msg.payload.result, + if (msg.payload.result) { + const currentEntry = + pageQueryData[normalizePagePath(msg.payload.id)] + + // run `processPageData` if modules changed + if ( + !currentEntry || + JSON.stringify(currentEntry.moduleDependencies) !== + JSON.stringify(msg.payload.result.moduleDependencies) + ) { + shouldEmit = false + processHotPageDataUpdate(msg.payload.result).then(() => { + pageQueryData = { + ...pageQueryData, + [normalizePagePath(msg.payload.id)]: msg.payload.result, + } + + ___emitter.emit(msg.type, msg.payload) + }) + } } } } else if (msg.type === `overlayError`) { @@ -52,7 +71,7 @@ export default function socketIo() { clearError(msg.payload.id) } } - if (msg.type && msg.payload) { + if (msg.type && msg.payload && shouldEmit) { ___emitter.emit(msg.type, msg.payload) } })