Using viewlet to insert js in head (Plone 5)

Is it OK to use a viewlet to insert javascript (google consent mode) in 'head', if yes: can it be done (and cached) with just a py file or does that require a template.pt ?
Maybe something like

class JSHeadViewlet(ViewletBase):
    """ A viewlet which renders the js."""
    
    def render(self):
        js_head =  api.portal.get_registry_record('my.adding.interfaces.ISomeThing.jshead')
        # a check maybe (?)
        if js_head:
            return js_head
        # else return nothing