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

AI-generated summary

Bcat is seeking help to dynamically add content to the <head> section of AMP pages. They tried using a script but it was rendered outside of the <head> and <body> sections, and the content was only visible after page load, not in the view-source.

Bcat initially used a Discourse plugin script to achieve this, but the changes were only visible after JavaScript execution. To resolve this, Bcat added <xhtml:link rel="amphtml" href="" /> to the Sitemap, which successfully allowed Google to start indexing AMP pages (Bcat).

Can anybody show me how to insert a piece of code into the header with dynamic content generated by POST ID!

I tried it but it was outside of head and body. Moreover, only render after the page is loaded, view-source is not displayed.

<script type="text/x-handlebars" data-template-name="/connectors/PLUGIN-OUTLET-NAME/UNIQUE-NAME">
</script>

I think a plugin is needed to do this!
It’s like \app\helpers\application_helper.rb

p/s: old posts Add rel="amphtml" to head - 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