Add dynamic content to <head> - AMP - any tips?

I used:

<script type="text/discourse-plugin" version="0.8">
    api.onPageChange((url, title) => {
        var res = url.match(/\/t\/(.*?)\/(\w+)/);
        if (res && res[2] > 0) {
            var amp = 'domain/amp/t/'+res[2];
            if (document.getElementById("amphtml")) document.getElementById("amphtml").href = amp;
            else{
                var link=document.createElement('link');
                link.id='amphtml';
                link.rel='amphtml';
                link.href=amp;
                document.getElementsByTagName('head')[0].appendChild(link);
            }
        }
    });
</script>

But when view source does not appear, it only shows when javascript executes.
So how to render from the beginning


03-13-20

I try to change by adding <xhtml:link rel="amphtml" href="" /> to Sitemap
Demo: GitHub - bcat95/discourse-sitemap: Generate XML sitemap for your Discourse forum. · GitHub

03-14-20

It succeeds, google is starting to index AMP pages