Meta tags
Meta tags
Section titled “Meta tags”This guide covers how to set meta tags from an article content type in Drupal to a front end app for SEO purposes.
Drupal setup
Section titled “Drupal setup”- Meta tags are set in the
articlecontent type in Drupal. graphql_compose_metatagssubmodule from Graphql compose is installed, and enabled.
Frontend setup
Section titled “Frontend setup”Install dependencies
Section titled “Install dependencies”First of all, to process the Drupal meta tags into the React Router format you can install the drupal-decoupled package.
npm install drupal-decoupledyarn add drupal-decoupledpnpm install drupal-decoupledFirst of all, to process the Drupal meta tags into the Next.js format you can install the drupal-decoupled package.
npm install drupal-decoupledyarn add drupal-decoupledpnpm install drupal-decoupledUpdate the content query
Section titled “Update the content query”On the front end, update the content query to also fetch the meta tags.
If you came from the React Router Guide, you should have a splat route with a route query. Update this query to fetch the meta tags for the page.
query route($path: String!) { route(path: $path) { ... on RouteInternal { entity { ... on NodeArticle { ... body { value } // highlight-addition-start metatag { __typename ... on MetaTagLink { attributes { rel href } } ... on MetaTagValue { attributes { name content } } ... on MetaTagProperty { attributes { property content } } } // highlight-addition-end } } } }}If you came from the Next.js Guide, you should be fetching data using NodeArticleFragment. Update this fragment to fetch the meta tags for the node.
export const NodeArticleFragment = graphql( ` fragment NodeArticleFragment on NodeArticle { __typename id title ... // highlight-addition-start metatag { __typename ... on MetaTagLink { attributes { rel href } } ... on MetaTagValue { attributes { name content } } ... on MetaTagProperty { attributes { property content } } } // highlight-addition-end body { processed } } `, [MediaImageFragment, UserFragment],);Expose the meta tags
Section titled “Expose the meta tags”Before you can set the meta tags, update the imports in the file that is loading the content from Drupal, to includes the meta tags parser function and the needed type definitions.
import { //highlight-addition metaTags,} from "drupal-decoupled/react-router";import type { Route } from "./+types/$";import { calculateMetaTags } from "~/utils/metatags";import { type FragmentOf, readFragment } from "gql.tada";import type { Metadata } from "next";//highlight-addition-startimport { metaTags } from "drupal-decoupled/next";import { calculateMetaTags } from "@/utils/metatags";//highlight-addition-endand finally you can set the meta tags in the page.
To archive it, export a meta function to handle the meta tags.
export function meta({ loaderData }: Route.MetaArgs) { if (!loaderData) { return []; } const { type, entity } = loaderData;
return metaTags({ tags: calculateMetaTags(type, entity), });}To archive it, export a generateMetadata function to handle the meta tags.
export async function generateMetadata({ params,}: PageProps): Promise<Metadata> { const { type, entity } = await getDrupalData({ params: await params });
return metaTags({ tags: calculateMetaTags(type, entity), });}Now, the meta tags should be set in the page, you can check it in the browser’s source code.