Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
7 tasks done
brandonroberts opened this issue Oct 24, 2022 · 8 comments · Fixed by #14117
Closed
7 tasks done
Labels
has workaround p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Comments

@brandonroberts
Copy link

brandonroberts commented Oct 24, 2022

Describe the bug

I'm using the import.meta.glob here https:/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:/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

@fc
Copy link
Contributor

fc commented Oct 24, 2022

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 - #10561

@itsdouges
Copy link

itsdouges commented Jan 27, 2023

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);
      },
    },
  ];
}

@brandonroberts
Copy link
Author

@itsdouges
Copy link

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.

@brandonroberts
Copy link
Author

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

@MirazMac
Copy link

MirazMac commented May 8, 2023

Any updates on this?

@madigan
Copy link

madigan commented May 21, 2023

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

@sapphi-red sapphi-red added has workaround p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) and removed pending triage labels Aug 16, 2023
@brandonroberts
Copy link
Author

Nice!

@github-actions github-actions bot locked and limited conversation to collaborators Aug 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants