Skip to content

Commit

Permalink
fix(sitemap): include root path in sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 20, 2024
1 parent 166d745 commit a8398e1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/app/(main)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import SectionRenderer from "src/utils/renderSection";
import { fetchSeoData, generateMetadataFromSeo } from "src/utils/seo";
import { LinkType } from "studio/lib/interfaces/navigation";
import { PageBuilder } from "studio/lib/interfaces/pages";
import { LANDING_QUERY } from "studio/lib/queries/navigation";
import { LANDING_PAGE_REF_QUERY } from "studio/lib/queries/navigation";
import { PAGE_QUERY, SEO_PAGE_QUERY } from "studio/lib/queries/page";
import { loadStudioQuery } from "studio/lib/store";

export async function generateMetadata(): Promise<Metadata> {
const { data: landingId } = await loadStudioQuery<string>(LANDING_QUERY);
const { data: landingId } = await loadStudioQuery<string>(
LANDING_PAGE_REF_QUERY,
);
const seo = await fetchSeoData(SEO_PAGE_QUERY, { id: landingId });
return generateMetadataFromSeo(seo);
}
Expand All @@ -36,7 +38,7 @@ const Home = async () => {
const { perspective, isDraftMode } = getDraftModeInfo();

const { data: landingId } = await loadStudioQuery<string>(
LANDING_QUERY,
LANDING_PAGE_REF_QUERY,
{},
{ perspective },
);
Expand Down
39 changes: 28 additions & 11 deletions src/app/sitemap.ts
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;
}
4 changes: 2 additions & 2 deletions studio/components/CustomCallToActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "sanity";

import { fetchWithToken } from "studio/lib/fetchWithToken";
import { LANDING_QUERY } from "studio/lib/queries/navigation";
import { LANDING_PAGE_REF_QUERY } from "studio/lib/queries/navigation";

type CustomCallToActionsProps = ArrayOfObjectsInputProps<
{ _key: string },
Expand All @@ -28,7 +28,7 @@ const CustomCallToActions: React.FC<CustomCallToActionsProps> = (props) => {
const fetchLandingId = async () => {
try {
setLoading(true);
const landingId = await fetchWithToken<string>(LANDING_QUERY);
const landingId = await fetchWithToken<string>(LANDING_PAGE_REF_QUERY);
setLandingPageId(landingId);
} catch (error) {
console.error("Failed to fetch navigation manager", error);
Expand Down
5 changes: 5 additions & 0 deletions studio/lib/interfaces/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export interface Reference {
_type: "reference";
_ref: string;
}

export interface DocumentWithSlug {
slug: Slug;
_updatedAt: string;
}
5 changes: 5 additions & 0 deletions studio/lib/queries/document.ts
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)]
`;
6 changes: 5 additions & 1 deletion studio/lib/queries/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export const NAV_QUERY = groq`
}
`;

export const LANDING_QUERY = groq`
export const LANDING_PAGE_REF_QUERY = groq`
*[_type == "navigationManager"][0].setLanding._ref
`;

export const LANDING_PAGE_QUERY = groq`
*[_type == "navigationManager"][0].setLanding ->
`;

0 comments on commit a8398e1

Please sign in to comment.