Watch a live product demo 👀 See how Sanity powers richer commerce experiences

NextJS - How to Retrieve Values for Meta Tags

14 replies
Last updated: Sep 8, 2022
Hello! I'm setting up a NextJS app with Sanity, and am wondering about how to retrieve values for the
<meta>
tags. It looks like Next only allows
getStaticProps
or
getServerSideProps
to be run on individual pages, not the
_app
or
_document
pages. Will I need to run the same query and apply meta tags on every page? Seems like there must be a single place to grab this data and include it on every page 🤔
Sep 6, 2022, 5:22 PM
if your meta tags are different for individual pages, you’ll want to use something like Next SEO — this will resolve merge differences in your
<head>
Sep 6, 2022, 5:24 PM
if you just need one set of meta tags, doing this in the _app or _document should be fine 🙂
Sep 6, 2022, 5:24 PM
But can I query Sanity from _app or _document? It seems like I can only do that client-side (no getStaticProps or getServerSideProps), and I was hoping to include these meta tags in the server-rendered page to ensure they're visible to search engines
Sep 6, 2022, 5:26 PM
Will I need to run the same query and apply meta tags on every page?
Yes, if you want to benefit from static builds.
Sep 6, 2022, 5:32 PM
Thanks, fortunately I just have a few pages on this site so it shouldn't be too bad. Just making sure I'm not missing anything obvious for doing it in a single location
Sep 6, 2022, 5:34 PM
you can render server side from
_app
— using
getInitialProps
rather than
getStaticProps
but that’s how I render my homepages for all the websites I build, fetching Sanity data.

https://nextjs.org/docs/api-reference/data-fetching/get-initial-props

For the initial page load,
getInitialProps
will run on the server only.
getInitialProps
will then run on the client when navigating to a different route via the
next/link
component or by using
next/router
. However, if
getInitialProps
is used in a custom
_app.js
, and the page being navigated to implements
getServerSideProps
, then
getInitialProps
will run on the server.
Sep 6, 2022, 7:29 PM
It disables static optimization though. 😔
Sep 6, 2022, 7:32 PM
but only for pages without
getStaticProps
right?
Sep 6, 2022, 7:34 PM
(as far as I understand it anyway)
Sep 6, 2022, 7:35 PM
That's not how I understand it.
Sep 6, 2022, 7:36 PM
the documentation says
Adding a custom
getInitialProps
in your
App
will disable Automatic Static Optimization in pages without Static Generation .
but they don’t go into details about it. I would love to know more from a nextjs developer
Sep 6, 2022, 7:43 PM
I am pretty sure if you enable
getInitialProps
on the
_app.js
level, you lose static optimization entirely. That makes only sense since it‘s a SSR function. If it’s on, then all pages need to use it, so it’s SSR only. At least that’s how I get it.
Sep 7, 2022, 6:40 AM
Hello wonderful people 👀
I was wondering that too at one point and I think giving up automatic static optimisation for meta in
_app
is nor worth it.What I instead do is:
create a
Layout
component, which is the meta wrapper on every site and add a specific
Head
and
title
within that Layout (sometimes I want to change those through some extra 💅 and this is why I have it outside of Layout)

//index.jsx where I query for siteSettings

export default function Index({ data, preview, setModal, modal }) {

const { image, title, description, siteSettings } = data?.page;

  return(
     <Layout preview={preview} description={description} image={image} siteSettings={siteSettings}>
       <Head>
         <title>MY AWESOME SITE - {title}</title>
       </Head>
     {/* ALL MY COMPONENTS */}
   </Layout>
  )
}
...

// in my query 
...,
  'siteSettings': *[_type == 'siteSettings'][0]
...,

siteSettings
are the overall settings for the whole site, which means, that there is some routing included, which kind of menu items the site should have etc. but also the default
meta
information.
What most people forget is that you can pass props down to components but you can also “bubble them up”, which is strangely easy in React/NextJS:


// _App.js

function MyApp({ Component, pageProps }) { ...

return (
    <>
      <Head>
        <meta name='viewport' content='minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=yes, user-scalable=yes, viewport-fit=cover' />
      </Head>

      {/*  <Header /> */}
      <MenuBar siteSettings={pageProps.data.page.siteSettings} />


      <Modals modal={modal} setModal={setModal} siteSettings={pageProps.data.page.siteSettings} />

      <Component {...pageProps} modal={modal} setModal={setModal} />
    </>
  )
}
As you can see you can access your queried data, from the
pageProps
, if you set it up that way 🙂
Sep 8, 2022, 2:58 PM
BUT: there are new things coming out in Next JS soon :party_parrot:
https://nextjs.org/blog/layouts-rfc 🎉
Sep 8, 2022, 3:00 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing