Eptagone / Vite.AspNetCore

Small library to integrate Vite into ASP.NET projects
MIT License
264 stars 35 forks source link

ViteManifest does not reload if the manifest file changes on a running site #87

Closed someguy20336 closed 10 months ago

someguy20336 commented 10 months ago

Describe the bug If you have a running ASP.NET Core site and you compile and deploy a new build of the client files only (no new binaries so the site doesn't need to restart), ViteManifest will still have the old manifest loaded in memory and attempt to serve files that may no longer exist (because the hash changed, for example). This causes any affected page to no longer render due to missing scripts.

To Reproduce Steps to reproduce the behavior:

  1. Start your site, but without the dev server (vite build)
  2. Navigate to your page, see that it works
  3. Change your client file in any way and rerun vite build. Presumably, you have different hashes of the file now
  4. Refresh your page, see that it tries to load the old JavaScript files.

Expected behavior Vite.AspNetCore should watch the manifest file for changes. If the file is updated, it should invalidate the cached data and reload.

Device (please complete the following information):

Additional context Note that, while I didn't actually try this out yet, it is possible that a workaround could be to implement your own IViteManifest wrapper around ViteManifest and add this behavior. Still, I would imagine it should ultimately be built into this library since I don't think there is a reason you wouldn't want this behavior.

The FileProvider may be of interest here.

Might be interested in taking the PR if I have a chance here and you agree with the proposed change. But feel free to take it if you feel like you can knock it out quickly.

Eptagone commented 10 months ago

Hi, that's because the IViteManifest service is registered as Singleton by default, so the manifest.json file is loaded once on first request. Subsequent changes are not read by the service. You can actually change this behavior by using another lifetime like Scoped or Transient. The AddViteServices method defines a parameter to achieve this.

Eptagone commented 10 months ago

If there's a better way to manage the manifest using ASP.NET APIs, feel free to open a new PR for that.

someguy20336 commented 10 months ago

Good point - I didn't think about changing the scope of the service, though that of course comes at the cost of reading it every time.

Let me think about how maybe the standard configuration API might work with this file. I'll open a PR if I think of something cool.