Reputation: 65
Problem: I'm encountering a persistent issue with my Vite-React project deployment. Whenever I deploy a new version, the changes are not immediately reflected on the production URL. Instead, they only appear after performing a hard reload. This seems to be a cache-related problem.
Details: I have a Vite-React project deployed in a production environment. However, whenever I push updates and redeploy the application, users visiting the site often don't see the latest changes until they perform a hard reload (Ctrl + Shift + R or Cmd + Shift + R). This indicates that there's a caching issue preventing the new version from being fetched and displayed immediately.
Steps Taken: I've tried various methods to tackle this problem, including:
Expected Outcome: I expect that whenever I deploy a new version of my Vite-React project, the changes should be immediately visible to users without requiring them to perform a hard reload. The deployment process should handle cache invalidation effectively to ensure seamless updates.
Seeking Solution: I'm looking for insights and recommendations on how to effectively manage browser cache invalidation in my Vite-React project deployment. Specifically, I need guidance on configuring Vite or implementing strategies to ensure that new versions are fetched and displayed automatically without relying on users to perform manual cache clearing.
Upvotes: 4
Views: 1278
Reputation: 1
You can use service worker capabilities on caching. The basic flow would allow you to hop callback that would be triggered upon new build files arriving. There you can choose to either force refresh the page / prompt user to refresh for a new version / etc
import {Workbox} from 'workbox-window';
export default function registerSW() {
//! running SW in dev mode can cause several problems due to
//! caching on every update, so use this commented out code
// if (process.env.NODE_ENV !== 'production') {
// return;
// }
if ('serviceWorker' in navigator) {
const wb = new Workbox('sw.js');
wb.addEventListener('installed', (e) => {
if (e.isUpdate && confirm('New app update is available, click to update')) {
window.location.reload();
}
});
wb.register();
}
}
import {clientsClaim} from 'workbox-core';
import {precacheAndRoute} from 'workbox-precaching';
clientsClaim();
self.skipWaiting();
precacheAndRoute(self.__WB_MANIFEST);
and ofcource registering it in the index file.
Note that this is just an example that I did like 2 years ago so the flow on the newer versions on workbox libraries may vary
Upvotes: 0