Skip to content

Commit

Permalink
feat: sitemap (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
jy95 authored Nov 24, 2024
1 parent 93f948d commit fe84e6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {MetadataRoute} from 'next';
import {routing, getPathname} from '@/i18n/routing';

type Href = Parameters<typeof getPathname>[0]['href'];
type pathnames = typeof routing.pathnames;
type AvailableRoutes = keyof pathnames;

// Adapt this as necessary
const host = 'https://jy95.github.io';

export default function sitemap(): MetadataRoute.Sitemap {
const paths = Object.keys(routing.pathnames) as AvailableRoutes[];
return paths.map(getEntry);
}

function getEntry(href: Href): MetadataRoute.Sitemap[number] {
return {
url: getUrl(href, routing.defaultLocale),
lastModified: new Date(),
changeFrequency: "daily",
alternates: {
languages: Object.fromEntries(
routing.locales.map((locale) => [locale, getUrl(href, locale)])
)
}
}
}

function getUrl(href: Href, locale: (typeof routing.locales)[number]) {
const pathname = getPathname({locale, href});
return host + pathname;
}
3 changes: 1 addition & 2 deletions src/i18n/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ export const routing = defineRouting({

// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const {Link, redirect, usePathname, useRouter} =
createNavigation(routing);
export const {Link, redirect, usePathname, useRouter, getPathname} = createNavigation(routing);

0 comments on commit fe84e6c

Please sign in to comment.