Skip to content

Commit

Permalink
feat(socket-io): run processHotPageDataUpdate if modules changed
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jul 27, 2020
1 parent 4cd850e commit 7d13f07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 23 additions & 4 deletions packages/gatsby/cache-dir/socketIo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { reportError, clearError } from "./error-overlay-handler"
import normalizePagePath from "./normalize-page-path"
import { processHotPageDataUpdate } from "./loader"

let socket = null

Expand Down Expand Up @@ -31,6 +32,7 @@ export default function socketIo() {
}

socket.on(`message`, msg => {
let shouldEmit = true
if (msg.type === `staticQueryResult`) {
if (didDataChange(msg, staticQueryData)) {
staticQueryData = {
Expand All @@ -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`) {
Expand All @@ -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)
}
})
Expand Down

0 comments on commit 7d13f07

Please sign in to comment.