Skip to content

Commit

Permalink
fix: add to robots.txt (#578)
Browse files Browse the repository at this point in the history
* chore: trigger build

* fix: add to robots.txt
  • Loading branch information
alexgoff committed Oct 16, 2024
1 parent 2952017 commit 3f36ea2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/[locale]/sitemap.xml/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from "next/server";
import { XMLBuilder } from "fast-xml-parser";
import {
generateAlternateLanguages,
generateSitemapUrl,
getSitemapData,
getSiteMapNewsData,
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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),
},
Expand Down
3 changes: 3 additions & 0 deletions app/robots.ts
Original file line number Diff line number Diff line change
@@ -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`,
};
}
16 changes: 14 additions & 2 deletions lib/api/sitemap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 3f36ea2

Please sign in to comment.