Next.js/Vercel new deployment notification.
Does anyone know how the update notification/toast on T3 chat is implemented?
By default when there's new update the page just automatically refreshes.
I would like to display the update toast to the users with the refresh button so that they can manually refresh the page instead of it refreshing automatically.
8 Replies
Sonner
An opinionated toast component for React.
lmk if you need help with the logic, or if your app isn’t react
but yeah this or the more-deprecated toast component from shadcn is most certainly what they are using
(please @ me or reply)
Oh I use Sonner as well, i didn’t mean the toast itself, I meant the logic that fires up the toast.
So far i managed to implement it with service workers.
so previously when there was a new page the users’ pages would refresh automatically? and you weren’t doing that yourself?
Yes, that’s how vercel seams to working by default.
Like any deployment update would trigger a refresh
Huh. I had no idea that was a thing. How’d you do it yourself?
I just used augment ai, it took a whole day of trial and error, honestly i should have just gone and read some documentation and figure it out myself but i was too lazy
I’ll add the code here tomorrow so that anyone can use it if they need it.
@Nate Here's the code:
So essentaily how this works is:
The UpdateNotification will register the service worker and check if the service worker file updated (sw.js file).
If it updated it will display the notification.
The generateBuildId method updates the sw.js file with a new build id every time a new build is created, but only on vercel since we don't want to change the file locally.
This way you don't need server code that would notify the client that there's an update.
It's a static file so it shouldn't count towards the monthly cost.
new UpdateNotification.tsx code, the previous one kept displaying the toast if you manually reloaded.
It would only go away if you pressed the refresh button on the toast.