How can I persist data and reload it between each render ?



  • I need to store a token in memory that can be reused by each render. I tried something like this with a custom module but it's being reset on every render.

    const state = require('src/state.js');
    
    async function beforeRender(req, res) {
      // should be not null on the second render
      if(!state.getToken()){
           const token = await login();
           state.setToken(token)
      }
      // use token...
    } 
    


  • By default, jsreport assures the required cache is cleared after every request, to ensure one request is isolated and can't break the others. In other words, you get always a new state when calling require. This can be disabled using config.

    "sandbox": {
        "isolateModules": false
      }
    

    With this, you get a cached module when doing require. However the require cache is unique for every worker thread, thus depends also on config workers.numberOfWorkers.

    The other option is to write the token to a file, database...



  • Thanks that worked.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.