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

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 https://meta.discourse.org/t/143773

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.

03-14-20

It succeeds, google is starting to index AMP pages

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.