From f6e75acc3c6de0075d4001ab83df1d9614da2d27 Mon Sep 17 00:00:00 2001 From: Ryan Freeman Date: Mon, 31 Jul 2023 22:24:30 +0100 Subject: [PATCH] Added generated sitemap --- app/sitemap.ts | 37 +++++++++++++---------- lib/generateSitemap.ts | 67 ------------------------------------------ 2 files changed, 22 insertions(+), 82 deletions(-) delete mode 100644 lib/generateSitemap.ts diff --git a/app/sitemap.ts b/app/sitemap.ts index baacad6..39f41a9 100644 --- a/app/sitemap.ts +++ b/app/sitemap.ts @@ -1,18 +1,25 @@ -import { MetadataRoute } from 'next' +import {MetadataRoute} from 'next' +import {getAllArticles} from '@/lib/getAllArticles' -export default function sitemap(): MetadataRoute.Sitemap { - return [ - { - url: 'https://acme.com', - lastModified: new Date(), - }, - { - url: 'https://acme.com/about', - lastModified: new Date(), - }, - { - url: 'https://acme.com/blog', - lastModified: new Date(), - }, +export default async function sitemap(): Promise { + const urls = [ + 'https://ryanfreeman.dev/', + 'https://ryanfreeman.dev/about', + 'https://ryanfreeman.dev/dashboard', + 'https://ryanfreeman.dev/writing', + 'https://ryanfreeman.dev/projects', + 'https://ryanfreeman.dev/uses' ] + + const pages = urls.map(url => ({ + url, + lastModified: new Date() + })) + + const posts = (await getAllArticles()).map(({slug, date}) => ({ + url: `https://ryanfreeman.dev/${slug}/`, + lastModified: new Date(date).toISOString() + })) + + return [...pages, ...posts] } \ No newline at end of file diff --git a/lib/generateSitemap.ts b/lib/generateSitemap.ts deleted file mode 100644 index a691169..0000000 --- a/lib/generateSitemap.ts +++ /dev/null @@ -1,67 +0,0 @@ -import glob from 'fast-glob' -import path from 'path' -import {getAllArticles} from '@/lib/getAllArticles' -import {writeFile} from 'fs/promises' - -const BASE_URL = process.env.NEXT_PUBLIC_SITE_URL - -async function createSitemap(pages: string[]) { - const sitemap = - ` - - ${pages.map((url) => - ` - ${url} - ${new Date().toISOString()} - daily - 1.0 - `).join('')} - `.replace(/(\s\s+|\t)/g, ' ').trim() - - await writeFile('./public/sitemap.xml', sitemap, 'utf8') -} - -async function createRobots() { - const robots = - `# * - User-agent: * - Allow: / - - # Host - Host: ${BASE_URL} - - # Sitemaps - Sitemap: ${BASE_URL}/sitemap.xml`.replace(/(\s\s+|\t)/g, ' ').trim() - - await writeFile('./public/robots.txt', robots, 'utf8') -} - -export async function generateSitemap() { - const excluded = [ - '_app.tsx', - '_document.tsx', - 'index.tsx' - ] - - const pages = (await glob(['*.tsx', '*.jsx'], { - cwd: path.join(process.cwd(), '/pages/'), - })).filter((page) => { - return !excluded - .includes(page) - }).map((page) => { - return `${BASE_URL}/${page}` - .replace(/\.(tsx|jsx)$/, '') - }) - - pages.unshift(`${BASE_URL}/`) - pages.push(`${BASE_URL}/writing`) - - const articles = await getAllArticles() - const slugs = articles.map(({slug}) => `${BASE_URL}/writing/${slug}`) - const allPages = [...pages, ...slugs] - - await Promise.all([ - await createSitemap(allPages), - await createRobots() - ]) -} \ No newline at end of file