<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Add dynamic meta tag depend on ?id with React CMS in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/Add-dynamic-meta-tag-depend-on-id-with-React-CMS/m-p/1035814#M40538</link>
    <description>&lt;P&gt;Hi, my site has a list of posts with url /post?id=&amp;lt;id&amp;gt;. The posts are stored on a backend outside hubspot. I need to add meta tags with relevant information about the post based on the ID. I am using &lt;A href="http://cms-react" target="_blank" rel="noopener"&gt;https://github.hubspot.com/cms-react/&lt;/A&gt; and everything works fine except one thing. When I use getServerSideProps to get the post data and useInlineHeadAsset() to add a meta tag, the meta tag is added in the form of&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;script nonce=‘hs-random-nonce’&amp;gt;
document.head.innerHTML += `&amp;lt;meta property=‘og:image’ content=‘url’/&amp;gt;;`;
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code looks like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;export const getServerSideProps = withUrlAndQuery(async (props, url) =&amp;gt; {
  const id = url.url.searchParams.get('id');
  const post = await fetch(...);

  return {
    serverSideProps: {
      img: post.img
    },
    caching: {
      cacheControl: {
        maxAge: 60
      }
    }
  }
});
 
export const  Component = ({ serverSideProps }) =&amp;gt; {
  const img = serverSideProps.img;
  
  useInlineHeadAsset(() =&amp;gt; {
    return &amp;lt;&amp;gt;
      &amp;lt;meta property="og:image" content={img} /&amp;gt;
    &amp;lt;/&amp;gt;
  });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was testing and noticed that the meta tag is added to the head if I don't use getServerSideProps, but in that case I can't get the post data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any solution to this problem or is there any other way to dynamically add meta tag?&lt;/P&gt;</description>
    <pubDate>Tue, 03 Sep 2024 14:19:36 GMT</pubDate>
    <dc:creator>VShvets</dc:creator>
    <dc:date>2024-09-03T14:19:36Z</dc:date>
    <item>
      <title>Add dynamic meta tag depend on ?id with React CMS</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Add-dynamic-meta-tag-depend-on-id-with-React-CMS/m-p/1035814#M40538</link>
      <description>&lt;P&gt;Hi, my site has a list of posts with url /post?id=&amp;lt;id&amp;gt;. The posts are stored on a backend outside hubspot. I need to add meta tags with relevant information about the post based on the ID. I am using &lt;A href="http://cms-react" target="_blank" rel="noopener"&gt;https://github.hubspot.com/cms-react/&lt;/A&gt; and everything works fine except one thing. When I use getServerSideProps to get the post data and useInlineHeadAsset() to add a meta tag, the meta tag is added in the form of&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;script nonce=‘hs-random-nonce’&amp;gt;
document.head.innerHTML += `&amp;lt;meta property=‘og:image’ content=‘url’/&amp;gt;;`;
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code looks like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;export const getServerSideProps = withUrlAndQuery(async (props, url) =&amp;gt; {
  const id = url.url.searchParams.get('id');
  const post = await fetch(...);

  return {
    serverSideProps: {
      img: post.img
    },
    caching: {
      cacheControl: {
        maxAge: 60
      }
    }
  }
});
 
export const  Component = ({ serverSideProps }) =&amp;gt; {
  const img = serverSideProps.img;
  
  useInlineHeadAsset(() =&amp;gt; {
    return &amp;lt;&amp;gt;
      &amp;lt;meta property="og:image" content={img} /&amp;gt;
    &amp;lt;/&amp;gt;
  });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was testing and noticed that the meta tag is added to the head if I don't use getServerSideProps, but in that case I can't get the post data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any solution to this problem or is there any other way to dynamically add meta tag?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 14:19:36 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Add-dynamic-meta-tag-depend-on-id-with-React-CMS/m-p/1035814#M40538</guid>
      <dc:creator>VShvets</dc:creator>
      <dc:date>2024-09-03T14:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Add dynamic meta tag depend on ?id with React CMS</title>
      <link>https://community.hubspot.com/t5/CMS-Development/Add-dynamic-meta-tag-depend-on-id-with-React-CMS/m-p/1036118#M40547</link>
      <description>&lt;P&gt;Hi &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/803599"&gt;@VShvets&lt;/a&gt;&lt;/SPAN&gt;! Welcome to the Community-- happy to have you here &lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I understand you are looking for a way to dynamically set a meta tag on your blog pages based on the post ID. Thank you for including your code. I'd like to invite some of our experts to see if they have come across this type of request! &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/73772"&gt;@Kevin-C&lt;/a&gt;&lt;/SPAN&gt;, &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/92197"&gt;@albertsg&lt;/a&gt;&lt;/SPAN&gt;, &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/78983"&gt;@Indra&lt;/a&gt; do any of you have insight into retrieving the post data and dynamically adding a meta tag? Thanks in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&amp;nbsp;&lt;BR /&gt;Kennedy&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 23:56:09 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/Add-dynamic-meta-tag-depend-on-id-with-React-CMS/m-p/1036118#M40547</guid>
      <dc:creator>kennedyp</dc:creator>
      <dc:date>2024-09-03T23:56:09Z</dc:date>
    </item>
  </channel>
</rss>

