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

SSR import.meta.glob does not update on file/directory add/remove #2734

Closed
GrygrFlzr opened this issue Mar 27, 2021 · 0 comments · Fixed by #3005
Closed

SSR import.meta.glob does not update on file/directory add/remove #2734

GrygrFlzr opened this issue Mar 27, 2021 · 0 comments · Fixed by #3005
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@GrygrFlzr
Copy link
Member

GrygrFlzr commented Mar 27, 2021

Describe the bug

Suppose you have a main.js script using import.meta.glob('/src/pages/*.js').

├ src
│ ├ pages
│ │ ├ a.js
│ │ └ b.js
│ └ main.js
...

main.js is an SSR-evaluated module. When the dev server is running and you add a c.js file, main.js is not updated with the new import. Similarly, deleting a.js does not update the imports. Only updating index.js directly seems to re-transform the file.

It is notable that import.meta.glob is already able to respond to file/directory changes fine when used outside an SSR context, which is why I believe this is a bug and not a feature request. In addition, there is an explicit test for this exact scenario of adding/removing files against import.meta.glob for non-SSR:

if (!isBuild) {
test('hmr for adding/removing files', async () => {
addFile('dir/a.js', '')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(
{
'/dir/a.js': {},
...allResult
},
null,
2
)
)

Which indicates this is indeed supposed to be an existing feature.

A workaround for this is to re-save or touch your SSR file after every update to the directory.

Reproduction

https://github.com/GrygrFlzr/vite-ssr-glob

git clone https://github.com/GrygrFlzr/vite-ssr-glob.git
cd vite-ssr-glob
npm install
npm run dev
  1. Load http://localhost:3000, which should currently show:
    {
      "/src/data/a.json": {
        "name": "a"
      },
      "/src/data/b.json": {
        "name": "bc"
      }
    }
  2. Update src/data/b.json to the contents {}. This will trigger HMR.
    Demonstrates correct update on file change.
  3. Add a new file src/data/c.json with the contents {}.
  4. Refresh http://localhost:3000 and observe lack of update.
    Demonstrates lack of update on file addition.
  5. Remove src/data/a.json.
  6. Refresh http://localhost:3000 and observe lack of update.
    Demonstrates lack of update on file removal.
  7. Re-save src/app.js without changing its contents. This will invalidate the file and trigger HMR, correctly showing the current state of the src/data directory.

System Info

  System:
    OS: Windows 10 10.0.19042
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 11.52 GB / 31.95 GB
  Binaries:
    Node: 14.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 7.6.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 89.0.4389.90
    Edge: Spartan (44.19041.423.0), Chromium (89.0.774.54)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    vite: ^2.1.3 => 2.1.3

Used package manager: npm

@GrygrFlzr GrygrFlzr added pending triage p2-edge-case Bug, but has workaround or limited in scope (priority) bug and removed pending triage labels Mar 27, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants