Read process.env.NODE_ENV in the footer



  • I need to access process.env.NODE_ENV in the footer to see if my site is in production. I first tried creating a route for /api/footer and outputting {isProduction: process.env.NODE_ENV === 'production' } but the result wasn't accessible in the footer.

    I've tried putting the value in /src/middleware/header.js but that unsurprisingly didn't work.

    As I need it in every page in the site, I don't want to update all of the /api/* routes. Is there some other way to get this value in the footer?

    BTW - The reason is need this is so that I can conditionally add the Google Tag Manager but only in the production environment so my code looks like:

    <!-- IF isProduction -->
    <google tag manager code>
    <!-- ENDIF isProduction -->


  • Plugin & Theme Dev

    @danielflippance you can send the value here https://github.com/NodeBB/NodeBB/blob/master/src/middleware/header.js#L29

    like this...

    app.render('footer', {loggedIn: (req.user ? parseInt(req.user.uid, 10) !== 0 : false), isProduction: true}, next);

    isProduction will be available in footer. separate route not required



  • Perfect, thank you!


Log in to reply
 

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