-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sitemap): include root path in sitemap
- Loading branch information
Showing
6 changed files
with
50 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,42 @@ | ||
import type { MetadataRoute } from "next"; | ||
|
||
import { client } from "studio/lib/client"; | ||
import { Slug } from "studio/lib/interfaces/global"; | ||
import { DocumentWithSlug } from "studio/lib/interfaces/global"; | ||
import { PageBuilder } from "studio/lib/interfaces/pages"; | ||
import { DOCUMENTS_WITH_SLUG_QUERY } from "studio/lib/queries/document"; | ||
import { LANDING_PAGE_QUERY } from "studio/lib/queries/navigation"; | ||
import { token } from "studio/lib/token"; | ||
|
||
interface SitemapDocument { | ||
slug: Slug; | ||
_updatedAt: string; | ||
} | ||
|
||
const clientWithToken = client.withConfig({ token }); | ||
|
||
export const dynamic = "force-dynamic"; | ||
export const fetchCache = "default-no-store"; | ||
|
||
export default async function sitemap(): Promise<MetadataRoute.Sitemap> { | ||
const documents = | ||
await clientWithToken.fetch<SitemapDocument[]>(`*[defined(slug)]`); | ||
|
||
return documents.map((s) => ({ | ||
url: new URL(s.slug.current, process.env.NEXT_PUBLIC_URL).toString(), | ||
const baseUrl = | ||
process.env.NEXT_PUBLIC_URL !== undefined && | ||
URL.canParse(process.env.NEXT_PUBLIC_URL) | ||
? new URL(process.env.NEXT_PUBLIC_URL) | ||
: undefined; | ||
if (baseUrl === undefined) { | ||
console.error("Failed to generate sitemap, missing baseUrl"); | ||
return []; | ||
} | ||
const slugDocuments = await clientWithToken.fetch<DocumentWithSlug[]>( | ||
DOCUMENTS_WITH_SLUG_QUERY, | ||
); | ||
const sitemapEntries = slugDocuments.map((s) => ({ | ||
url: new URL(s.slug.current, baseUrl).toString(), | ||
lastModified: new Date(s._updatedAt), | ||
})); | ||
const landingPage = await clientWithToken.fetch<PageBuilder | null>( | ||
LANDING_PAGE_QUERY, | ||
); | ||
if (landingPage !== null) { | ||
sitemapEntries.push({ | ||
url: baseUrl.toString(), | ||
lastModified: new Date(landingPage._updatedAt), | ||
}); | ||
} | ||
return sitemapEntries; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { groq } from "next-sanity"; | ||
|
||
export const DOCUMENTS_WITH_SLUG_QUERY = groq` | ||
*[defined(slug)] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters