Skip to content

Commit

Permalink
v3 - basic sitemap generation (#580)
Browse files Browse the repository at this point in the history
* feat: add NEXT_PUBLIC_URL env variable

* feat: basic sitemap generation

* feat(sitemap): generate robots.txt with sitemap reference

* feat(robots): disallow crawling of studio and api paths

* fix(sitemap): fetch Sanity data with token
  • Loading branch information
mathiazom authored Sep 9, 2024
1 parent a88e146 commit f5d19c4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_URL=http://localhost:3000
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_URL=https://$NEXT_PUBLIC_VERCEL_URL
11 changes: 11 additions & 0 deletions src/app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { MetadataRoute } from "next";

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: "*",
disallow: ["/studio", "/shared", "/api"],
},
sitemap: new URL("sitemap.xml", process.env.NEXT_PUBLIC_URL).toString(),
};
}
21 changes: 21 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { MetadataRoute } from "next";
import { client } from "../../studio/lib/client";
import { Slug } from "../../studio/lib/payloads/global";
import { token } from "../../studio/lib/token";

interface SitemapDocument {
slug: Slug;
_updatedAt: string;
}

const clientWithToken = client.withConfig({ token });

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(),
lastModified: new Date(s._updatedAt),
}));
}

0 comments on commit f5d19c4

Please sign in to comment.