I have large static files which should be busted with a hash for HTTP-caching. If I put them into the public directory, vite only copies them without appending a hash to the filenames. If I put them into the assets directory, vite ignores them, because they are not referenced directly from code, but loaded via XHR requests.
The directory structure is pretty standard:
/
├─ src/
│ ├─ assets/
│ │ ├─ locales/
│ │ │ ├─ en.json
│ │ │ ├─ de.json
│ │ │ ├─ ru.json
│ ├─ main.js
├─ public/
├─ dist/
├─ index.html
How do I tell vite to copy those files with hash added to filenames and how do I get the resulting filenames to use them in XHR requests?
One solution is to use the
urlsuffix to import the.json:If there are many locale files, you can import them all with
import.meta.glob:If your files are less than 4KB, they'll be inlined as a data URL, and you won't see the corresponding file in your build output. To disable this inlining, set
build.assetsInlineLimitto0:Also, to keep the built locale files under
dist/locales, you'd have to configure the asset naming:demo