vite icon indicating copy to clipboard operation
vite copied to clipboard

Vite must be restarted along with browser refresh to pick up new files using import.meta.glob

Open brandonroberts opened this issue 3 years ago • 2 comments

Describe the bug

I'm using the import.meta.glob here https://github.com/analogjs/analog/blob/main/packages/router to generate routes based on directory of files.

Adding new files is not invalidating the necessary modules, so refreshing the page does not produce a new set of files.

Reproduction

https://github.com/brandonroberts/analog-router-demo

Steps to reproduce

  1. Clone repo
  2. Run yarn to install dependencies
  3. Run yarn dev
  4. Copy the src/app/routes/about.ts to src/app/routes/test.ts and change the template
  5. Try to visit http://127.0.0.1:5173/test
  6. Note the Page Not Found displayed
  7. Stop/Restart the development server
  8. Hard refresh the page in the browser
  9. Try to visit http://127.0.0.1:5173/test
  10. Note the page is displayed

The routes can also be updated by changing an existing file in the src/app/routes folder.

System Info

OSX

Used Package Manager

yarn

Logs

No response

Validations

brandonroberts avatar Oct 24 '22 12:10 brandonroberts

out of curiosity, if you disable the cache in your browser does that also resolve it? If so, I ran into a similar cache issue recently - https://github.com/vitejs/vite/issues/10561

fc avatar Oct 24 '22 22:10 fc

Heya! Do we have any idea where the fix could be in Vite? @brandonroberts can you go into your workaround?

Edit: I've worked around this with the following plugin, which works with HMR. I run my own watcher.

import { watch } from "chokidar";
import type { Plugin } from "vite";

const tempFolderName = join("node_modules", ".triplex");
const tempDir = join(process.cwd(), tempFolderName);
const triplexGlobModule = "triplex-scene-glob";

function forceGlobsHmr(): Plugin[] {
  return [
    {
      name: "triplex/watch-globs",
      configureServer(server) {
        function reloadGlobModule() {
          server.moduleGraph.fileToModulesMap.forEach((mods) => {
            mods.forEach((mod) => {
              if (mod.id?.includes(triplexGlobModule)) {
                server.reloadModule(mod);
              }
            });
          });
        }

        const watcher = watch(tempDir);
        watcher.on("add", reloadGlobModule);
      },
    },
  ];
}

itsdouges avatar Jan 27 '23 21:01 itsdouges

I did something similar also

https://github.com/analogjs/analog/blob/main/packages/platform/src/lib/router-plugin.ts#L30

brandonroberts avatar Feb 17 '23 22:02 brandonroberts

Update on my end: I was able to delete all this code. Instead I used a virtual module that would have its glob be declared at build time, and then it all works as expected when modules are added and removed.

itsdouges avatar Apr 22 '23 01:04 itsdouges

@itsdouges do you have some sample code to share? I'd be interested in removing my workaround also

brandonroberts avatar Apr 23 '23 19:04 brandonroberts

Any updates on this?

MirazMac avatar May 08 '23 13:05 MirazMac

@itsdouges Could you share how you did that? I'm running into this issue as well.

madigan avatar May 21 '23 02:05 madigan

Nice!

brandonroberts avatar Aug 16 '23 14:08 brandonroberts