BeforeRender gives me headache. Are modules scoped per report or per server?



  • The data I send to jsreport holds report data as well as i18n translations. I'm using i18next to pull out the correct strings from the data.

    In some rare occasions my i18n instance seems to work with data from the previously rendered report. Or more likely, a report that rendered in parallel with the problematic report.

    I'm not sure of the scope of everything. Clearly my i18n variable is local to the if statement, which works, so it has to be scoped to the i18n module, probably as a singleton.

    How can I assure that my i18n instances does not get mixed up in jsreport? This might be more of an 18next question, but first I need to understand how required modules in jsreport are scoped between separate render jobs.

    Here is a simplified version of my beforeRender.

    async function beforeRender (req, res) {
        if(req.data && req.data.i18n) {
            const init = req.data.i18n.init;
            const i18n = require('i18next');
    
            await i18n.init({
                resources: req.data.i18n,
                lng: init.languageCode,
                defaultNS: init.defaultNamespace,
                fallbackNS: init.fallbackNamespace,
                ns: init.namespaces,
                interpolation: { escapeValue: true },
                getAsync: false, //load resources synchronously. I don't know if necessary, but to be sure..
            });
            req.data.i18nInstance = i18n;
        }
    }
    


  • require in nodejs caches the output. So if the i18next returns object instance as it does, you will use the same one in the subsequent calls to require. What you likely want to do is this:

    let i18next = require('i18next').createInstance()
    

    To clarify the questions about scopes. jsreport uses an extra process for scripts evaluation based on the strategy configuration. In case you use in-process you run in the same process so the require calls are always cached. When using dedicated-process you get always a new process for evaluation so the require call is never cached. In case you use the http-process, then several processes are reused, so then require cache is different on every worker.



  • Thanks Jan!
    As usual, great support.

    The same thing happens when I email Locize (i18next) for support. Both Locize and jsReport are really at the top of the game when it comes to support.



  • To clarify. If I had used dedicated-process, I wouldn't run into this problem, would I?
    I used dedicated-process some time ago. That could explain why I haven't seen this problem before.



  • To clarify. If I had used dedicated-process, I wouldn't run into this problem, would I?

    Yes exactly, there is always a new process then, so there is an empty require cache.

    As usual, great support.

    Thank you, glad you like it.


Log in to reply
 

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