Using singletons for the homepage in Next.js with Sanity
37 replies
Last updated: Jul 28, 2021
N
Guys, using slugs works but how do you do that for the homepage? I don’t want to define a slug for the homepage since it’s the root page.
I am using sections to create all the pages with renderSections in Next js.
I am using sections to create all the pages with renderSections in Next js.
Jul 28, 2021, 5:28 PM
M
you can create a global settings doc with a Homepage reference and then use the structure builder to render it as a singleton
Jul 28, 2021, 5:36 PM
M
this is a powerful pattern that can be used in a bunch of creative ways
Jul 28, 2021, 5:37 PM
M
user G
is using it to swap out entire site themes like navigations, footers, etc Jul 28, 2021, 5:37 PM
K
in the past i would default the site root to a slug of
homeor
homepagebut as Mike said, I am not following a wordpress settings style pattern, where you set the homepage in a global config, so any page in the system can be swapped as the hompage without having to worry about the
slug
Jul 28, 2021, 5:40 PM
K
https://www.sanity.io/guides/crafting-a-theme-engine i wrote a guide about the pattern
Jul 28, 2021, 5:41 PM
N
How do you show the singleton on the front-end is there a example or anything? Because I love articles but this is way too long.
Jul 28, 2021, 6:19 PM
K
front-end of what?
Jul 28, 2021, 6:19 PM
K
sanity or the main site?
Jul 28, 2021, 6:19 PM
N
Example of like how to use it with Next JS f
Jul 28, 2021, 6:20 PM
K
you would just make an index.jsx file that overrides the [slug] file and select the specific singleton in the query
Jul 28, 2021, 6:20 PM
N
So I can keep the [slug].js inside the “pages” but the index should have the singleton only right?
Jul 28, 2021, 6:21 PM
N
// Next import { useRouter } from "next/router"; // Queries import { routesQuery } from "@lib/queries"; // Sanity import { getClient } from "@lib/sanity.server"; import { PortableText, usePreviewSubscription } from "@lib/sanity"; // Helpers import { formatDate } from "@lib/helpers"; // Components import FeaturedImage from "@components/FeaturedImage"; import LandingPage from "@components/LandingPage"; import NextSeo from "@components/NextSeo"; const Page = ({ pageData, preview, slug }) => { const router = useRouter(); const { data: { page = {} } = {} } = usePreviewSubscription(routesQuery, { params: { slug }, initialData: pageData, enabled: preview || router.query.preview !== null, }); const { body, featuredImage, publishedAt, title, author } = page; return ( <> <NextSeo page={page} slug={slug} /> <LandingPage page={page} /> {featuredImage && <FeaturedImage featuredImage={featuredImage} />} {author && <p>Written by: {author.name}</p>} {publishedAt && <p>Publish date: {formatDate(publishedAt)}</p>} {title && <h1>{title}</h1>} {body && <PortableText blocks={body} />} </> ); }; export const getStaticProps = async ({ params = {}, preview = false }) => { const { slug } = params; const { page: pageData } = await getClient(preview).fetch(routesQuery, { slug, }); return { props: { preview, pageData, slug }, }; }; export const getStaticPaths = async () => { const routes = await getClient() .fetch(`*[_type == "route" && defined(slug.current)]{ "params": {"slug": slug.current} }`); return { paths: routes || null, fallback: false, }; }; export default Page;
Jul 28, 2021, 6:21 PM
N
This my [slug].js btw for all other pages
Jul 28, 2021, 6:21 PM
K
you’d use essentially the same code just not fetcha ll pages, only the 1 you want
Jul 28, 2021, 6:22 PM
K
so you basically don’t need the getstaticpaths, and just need getstaticProps with a query to your home singleton
Jul 28, 2021, 6:22 PM
K
because you don’t need all the other
pathsin this case
Jul 28, 2021, 6:22 PM
N
I am using “routes” but instead of that I should use the
?
queries.homepage
Jul 28, 2021, 6:22 PM
N
rIght
Jul 28, 2021, 6:22 PM
N
Is there an example of index.js that I can check?
Jul 28, 2021, 6:23 PM
K
i don’t know what you mean by example, you have everything you need, just remove the getStaticPaths, and replace the routesQuery with a homeQuery
Jul 28, 2021, 6:23 PM
N
Ahhhhhh
Jul 28, 2021, 6:24 PM
K
where homeQuery is some singleton in your queries file
Jul 28, 2021, 6:24 PM
N
So everything else is oke just remove getStaticPaths and repalce routesQuery with Homequery
Jul 28, 2021, 6:24 PM
N
Got it
Jul 28, 2021, 6:24 PM
K
*[_type == 'page' && slug.current == 'home'][0]
Jul 28, 2021, 6:24 PM
K
exactly
Jul 28, 2021, 6:24 PM
N
Ahhh that’s why you create the singeleton 😄
Jul 28, 2021, 6:24 PM
N
for the homepage only
Jul 28, 2021, 6:24 PM
N
Great!
Jul 28, 2021, 6:24 PM
N
Maybe a stupid question
Jul 28, 2021, 6:25 PM
N
but Do you suggest like using “sections” for every single page?
Jul 28, 2021, 6:25 PM
K
i find it easier at the end of the day not to limit the client
Jul 28, 2021, 6:25 PM
N
Nice
Jul 28, 2021, 6:25 PM
K
because they always ask to add
xyzto other places
Jul 28, 2021, 6:25 PM
N
So best practice is to let them select
Jul 28, 2021, 6:25 PM
N
Correct
Jul 28, 2021, 6:25 PM
N
Thanks boss
Jul 28, 2021, 6:25 PM
Sanity– build remarkable experiences at scale
Sanity is a 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.