-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Document how to detect SW updates and alert users #9087
Comments
Same here |
Right now workflow is like that - when there is SW update, gatsby will just set flag to reload page on next navigation (so it doesn't refresh itself which would cause weird UX) You can hook into |
@pieh do I understand correctly, that with every new gatsby build the service worker gets the update and I don't need to worry about my users getting an old version of the webiste? I couldn't find anything about it in the gatsby-offline-plugin docs. |
@rwieruch Same problem here. On libresat.space (sources here), the old content still gets served (deployed using Netlify). Some docs would be awesome. |
Well, found it out. It's very simple actually! // gatsby-browser.js
exports.onServiceWorkerUpdateFound = () => {
if (
window.confirm(
"This site has been updated with new data. Do you wish to reload the site to get the new data?"
)
) {
window.location.reload(true);
}
}; This shows a little modal that reloads the site if the user pressed "Yes". See learn-chinese.tk (sources here) for a demo. |
bad UX in my opinion, the user will keep seeing modals every time a new content is available |
I think I will just disable the offline plugin before launching a new website/application. Heard about too many launches that went wrong or website still serving old content due to service workers. Not necessarily related to Gatsby, but maybe just avoiding service workers for now is a better option. |
Well, another option would be to simply reload the page without any confirm: // gatsby-browser.js
exports.onServiceWorkerUpdateFound = () => window.location.reload(true); I prefer the modal though. I've seen this a lot on other sites as well - on a slow mobile connection you don't want to reload the whole page, after all that's what serviceworkers are for! |
Closes #9087 I also deleted `/docs/add-offline-support/` since it just redirects to `/docs/add-offline-support-with-a-service-worker/` anyway, and hasn't been updated in a while.
The discussion in this thread has been helpful - thanks! Using the method in the previous comment, I'm seeing the following behaviour after service worker update:
I would expect the content to be updated in step 2. Am I missing something? My code (from @pojntfx's comment above): // gatsby-browser.js
exports.onServiceWorkerUpdateFound = () => window.location.reload(true); I would actually prefer the user alert method, but this turned out to be problematic because of #10432 (comment). |
Hi @laradevitt, I believe the problems you've described should be fixed in #10432 - hopefully this should be merged soon so please could you have another look once this is ready? Thanks! |
@davidbailey00 - Thanks! Yes, I mentioned that PR above. Was looking for a quick fix in the meantime but I guess I should just be more patient. :) If there is anything I can do to help test, let me know. |
…#10417) Closes gatsbyjs#9087 I also deleted `/docs/add-offline-support/` since it just redirects to `/docs/add-offline-support-with-a-service-worker/` anyway, and hasn't been updated in a while.
Hey, @davidbailey00 - Noting that #10432 had been merged I updated all packages and the problem as described in my previous comment no longer exists. I'll note here, in case somebody else stumbles upon this issue, that this: exports.onServiceWorkerUpdateFound = () => window.location.reload(true); should now be: exports.onServiceWorkerUpdateReady= () => window.location.reload(true); Works a charm. Thx. :) |
I dont get it... do I need both handlers? |
@diegodorado No, the second replaces the first. |
That is the same issue I am having, cache is not updating new content, annoying as hell. |
it's interesting since when I do
|
@cxspxr - AFAIK code in gatsby-browser.js shouldn't be applied during build time. Is that where you're putting it? |
@laradevitt yes. It's strange but problem resolved on freshly cloned project. No tracked changes in old folder |
A good habit that saves me a lot of headache on Gatsby projects is to always use |
I frequently observed troubles with I used the Notification API so that it is opt-in and non-invasive for the user (unlike The implementation looks as follows, in case anyone is interested: import { ServiceWorkerArgs } from "gatsby"
export const onServiceWorkerUpdateReady = async (args: ServiceWorkerArgs) => {
const permissionResponse = await Notification.requestPermission()
if (permissionResponse === "granted") {
await args.serviceWorker.showNotification("Website update", {
body:
"Our website just got a little bit better. We reloaded the site with the update to ensure a smooth experience for you."
})
}
window.location.reload(true)
} You can find information about adapting the behavior of notifications here. In case you wonder how to use TypeScript for Gatsby APIs, the most comprehensive real-world example I know is this website. |
I have the same issue. I need to perform a hard reload on the page to get new content. I've tried adding this to gatsby-browser.js: |
in |
@nibtime |
@ali4heydari However, I think that browsers do not perform the necessary Shift + F5 refresh if the When calling Removing the This problem is mentioned in an answer to the same stackoverflow question you posted. It suggests an alternative solution involving a Therefore, I keep using the |
@nibtime I agree, but I don't fully understand the last part: is this issue specific to |
ProblemThe problem with Using it almost guarantees that repeat visitors will get "stuck" on old builds of the site and won't be able to get the latest version without the help of someone walking them through developer tools to manually clear out cache and remove registered service workers. I've personally had to replace WorkaroundsCurrently the only way for developers to resolve this issue is to add hacks in I see this hack in export const onRouteUpdate = () => {
navigator.serviceWorker.register('/sw.js').then((reg) => {
reg.update();
});
};
export const onServiceWorkerUpdateReady = () => {
window.location.reload(true)
}; Others use a combination of a state update in export const onRouteUpdate = () => {
navigator.serviceWorker.register('/sw.js').then((reg) => {
reg.update();
});
};
export const onServiceWorkerUpdateReady = () => {
document.getElementById('___gatsby').setAttribute('data-update-available', 'true');
console.info('PWA update available.');
}; export const UpdateButton = () => {
const [isUpdateAvailable, setIsUpdateAvailable] = useState(false)
useEffect(() => {
const interval = setInterval(() => {
const isUpdateAvailable =
document.getElementById('___gatsby').dataset.updateAvailable === 'true';
if (isUpdateAvailable) {
setIsUpdateAvailable(true);
}
}, 1000);
return () => {
clearInterval(interval);
};
}, []);
return isUpdateAvailable ? <button onClick={() => window.location.reload(true)}>An update is available. Click to update the website.</button> : null
} ConclusionEveryone that uses |
@martingronlund @sandren |
@nibtime Thanks! I agree that Zustand seems like a great choice for implementing this. I look forward to seeing your solutions! Is there a repository I can follow? 😀 |
@sandren Thanks for your interest! Not yet, I am still experimenting with Gatsby Themes in a private repo, and also another theme has currently a higher priority for me (to add a Next.JS getStaticProps-ish per-page level abstraction for data fetching and proper content i18n to Gatsby). I will eventually create a public monorepo with a single semantic release process for all my Gatsby Themes and other libraries I make. I am using TypeScript all the way and rely heavily on my Gatsby with
|
I am using the gatsby-plugin-offline plugin and would like to know if there is a way to handle updates.
I would like to display a message if a new version is available like
https://medium.com/progressive-web-apps/pwa-create-a-new-update-available-notification-using-service-workers-18be9168d717
At the moment I need to hard reload the site in order to see changes.
How to handle this?
Thanks in advance!
The text was updated successfully, but these errors were encountered: