Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add to robots.txt #578

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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