There is always remaining tasks you have to do in order to finalize a website. Out of this series I'd like to share my simple Meta Tag and Open Graph implementation in XM Cloud using Headless SXA, JSS and NextJs.
Similar to the way it has been implemented with SXA I'm creating 2 base templates within the feature folder that I will inherit to my App Route Template later. This way I keep my template structure modular. In case of tenancy solutions I would be able to reuse the same templates. Also I separate what gets generated by SXA from the custom functionalities.
For Meta Tags I'm adding MetaKeywords field as Singleline Text and MetaDescription as a Multiline Textfield.
For OpenGraph Tags I'm adding:
I want to make sure that every page can have its own Meta Tags and Open Graph Tags. Therefore I'm adding my newly created Base Templates to the App Route Template that was generated for me in Templates/Project/Sugcon during Tenant Creation.
The fields should now appear in each App Route Item.
But of course they also need to be put out. This can be done defining a placeholder in the head section of the layout.tsx file and adding components later to it (e.g. via Meta Partial Design) or simplified, as I have done it, just statically baked into the layout.tsx. This depends on your requirements.
I can read the fields of the page from route.fields by name of the field.
Also I'm wrapping a check around the output to make sure that the field is available and has a value.
{route?.fields?.MetaDescription?.value
? <meta name="description" content={`${route?.fields?.MetaDescription?.value}`}/
: ''}
Make sure that you import the Head from next/head:
import Head from 'next/head';
Created: 7.10.2022
XM Cloud NextJs JSS SXA Headless SXA