yuanchuan / node-watch

A wrapper and enhancements for fs.watch
https://npm.im/node-watch
MIT License
341 stars 44 forks source link

images update problem #127

Closed tumblingG closed 1 year ago

tumblingG commented 1 year ago

image image

I monitor my engineering projects. When I opened the images folder of my project from windows D, the update automatically triggered, but I didn't actually change the images. And the js files sometimes update automatically even though I haven't changed anything, why?

tumblingG commented 1 year ago

image

When I use the local proxy to access the local file, the js file will also trigger the update. Is the browser changing the file?

yuanchuan commented 1 year ago

There is nothing to do with node-watch. Replacing watch with fs.watch would be the same.

When you open a directory your OS system may update some meta data of that directory. Since you're watching the whole directory recursively that callback will be triggered anyway. And sometimes the editors will save files silently even if the save button isn't being touched.

yuanchuan commented 1 year ago

One way to avoid the blindly calling is to detect the file types which you're going to handle. For example, inside the callback function:

watch('somehere', {recursive: true}, (evt, pathname) => {
  if (/\.png$/.test(pathname)) {
     // do something
  }
});

You can also increase the delay so that the setTimeout thing in your code would be unnecessary.

watch('somewhere', { recursive: true, delay: 1000 }, (evt, pathname) => {

});