Giter Site home page Giter Site logo

Comments (6)

0cv avatar 0cv commented on August 20, 2024 1

Got it thank you @glenjamin. My mistake was to register the middleware straight (I did the require, then app.use) instead of wrapping the require into a function inside app.use like you did). I will close the issue, may that help someone else! Thanks again.

from ultimate-hot-reloading-example.

glenjamin avatar glenjamin commented on August 20, 2024

The other part that makes it work is doing a require inside the main HTTP handler. Can you post a sample?

from ultimate-hot-reloading-example.

0cv avatar 0cv commented on August 20, 2024

I have added that in the server part:

const watcher = chokidar.watch(`${__dirname}/..`);

watcher.on('ready', function() {
  watcher.on('all', function() {
    console.log('Clearing /server/ module cache from server');
    Object.keys(require.cache).forEach(function(id) {
      if (/[\/\\]server[\/\\]/.test(id) && !(/[\/\\]bin[\/\\]/.test(id)) && !(/[\/\\]models[\/\\]/.test(id))) {
        console.log('delete', id)
        delete require.cache[id]
        require(id)
      }
    });
  });
});

=> I exclude the bin folder where the server is located, also the models, because mongoose complains when the model is recreated twice, fair enough.

I have also added explicitly a require(id) after deleting from the cache, otherwise the next time it's run, somehow, the modules are not required again (could be already one problem why I need to do that...)

I should mention I'm using babel with babel-register and ES6 import/export. I've seen, they are somehow caching modules too http://babeljs.io/docs/usage/require/, but I've disabled the cache like mentioned (I have also tried to delete the json file created to force a refresh). Both options didn't change anything.

Below is the structure of the server folder where we can see the bin folder and models folder (both ignored):
image

For my tests, I changed the value in the console.log and then refreshed the browser to trigger the route. The value shown in the console is (of course) correct the first time the app is started, but not for any subsequent edit to the console.log.

from ultimate-hot-reloading-example.

glenjamin avatar glenjamin commented on August 20, 2024

I think doing the require(id) inside that loop isn't going to work, as you'll be partially repopulating the cache as you're clearing it.

You need to clear it out entirely, and let the next incoming route repopulate it.

from ultimate-hot-reloading-example.

0cv avatar 0cv commented on August 20, 2024

You're right. I could narrow down the issue to app.use. Something modified outside of Koa feels like to work, but not in Koa. In your version, the middleware is somehow overwritten, but not in my app where even so I do a new app.use with the fresh route, it still keep the previous version. Will try to see/figure out how to deregister/update a middleware...

from ultimate-hot-reloading-example.

glenjamin avatar glenjamin commented on August 20, 2024

Here: https://github.com/glenjamin/ultimate-hot-reloading-example/blob/master/server.js#L28-L31

The middleware here delegates to the real middleware which is "late bound", it always requires the real middleware and passes all the arguments through.

You'll need something like that.

from ultimate-hot-reloading-example.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.