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

**URL:** <https://meta.discourse.org/t/add-dynamic-content-to-head-amp-any-tips/143815>\
**Category:** Support\
**Created:** [10 maart 2020 om 15:50 UTC](https://meta.discourse.org/t/add-dynamic-content-to-head-amp-any-tips/143815 "2020-03-10T15:50:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bcat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bcat/32/163148_2.png) [@Bcat](https://meta.discourse.org/u/Bcat)\
**Post date:** [10 maart 2020 om 15:50 UTC](https://meta.discourse.org/t/add-dynamic-content-to-head-amp-any-tips/143815/1 "2020-03-10T15:50:22Z")

</div>

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

 ![image](https://global.discourse-cdn.com/meta/original/3X/a/3/a356fc04146bda2c9e9757604b1a829ef198183a.png)

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

```plaintext
<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?](https://meta.discourse.org/t/143773)

---

<div class="post-metadata">

**Author:** ![Bcat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bcat/32/163148_2.png) [@Bcat](https://meta.discourse.org/u/Bcat)\
**Post date:** [13 maart 2020 om 02:18 UTC](https://meta.discourse.org/t/add-dynamic-content-to-head-amp-any-tips/143815/3 "2020-03-13T02:18:28Z")

</div>

I used:

```plaintext
<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](https://github.com/bcat95/discourse-sitemap)

03-14-20

**It succeeds, google is starting to index AMP pages**

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [12 april 2020 om 02:18 UTC](https://meta.discourse.org/t/add-dynamic-content-to-head-amp-any-tips/143815/4 "2020-04-12T02:18:34Z")

</div>

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