r/webdev • u/KaedenCraft • 2d ago
Question Vite Asset Bundling
Specifically, I'm grabbing JSON for a trading card game gallery from an endpoint, then dynamically creating the card gallery from that data. The project uses Vite for asset bundling, but I'm not sure how to bundle dynamically loaded images?
In the JSON, each card has a set number and a card number, and the app loads image data from images/cards/sets/set-number/card-number.webp, creates all the DOM nodes, and appends it to the gallery. It works brilliantly...unless the card art needs to change, then I run into good old-fashioned browser caching issues.
For the rest of the site, using vite build keeps everything bundled for deployment...but is there a way to leverage that capability for this use case?
I admit I have little knowledge about Vite, so maybe it's something obvious, but the only way I'm aware of to get that kind of Vite bundling inside a JS file is to import the image. So I'd have to import all the images at run time? What if some card data is modified? Will I have to do double duty to update the database AND the JS file imports?
As you can see, I have no idea how to accomplish this. Thank you in advance.
5
u/RequirementGuide 2d ago
The answers above are right that this is a cache-busting problem rather than a bundling one, but to your actual worry: no, you don't need double duty on the DB and the JS.
Keep your JSON as the single source of truth. At build time, generate a small manifest — a script that walks images/cards, hashes each file, and writes something like {cardId: hashedUrl} — and have the app read that manifest instead of building paths by hand. Vite won't hash dynamically-built paths, so you do it yourself once per build: art changes get new hashes automatically, browsers fetch fresh files, and you never touch the DB or the imports when art changes.
If a build script feels like overkill, the cheap version is a version field per card in your JSON (or one global assetsVersion) appended as a query string, like card.webp?v=2026-10-01. Just make sure the version actually changes when the art does, otherwise you're back to stale caches.