skip to Main Content

So I’ve been told by the project manager of this project I’m volunteering for that keeping names for files like index-xxxx.js will help to force the cache to clear if we use them in like the root index.html file inside a function in a script tag. I’ve also been told to copy the contents of these files and save it as a file with a consistent name. When I got told this I was like why would copying the contents of generated files help at all?

For reference the project is using Vite and React. I know vite generates a dist folder with a bunch of assets and the index-xxxx.js file is one of these assets. I am fairly certain that trying to use these files in the root index.html file will not work because every time the project gets built the files will change names and then break the build. So is there something I’m missing here?

Also any resources that explicitly states that generated files that should not be used outside of the dist folder would be greatly appreciated. I need to be 100% certain about these generated files.

file structure I hope this helps.

Project_folder
     
      dist
          assets
             index-xxxx.js
             index-xxxx.css
        index.html
      src

      index.html

2

Answers


  1. Yes, it is necessary to guarantee a cache clear on the user end.

    To put it simply, a web host can do everything perfect on their end (Setting headers, sending cookies, etc.) to force the cache to clear on the user’s end (Including possible CDN caches in the middle). But there’s no guarantee that the User Agent will comply and clear the cache. Using a unique file name solves this problem.

    What I think your missing is that when the project is built all the references to those file names will be updated everywhere that references them. So, the root html file will have its contents updated accordingly.

    Login or Signup to reply.
  2. Using versioned filenames like index-xxxx.js is essential for cache busting, ensuring that browsers load the latest version of your files when the content changes. Vite handles this automatically, updating the references in the generated index.html. Copying contents to files with consistent names (like index.js) and using these names will cause caching issues, as the browser might serve outdated content, and it can break the build process since Vite’s generated filenames change with each build.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search