Skip to content

Commit 4c3d5a3

Browse files
committed
refactor the sitemap.js a smidge
1 parent ad4f0c7 commit 4c3d5a3

File tree

3 files changed

+78
-69
lines changed

3 files changed

+78
-69
lines changed

nextjs-app/app/sitemap.ts

+53-35
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
import { MetadataRoute } from "next";
22
import { sanityFetch } from "@/sanity/lib/live";
3-
import { allPagesQuery, allPostsQuery } from "@/sanity/lib/queries";
4-
import { headers } from 'next/headers';
3+
import { sitemapData } from "@/sanity/lib/queries";
4+
import { headers } from "next/headers";
5+
6+
/**
7+
* This file creates a sitemap (sitemap.xml) for the application. Learn more about sitemaps in Next.js here: https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap
8+
* Be sure to update the `changeFrequency` and `priority` values to match your application's content.
9+
*/
510

6-
// TODO change Frequency accordingly
711
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
8-
const allPosts = await sanityFetch({query: allPostsQuery});
9-
const allPages = await sanityFetch({query: allPagesQuery});
10-
const headersList = await headers();
11-
const sitemap: MetadataRoute.Sitemap = [];
12-
const domain: String = headersList.get("host") as string;
13-
sitemap.push({
14-
url: domain as string,
15-
lastModified: new Date(),
16-
priority: 1,
17-
changeFrequency: "monthly"
18-
})
12+
const allPostsAndPages = await sanityFetch({
13+
query: sitemapData,
14+
});
15+
const headersList = await headers();
16+
const sitemap: MetadataRoute.Sitemap = [];
17+
const domain: String = headersList.get("host") as string;
18+
sitemap.push({
19+
url: domain as string,
20+
lastModified: new Date(),
21+
priority: 1,
22+
changeFrequency: "monthly",
23+
});
1924

20-
if (allPosts != null && allPosts.data.length != 0) {
21-
for (const p of allPosts.data) {
22-
sitemap.push({
23-
lastModified: new Date(),
24-
priority: .5,
25-
changeFrequency: "never",
26-
url: `${domain}/posts/${p.slug}`
27-
});
28-
}
29-
}
25+
if (allPostsAndPages != null && allPostsAndPages.data.length != 0) {
26+
let priority: number;
27+
let changeFrequency:
28+
| "monthly"
29+
| "always"
30+
| "hourly"
31+
| "daily"
32+
| "weekly"
33+
| "yearly"
34+
| "never"
35+
| undefined;
36+
let url: string;
3037

31-
if (allPosts != null && allPages.data.length != 0) {
32-
for (const p of allPages.data) {
33-
sitemap.push({
34-
lastModified: new Date(),
35-
priority: .8,
36-
changeFrequency: "monthly",
37-
url: `${domain}/${p.slug}`
38-
});
39-
}
38+
for (const p of allPostsAndPages.data) {
39+
switch (p._type) {
40+
case "page":
41+
priority = 0.8;
42+
changeFrequency = "monthly";
43+
url = `${domain}/${p.slug}`;
44+
break;
45+
case "post":
46+
priority = 0.5;
47+
changeFrequency = "never";
48+
url = `${domain}/posts/${p.slug}`;
49+
break;
50+
}
51+
sitemap.push({
52+
lastModified: p._updatedAt || new Date(),
53+
priority,
54+
changeFrequency,
55+
url,
56+
});
4057
}
58+
}
4159

42-
return sitemap
43-
}
60+
return sitemap;
61+
}

nextjs-app/sanity.types.ts

+21-20
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ export type SettingsQueryResult = {
505505
};
506506
} | null;
507507
// Variable: getPageQuery
508-
// Query: *[_type == 'page' && slug.current == $slug][0]{ _id, _type, name, slug, heading, subheading, "pageBuilder": pageBuilder[]{ ..., _type == "callToAction" => { ..., link { ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } }, } }, }
508+
// Query: *[_type == 'page' && slug.current == $slug][0]{ _id, _type, name, slug, heading, subheading, "pageBuilder": pageBuilder[]{ ..., _type == "callToAction" => { ..., link { ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } }, } }, }
509509
export type GetPageQueryResult = {
510510
_id: string;
511511
_type: "page";
@@ -566,12 +566,23 @@ export type GetPageQueryResult = {
566566
}>;
567567
}> | null;
568568
} | null;
569+
// Variable: sitemapData
570+
// Query: *[_type == "page" || _type == "post" && defined(slug.current)] | order(_type asc) { "slug": slug.current, _type, _updatedAt, }
571+
export type SitemapDataResult = Array<{
572+
slug: string;
573+
_type: "page";
574+
_updatedAt: string;
575+
} | {
576+
slug: string;
577+
_type: "post";
578+
_updatedAt: string;
579+
}>;
569580
// Variable: allPostsQuery
570581
// Query: *[_type == "post" && defined(slug.current)] | order(date desc, _updatedAt desc) { _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{firstName, lastName, picture}, }
571582
export type AllPostsQueryResult = Array<{
572583
_id: string;
573584
status: "draft" | "published";
574-
title: string | "Untitled";
585+
title: string;
575586
slug: string;
576587
excerpt: string | null;
577588
coverImage: {
@@ -609,7 +620,7 @@ export type AllPostsQueryResult = Array<{
609620
export type MorePostsQueryResult = Array<{
610621
_id: string;
611622
status: "draft" | "published";
612-
title: string | "Untitled";
623+
title: string;
613624
slug: string;
614625
excerpt: string | null;
615626
coverImage: {
@@ -643,7 +654,7 @@ export type MorePostsQueryResult = Array<{
643654
} | null;
644655
}>;
645656
// Variable: postQuery
646-
// Query: *[_type == "post" && slug.current == $slug] [0] { content[]{ ..., markDefs[]{ ..., link { ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } } } }, _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{firstName, lastName, picture}, }
657+
// Query: *[_type == "post" && slug.current == $slug] [0] { content[]{ ..., markDefs[]{ ..., _type == "link" => { "page": page->slug.current, "post": post->slug.current } } }, _id, "status": select(_originalId in path("drafts.**") => "draft", "published"), "title": coalesce(title, "Untitled"), "slug": slug.current, excerpt, coverImage, "date": coalesce(date, _updatedAt), "author": author->{firstName, lastName, picture}, }
647658
export type PostQueryResult = {
648659
content: Array<{
649660
children?: Array<{
@@ -657,30 +668,19 @@ export type PostQueryResult = {
657668
markDefs: Array<{
658669
linkType?: "href" | "page" | "post";
659670
href?: string;
660-
page?: {
661-
_ref: string;
662-
_type: "reference";
663-
_weak?: boolean;
664-
[internalGroqTypeReferenceTo]?: "page";
665-
};
666-
post?: {
667-
_ref: string;
668-
_type: "reference";
669-
_weak?: boolean;
670-
[internalGroqTypeReferenceTo]?: "post";
671-
};
671+
page: string | null;
672+
post: string | null;
672673
openInNewTab?: boolean;
673674
_type: "link";
674675
_key: string;
675-
link: null;
676676
}> | null;
677677
level?: number;
678678
_type: "block";
679679
_key: string;
680680
}> | null;
681681
_id: string;
682682
status: "draft" | "published";
683-
title: string | "Untitled";
683+
title: string;
684684
slug: string;
685685
excerpt: string | null;
686686
coverImage: {
@@ -729,10 +729,11 @@ import "@sanity/client";
729729
declare module "@sanity/client" {
730730
interface SanityQueries {
731731
"*[_type == \"settings\"][0]": SettingsQueryResult;
732-
"\n *[_type == 'page' && slug.current == $slug][0]{\n _id,\n _type,\n name,\n slug,\n heading,\n subheading,\n \"pageBuilder\": pageBuilder[]{\n ...,\n _type == \"callToAction\" => {\n ...,\n \n link {\n ...,\n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n }\n,\n }\n },\n }\n": GetPageQueryResult;
732+
"\n *[_type == 'page' && slug.current == $slug][0]{\n _id,\n _type,\n name,\n slug,\n heading,\n subheading,\n \"pageBuilder\": pageBuilder[]{\n ...,\n _type == \"callToAction\" => {\n ...,\n \n link {\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n,\n }\n },\n }\n": GetPageQueryResult;
733+
"\n *[_type == \"page\" || _type == \"post\" && defined(slug.current)] | order(_type asc) {\n \"slug\": slug.current,\n _type,\n _updatedAt,\n }\n": SitemapDataResult;
733734
"\n *[_type == \"post\" && defined(slug.current)] | order(date desc, _updatedAt desc) {\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": AllPostsQueryResult;
734735
"\n *[_type == \"post\" && _id != $skip && defined(slug.current)] | order(date desc, _updatedAt desc) [0...$limit] {\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": MorePostsQueryResult;
735-
"\n *[_type == \"post\" && slug.current == $slug] [0] {\n content[]{\n ...,\n markDefs[]{\n ...,\n \n link {\n ...,\n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n }\n\n }\n },\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": PostQueryResult;
736+
"\n *[_type == \"post\" && slug.current == $slug] [0] {\n content[]{\n ...,\n markDefs[]{\n ...,\n \n _type == \"link\" => {\n \"page\": page->slug.current,\n \"post\": post->slug.current\n }\n\n }\n },\n \n _id,\n \"status\": select(_originalId in path(\"drafts.**\") => \"draft\", \"published\"),\n \"title\": coalesce(title, \"Untitled\"),\n \"slug\": slug.current,\n excerpt,\n coverImage,\n \"date\": coalesce(date, _updatedAt),\n \"author\": author->{firstName, lastName, picture},\n\n }\n": PostQueryResult;
736737
"\n *[_type == \"post\" && defined(slug.current)]\n {\"slug\": slug.current}\n": PostPagesSlugsResult;
737738
"\n *[_type == \"page\" && defined(slug.current)]\n {\"slug\": slug.current}\n": PagesSlugsResult;
738739
}

nextjs-app/sanity/lib/queries.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,11 @@ export const getPageQuery = defineQuery(`
4545
}
4646
`);
4747

48-
export const allPagesQuery = defineQuery(`
49-
*[_type == "page" && defined(slug.current)] | order(date desc, _updatedAt desc) {
50-
_id,
48+
export const sitemapData = defineQuery(`
49+
*[_type == "page" || _type == "post" && defined(slug.current)] | order(_type asc) {
50+
"slug": slug.current,
5151
_type,
52-
name,
53-
slug,
54-
heading,
55-
subheading,
56-
"pageBuilder": pageBuilder[]{
57-
...,
58-
_type == "callToAction" => {
59-
...,
60-
${linkFields},
61-
}
62-
},
52+
_updatedAt,
6353
}
6454
`);
6555

0 commit comments

Comments
 (0)