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

Page refresh loses hashes after service worker update #25889

Closed
ediblecode opened this issue Jul 20, 2020 · 9 comments · Fixed by #32323
Closed

Page refresh loses hashes after service worker update #25889

ediblecode opened this issue Jul 20, 2020 · 9 comments · Fixed by #32323
Labels
status: needs core review Currently awaiting review from Core team member type: bug An issue or pull request relating to a bug in Gatsby

Comments

@ediblecode
Copy link
Contributor

Description

I'm linking between pages using a hash to link to a specific section. After a site re-deploy, which I think in turn triggers a service worker update, the page reloads after navigation and loses the hash. Which in turns mean a user isn't taken to the section related to the link they clicked.

From digging around, I think it's caused by this:

// If we had a service worker update, no matter the path, reload window and
// reset the pathname whitelist
if (window.___swUpdated) {
window.location = pathname
return
}

Where the ___swUpdated comes from:

// We set a flag so Gatsby Link knows to refresh the page on next navigation attempt
window.___swUpdated = true
// We call the onServiceWorkerUpdateReady API so users can show update prompts.
apiRunner(`onServiceWorkerUpdateReady`, { serviceWorker: reg })
.

I think I might be able to work round it by using onServiceWorkerUpdateReady to reload the window myself (keeping the hash), or by setting window.___swUpdated = false like this blog post. But this seems wrong to have to do this, when the default behaviour is to refresh after an update - it feels like navigation.js should reload the page, and keep the hash. Maybe by using window.reload(true) rather than setting window.location?

-window.location = pathname
+window.reload(true)

Steps to reproduce

  1. Add gatsby-plugin-offline to a new gatsby site
  2. Create a site with 2 pages, page1.html and page 2.html
  3. Add link (using gatsby Link) from page1.html to page 2 with a hash e.g. page2.html#a-section
  4. Build the site
  5. Navigate to page1, click the link to page 2
  6. Make a small code change and build the site again
  7. Navigate to page1, click the link to page 2

Expected result

The page reloads, because of a service worker update, with the same URL, including the hash.

Actual result

The page reloads, but excludes the hash and just uses the pathname.

Environment

n/a

@ediblecode ediblecode added the type: bug An issue or pull request relating to a bug in Gatsby label Jul 20, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 20, 2020
@ediblecode
Copy link
Contributor Author

This is kind of related to #25778 in a way as they're both dealing with hashes in links. Just in case anyone else has issues using hash links...

@madalynrose madalynrose added status: needs core review Currently awaiting review from Core team member and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 21, 2020
@madalynrose
Copy link
Contributor

I'm sorry you're running into this issue @ediblecode! Thanks for lettings us know! It would be really helpful if you could provide a minimal reproduction. I'm tagging in @moonmeister who might have more context on gatsby-plugin-offline and service workers but it'd be easiest for us to help debug if we could see a live reproduction of the issue in something like codesandbox.

@ediblecode
Copy link
Contributor Author

ediblecode commented Jul 21, 2020 via email

@ediblecode
Copy link
Contributor Author

Ok, I've create a minimal repro repo, see https://github.com/ediblecode/gatsby-hash-repro.

  1. Git clone
  2. npm run build
  3. npm run serve
  4. Open localhost:9000
  5. Click the link to page 2 - the url should still contain #test
  6. Stop the serve command, and make a simple content change to any file
  7. Repeat steps 2-5 only this time page2.html will reload and lose the hash

There must be a build hash used somewhere for the service worker as you need to make a content change to see trigger a service worker update.

I hope that helps @madalynrose and @moonmeister but more than happy to add more details/screenshare to demo if needs be.

@wpj
Copy link

wpj commented Jul 23, 2020

There may be other aspects to this that I haven't considered, but I think setting window.location to the original value of to passed to the navigate function instead of the parsed pathname would fix this:

const navigate = (to, options = {}) => {

@ediblecode
Copy link
Contributor Author

I think you're right @wpj. I initially thought window.reload(true) would do the trick but that would reload the current page rather than the destination page, so you're right that this makes more sense:

-window.location = pathname
+window.location = to

I did wonder if this wouldn't work if there was a prefixed path (sub folder) but looks like the prefixing is done in gatby-link itself:

const prefixedTo = rewriteLinkPath(to, location.pathname)

so by the time it get's to navigate then it's already prefixed, so just using to seems sensible to me.

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Aug 14, 2020
@ediblecode
Copy link
Contributor Author

Definitely not stale

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Aug 14, 2020
@Auspicus
Copy link
Contributor

@ediblecode Don't know if this is still an issue for you but #32323 should address the issue. It also handles query parameters getting dropped which was the bug we had that caused us to look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs core review Currently awaiting review from Core team member type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants