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

Improve UX for service worker updates #6263

Merged
merged 2 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class GatsbyLink extends React.Component {
.split(`#`)
.slice(1)
.join(`#`)
const element = hashFragment ? document.getElementById(hashFragment) : null
const element = hashFragment
? document.getElementById(hashFragment)
: null
if (element !== null) {
element.scrollIntoView()
return true
Expand Down
17 changes: 12 additions & 5 deletions packages/gatsby/cache-dir/production-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ apiRunnerAsync(`onClientEntry`).then(() => {
if (redirect) {
pathname = redirect.toPath
}

// If we had a service worker update, no matter the path, reload window
if (window.GATSBY_SW_UPDATED) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pieh can you check this out when you get a chance? is this essentially what you had in mind?

window.location = pathname
}

const wl = window.location

// If we're already at this location, do nothing.
Expand All @@ -79,15 +85,16 @@ apiRunnerAsync(`onClientEntry`).then(() => {
? window.___history.replace
: window.___history.push

const historyNavigateAction = replace
? `REPLACE`
: `PUSH`
const historyNavigateAction = replace ? `REPLACE` : `PUSH`

// Start a timer to wait for a second before transitioning and showing a
// loader in case resources aren't around yet.
const timeoutId = setTimeout(() => {
emitter.emit(`onDelayedLoadPageResources`, { pathname })
apiRunner(`onRouteUpdateDelayed`, { location, action: historyNavigateAction })
apiRunner(`onRouteUpdateDelayed`, {
location,
action: historyNavigateAction,
})
}, 1000)

lastNavigateToLocationString = `${location.pathname}${location.search}${
Expand All @@ -99,7 +106,7 @@ apiRunnerAsync(`onClientEntry`).then(() => {
const loaderCallback = pageResources => {
if (!pageResources) {
// We fetch resources for 404 page in page-renderer.js. Calling it
// here is to ensure that we have needed resouces to render page
// here is to ensure that we have needed resouces to render page
// before navigating to it
loader.getResourcesForPathname(`/404.html`, loaderCallback)
} else {
Expand Down
5 changes: 2 additions & 3 deletions packages/gatsby/cache-dir/register-service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ if (`serviceWorker` in navigator) {
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and the fresh content will
// have been added to the cache.
// We reload immediately so the user sees the new content.
// This could/should be made configurable in the future.
window.location.reload()
// We set a flag so Gatsby Link knows to refresh the page on next navigation attempt
window.GATSBY_SW_UPDATED = true
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
Expand Down