Know if Studio rendered or not?



  • Sorry if this has already been asked before, but is there a way to know if a template is being rendered via studio or not? The scenario is that I need to include a .js in my html rendering if i am in studio for testing/debugging, but if that is included in my site that renders it inline, it causes issues because it's included twice. I would like to only render the script tag in the HTML if it's being rendered from with Studio and not output that tag if it's being rendered via an API call. I am looking for an environment variable or something that i can test. I currently have a work around, but I am looking for something more official and less "hacky"

    Thanks


  • administrators

    hi @zewar96

    you can use a script like this

    async function beforeRender (req, res) {
        const isPreview = req.options.preview === true
        console.log('IS PREVIEW:', isPreview)
        req.data.IS_PREVIEW = isPreview
    }
    

    then you can then check in your template for the IS_PREVIEW variable to create conditions to include/remove content



  • perfect...thank you!


Log in to reply
 

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