4

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:

  1. Adding cache-control headers in the server configuration.
  2. Attempting to bust cache by changing the version data of package.json.

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.

2
  • Did you find solution to this? Commented Jun 18, 2024 at 11:33
  • 1
    The root of my issue was that the index.html file was being cached, which prevented the new css and js files from being downloaded despite having unique hashes. I added Cache-Control "max-age=0, must-revalidate" and the issue when away Commented Jan 11 at 20:04

2 Answers 2

1

used React Cache Buster and this solved my problem.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.