From 5af84feb3871285a0719eac68d68a2a7c4c90b78 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Wed, 4 Sep 2024 13:03:48 +0200 Subject: [PATCH 1/5] feat: add NEXT_PUBLIC_URL env variable --- .env.development | 1 + .env.production | 1 + 2 files changed, 2 insertions(+) create mode 100644 .env.development create mode 100644 .env.production diff --git a/.env.development b/.env.development new file mode 100644 index 000000000..812358268 --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +NEXT_PUBLIC_URL=http://localhost:3000 \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 000000000..01b130c6a --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +NEXT_PUBLIC_URL=https://$NEXT_PUBLIC_VERCEL_URL \ No newline at end of file From 3c1d24f4bd34d88f0d4e8a7c6973a24ef3249ca4 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Wed, 4 Sep 2024 13:07:24 +0200 Subject: [PATCH 2/5] feat: basic sitemap generation --- src/app/sitemap.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/app/sitemap.ts diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts new file mode 100644 index 000000000..6b02da5a2 --- /dev/null +++ b/src/app/sitemap.ts @@ -0,0 +1,17 @@ +import type { MetadataRoute } from "next"; +import { client } from "../../studio/lib/client"; +import { Slug } from "../../studio/lib/payloads/global"; + +interface SitemapDocument { + slug: Slug; + _updatedAt: string; +} + +export default async function sitemap(): Promise { + const documents = await client.fetch(`*[defined(slug)]`); + + return documents.map((s) => ({ + url: new URL(s.slug.current, process.env.NEXT_PUBLIC_URL).toString(), + lastModified: new Date(s._updatedAt), + })); +} From ab0b845d063b11728ebdcdd210e770cc8a4e2693 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Wed, 4 Sep 2024 13:32:09 +0200 Subject: [PATCH 3/5] feat(sitemap): generate robots.txt with sitemap reference --- src/app/robots.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/app/robots.ts diff --git a/src/app/robots.ts b/src/app/robots.ts new file mode 100644 index 000000000..0c36b9bc3 --- /dev/null +++ b/src/app/robots.ts @@ -0,0 +1,10 @@ +import type { MetadataRoute } from "next"; + +export default function robots(): MetadataRoute.Robots { + return { + rules: { + userAgent: "*", + }, + sitemap: new URL("sitemap.xml", process.env.NEXT_PUBLIC_URL).toString(), + }; +} From b363ccfe0beddd91c88847b15e303276494c9b18 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Wed, 4 Sep 2024 13:37:23 +0200 Subject: [PATCH 4/5] feat(robots): disallow crawling of studio and api paths --- src/app/robots.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/robots.ts b/src/app/robots.ts index 0c36b9bc3..3265d2247 100644 --- a/src/app/robots.ts +++ b/src/app/robots.ts @@ -4,6 +4,7 @@ export default function robots(): MetadataRoute.Robots { return { rules: { userAgent: "*", + disallow: ["/studio", "/shared", "/api"], }, sitemap: new URL("sitemap.xml", process.env.NEXT_PUBLIC_URL).toString(), }; From 1c8e6aa8ce93a43e2090c1d1bda303df966b0aba Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Mon, 9 Sep 2024 11:29:02 +0200 Subject: [PATCH 5/5] fix(sitemap): fetch Sanity data with token --- src/app/sitemap.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts index 6b02da5a2..73652f949 100644 --- a/src/app/sitemap.ts +++ b/src/app/sitemap.ts @@ -1,14 +1,18 @@ 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 { - const documents = await client.fetch(`*[defined(slug)]`); + const documents = + await clientWithToken.fetch(`*[defined(slug)]`); return documents.map((s) => ({ url: new URL(s.slug.current, process.env.NEXT_PUBLIC_URL).toString(),