Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): refresh browser if webpack rebuild occurs #13871

Merged
merged 14 commits into from
May 17, 2019
Merged
Prev Previous commit
Next Next commit
reload browser if rebuild occurs in background
  • Loading branch information
Moocar committed May 16, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 84b9edd7692c6a73c6553a861bbc12ca7482d15f
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
@@ -290,6 +290,7 @@ const queue = {
const page = {
componentChunkName: pageData.componentChunkName,
path: pageData.path,
webpackCompilationHash: pageData.webpackCompilationHash,
}

const jsonData = pageData.result
11 changes: 11 additions & 0 deletions packages/gatsby/cache-dir/navigation.js
Original file line number Diff line number Diff line change
@@ -82,6 +82,17 @@ const navigate = (to, options = {}) => {
}, 1000)

loader.loadPage(pathname).then(pageResources => {
// If the loaded page has a different compilation hash to the
// window, then a rebuild has occurred on the server. Reload.
if (process.env.NODE_ENV === `production` && pageResources) {
if (
pageResources.page.webpackCompilationHash !==
window.___webpackCompilationHash
) {
console.log(`Site has changed on server. Reloading browser`)
window.location = pathname
}
}
reachNavigate(to, options)
clearTimeout(timeoutId)
})
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import matchPaths from "./match-paths.json"
window.asyncRequires = asyncRequires
window.___emitter = emitter
window.___loader = loader
window.___webpackCompilationHash = window.webpackCompilationHash

loader.addProdRequires(asyncRequires)
setApiRunnerForLoader(apiRunner)
6 changes: 4 additions & 2 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
@@ -338,15 +338,17 @@ export default (pagePath, callback) => {
}
})

const webpackCompilationHash = pageData.webpackCompilationHash

// Add page metadata for the current page
const windowPagePath = `/*<![CDATA[*/window.pagePath="${pagePath}";/*]]>*/`
const windowPageData = `/*<![CDATA[*/window.pagePath="${pagePath}";window.webpackCompilationHash="${webpackCompilationHash}";/*]]>*/`

postBodyComponents.push(
<script
key={`script-loader`}
id={`gatsby-script-loader`}
dangerouslySetInnerHTML={{
__html: windowPagePath,
__html: windowPageData,
}}
/>
)
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/page-data.js
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ const write = async ({ publicDir }, page, result, webpackCompilationHash) => {
const body = {
componentChunkName: page.componentChunkName,
path: page.path,
compilationHash: webpackCompilationHash,
webpackCompilationHash,
result,
}
await fs.outputFile(filePath, JSON.stringify(body))