Skip to content

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

@GrygrFlzr

Description

@GrygrFlzr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: ssrp2-edge-caseBug, but has workaround or limited in scope (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions