Skip to content

Commit 91f3adf

Browse files
committed
Fix external links in sitemap
1 parent 4af1d55 commit 91f3adf

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

apps/site/app/sitemap.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { availableLocaleCodes, defaultLocale } from '@node-core/website-i18n';
22

3-
import { BASE_PATH } from '#site/next.constants.mjs';
4-
import { BASE_URL } from '#site/next.constants.mjs';
5-
import { EXTERNAL_LINKS_SITEMAP } from '#site/next.constants.mjs';
3+
import {
4+
BASE_PATH,
5+
BASE_URL,
6+
EXTERNAL_LINKS_SITEMAP,
7+
} from '#site/next.constants.mjs';
68
import { BLOG_DYNAMIC_ROUTES } from '#site/next.dynamic.constants.mjs';
79
import { dynamicRouter } from '#site/next.dynamic.mjs';
810

@@ -16,8 +18,11 @@ const nonDefaultLocales = availableLocaleCodes.filter(
1618
l => l !== defaultLocale.code
1719
);
1820

19-
const getAlternatePath = (r: string, locales: Array<string>) =>
20-
Object.fromEntries(locales.map(l => [l, `${baseUrlAndPath}/${l}/${r}`]));
21+
const getFullPath = (r: string, l: string) =>
22+
/^https?:\/\//.test(r) ? r : `${baseUrlAndPath}/${l}/${r}`;
23+
24+
const getAlternatePaths = (r: string, locales: Array<string>) =>
25+
Object.fromEntries(locales.map(l => [l, getFullPath(r, l)]));
2126

2227
// This allows us to generate a `sitemap.xml` file dynamically based on the needs of the Node.js Website
2328
const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
@@ -27,10 +32,10 @@ const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
2732
const currentDate = new Date().toISOString();
2833

2934
const getSitemapEntry = (r: string, locales: Array<string> = []) => ({
30-
url: `${baseUrlAndPath}/${defaultLocale.code}/${r}`,
35+
url: getFullPath(r, defaultLocale.code),
3136
lastModified: currentDate,
3237
changeFrequency: 'always' as const,
33-
alternates: { languages: getAlternatePath(r, locales) },
38+
alternates: { languages: getAlternatePaths(r, locales) },
3439
});
3540

3641
const staticPaths = routes.map(r => getSitemapEntry(r, nonDefaultLocales));

0 commit comments

Comments
 (0)