From 3f36ea25e619dd283e104152a0c32a020ba1060a Mon Sep 17 00:00:00 2001 From: Alexandra Goff Date: Wed, 16 Oct 2024 09:40:52 -0700 Subject: [PATCH] fix: add to robots.txt (#578) * chore: trigger build * fix: add to robots.txt --- app/[locale]/sitemap.xml/route.ts | 4 ++++ app/robots.ts | 3 +++ lib/api/sitemap/index.ts | 16 ++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/[locale]/sitemap.xml/route.ts b/app/[locale]/sitemap.xml/route.ts index 7f7aaca5..1114bc61 100644 --- a/app/[locale]/sitemap.xml/route.ts +++ b/app/[locale]/sitemap.xml/route.ts @@ -1,6 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import { XMLBuilder } from "fast-xml-parser"; import { + generateAlternateLanguages, generateSitemapUrl, getSitemapData, getSiteMapNewsData, @@ -15,6 +16,7 @@ export async function GET( return { loc: generateSitemapUrl(uri, locale), lastmod: dateUpdated, + "xhtml:link": generateAlternateLanguages(uri, locale), }; }); const { siteTitle, news } = await getSiteMapNewsData(locale); @@ -28,6 +30,7 @@ export async function GET( const entry = { loc: generateSitemapUrl(uri, locale), lastmod: dateUpdated, + "xhtml:link": generateAlternateLanguages(uri, locale), }; if (new Date(date) > recentNewsThreshold) { @@ -51,6 +54,7 @@ export async function GET( }, urlset: { $xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", + "$xmlns:xhtml": "http://www.w3.org/1999/xhtml", "$xmlns:news": "http://www.google.com/schemas/sitemap-news/0.9", url: pageData.concat(newsData), }, diff --git a/app/robots.ts b/app/robots.ts index fc42c639..f8c06402 100644 --- a/app/robots.ts +++ b/app/robots.ts @@ -1,10 +1,13 @@ import { MetadataRoute } from "next"; +const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || ""; + export default function robots(): MetadataRoute.Robots { return { rules: { userAgent: "*", allow: "/", }, + sitemap: `${baseUrl}/sitemap_index.xml`, }; } diff --git a/lib/api/sitemap/index.ts b/lib/api/sitemap/index.ts index 67bbe004..2760be22 100644 --- a/lib/api/sitemap/index.ts +++ b/lib/api/sitemap/index.ts @@ -3,7 +3,7 @@ import { getSiteFromLocale } from "@/lib/helpers/site"; import queryAPI from "@/lib/api/client/query"; import tags from "@/lib/api/client/tags"; import { CRAFT_HOMEPAGE_URI } from "@/lib/constants"; -import { fallbackLng } from "@/lib/i18n/settings"; +import { fallbackLng, languages } from "@/lib/i18n/settings"; interface PageMetadata { uri: string; @@ -16,7 +16,7 @@ export const generateSitemapUrl = ( locale: string, preserveLocale = false ) => { - const segments = uri === CRAFT_HOMEPAGE_URI ? [] : [uri]; + const segments = uri === CRAFT_HOMEPAGE_URI ? [] : [encodeURIComponent(uri)]; if (preserveLocale || locale !== fallbackLng) { segments.unshift(locale); @@ -27,6 +27,18 @@ export const generateSitemapUrl = ( return segments.join("/"); }; +export const generateAlternateLanguages = (uri: string, locale: string) => { + return languages + .filter((language) => language !== locale) + .map((language) => { + return { + $rel: "alternate", + $hreflang: language, + $href: generateSitemapUrl(uri, language), + }; + }, {}); +}; + export const getSiteMapNewsData = async (locale: string) => { const site = getSiteFromLocale(locale);