Skip to content

Commit

Permalink
feat: support sitemap reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Aug 25, 2023
1 parent 225f95d commit 8ba9f33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ let configs = {
},
async rewrites() {
return {
beforeFiles: [{ source: '/feed', destination: '/api/feed' }],
beforeFiles: [
{ source: '/feed', destination: '/api/feed' },
{ source: '/atom.xml', destination: '/api/feed' },
{ source: '/sitemap', destination: '/api/sitemap' },
{ source: '/sitemap.xml', destination: '/api/sitemap' },
],
fallback: [
{ source: '/:page*/:slug*', destination: '/posts/:page*/:slug*' },
],
Expand Down
26 changes: 26 additions & 0 deletions src/pages/api/sitemap/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { NextApiResponse } from 'next'

import { apiClient } from '~/utils/client'

export default async function handler(_, res: NextApiResponse) {
const { data } = await apiClient.aggregate.proxy.sitemap.get<{
data: { url: string; publishedAt: string }[]
}>()

const xml = `
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
${data
.map(
(item: any) => `<url>
<loc>${item.url}</loc>
<lastmod>${item.publishedAt || 'N/A'}</lastmod>
</url>`,
)

.join('')}
</urlset>
`.trim()

res.setHeader('Content-Type', 'application/xml')
return res.send(xml)
}

1 comment on commit 8ba9f33

@vercel
Copy link

@vercel vercel bot commented on 8ba9f33 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.