feat: 动态sitemap.xml
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
export const metadata = {
|
||||
site: "https://ua42.com",
|
||||
siteTitle: "Uroboros",
|
||||
|
||||
postPageSize: 6
|
||||
}
|
||||
|
||||
33
src/pages/sitemap-pages.xml.ts
Normal file
33
src/pages/sitemap-pages.xml.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type {APIRoute} from 'astro';
|
||||
import {metadata} from "@/consts";
|
||||
|
||||
const site = metadata.site
|
||||
|
||||
const staticUrls = [
|
||||
'',
|
||||
'/archives',
|
||||
'/search',
|
||||
];
|
||||
|
||||
export const GET: APIRoute = async () => {
|
||||
const urls = staticUrls
|
||||
.filter((url, index, array) => {
|
||||
return array.indexOf(url) === index;
|
||||
});
|
||||
|
||||
const xml = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
${urls.map((url) => `<url>
|
||||
<loc>${site}${url}</loc>
|
||||
</url>`
|
||||
)
|
||||
.join('\n')}
|
||||
</urlset>`;
|
||||
|
||||
return new Response(xml, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/xml; charset=utf-8',
|
||||
'Cache-Control': 'public, max-age=3600, s-maxage=3600',
|
||||
},
|
||||
});
|
||||
};
|
||||
35
src/pages/sitemap-posts.xml.ts
Normal file
35
src/pages/sitemap-posts.xml.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type {APIRoute} from 'astro';
|
||||
import {metadata} from "@/consts";
|
||||
import {createHttp} from "@/lib/request.ts";
|
||||
import type {PostForSitemap} from "@/types";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
const site = metadata.site
|
||||
|
||||
export const GET: APIRoute = async (context) => {
|
||||
const http = createHttp(context.request.headers);
|
||||
|
||||
const {data} = await http.get<ApiResponse<PostForSitemap[]>>("posts/sitemap")
|
||||
|
||||
const urls = data
|
||||
.filter((url, index, array) => {
|
||||
return array.indexOf(url) === index;
|
||||
});
|
||||
|
||||
const xml = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
${urls.map((post) => `<url>
|
||||
<loc>${site}/${post.slug}</loc>
|
||||
<lastmod>${dayjs(post.updated_at || post.created_at).toISOString()}</lastmod>
|
||||
</url>`
|
||||
)
|
||||
.join('\n')}
|
||||
</urlset>`;
|
||||
|
||||
return new Response(xml, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/xml; charset=utf-8',
|
||||
'Cache-Control': 'public, max-age=3600, s-maxage=3600',
|
||||
},
|
||||
});
|
||||
};
|
||||
21
src/pages/sitemap.xml.ts
Normal file
21
src/pages/sitemap.xml.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type {APIRoute} from 'astro';
|
||||
import {metadata} from "@/consts";
|
||||
|
||||
const site = metadata.site
|
||||
|
||||
export const GET: APIRoute = () => {
|
||||
const xml = `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<sitemap>
|
||||
<loc>${site}/sitemap-pages.xml</loc>
|
||||
</sitemap>
|
||||
<sitemap>
|
||||
<loc>${site}/sitemap-posts.xml</loc>
|
||||
</sitemap>
|
||||
</sitemapindex>`;
|
||||
|
||||
return new Response(xml, {
|
||||
headers: {
|
||||
'Content-Type': 'application/xml; charset=utf-8',
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -24,3 +24,9 @@ export type CategoriesStats = {
|
||||
code: string
|
||||
post_count: number;
|
||||
}
|
||||
|
||||
export type PostForSitemap = {
|
||||
slug: string
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user