Update blog sitemap
This commit is contained in:
@@ -3,7 +3,7 @@ import { print } from 'graphql';
|
||||
import getBlogs from '@/graphql/blogs/getBlogs.graphql';
|
||||
import { formatDate } from "@/lib/dates";
|
||||
|
||||
export async function getAllBlogs(blogSettings: BlogSettings): Promise<BlogPost[]> {
|
||||
export async function getAllBlogs(settings: GlobalSettings): Promise<BlogPost[]> {
|
||||
const client = await createDirectusConnection();
|
||||
const result = await client.query(print(getBlogs), {
|
||||
date: formatDate(new Date(), "%Y-%M-%D")
|
||||
@@ -13,7 +13,8 @@ export async function getAllBlogs(blogSettings: BlogSettings): Promise<BlogPost[
|
||||
|
||||
result["Blogs"].forEach((blogRecord: any) => {
|
||||
let dates: string[] = [
|
||||
blogSettings.lastModified,
|
||||
settings.blog.lastModified.toISOString(),
|
||||
settings.website.lastModified.toISOString(),
|
||||
blogRecord["date_created"],
|
||||
blogRecord["date_updated"],
|
||||
blogRecord["search_engine"][0]["date_created"],
|
||||
|
||||
10
astro/src/lib/routing.ts
Normal file
10
astro/src/lib/routing.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function getBlogRoute(blogSettings: BlogSettings, blog: BlogPost) {
|
||||
const date = new Date(blog.date);
|
||||
|
||||
return blogSettings.blogRouteTemplate
|
||||
.replaceAll("%Y", date.getFullYear().toString())
|
||||
.replaceAll("%M", (date.getMonth() + 1).toString().padStart(2, '0'))
|
||||
.replaceAll("%D", date.getDate().toString().padStart(2, '0'))
|
||||
.replaceAll("%R", blog.url)
|
||||
.replace(/\/+/g, '/');
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { getSettings } from "@/content/settings/settings"
|
||||
import WebpageLayout from "@/layouts/WebpageLayout.astro";
|
||||
|
||||
const settings = await getSettings();
|
||||
const blogs = await getAllBlogs(settings.blog);
|
||||
const blogs = await getAllBlogs(settings);
|
||||
---
|
||||
|
||||
<WebpageLayout>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { getAllBlogs } from "@/content/blogs/blogs";
|
||||
import { getSettings } from "@/content/settings/settings";
|
||||
import { getBlogRoute } from "@/lib/routing";
|
||||
import type { APIRoute } from "astro";
|
||||
import minifyXML from "minify-xml";
|
||||
|
||||
@@ -12,14 +14,22 @@ export const GET = (async ({ params }) => {
|
||||
});
|
||||
}
|
||||
|
||||
const currentPage = params.page;
|
||||
const currentPage = 1;
|
||||
|
||||
let pages: SitemapPage[] = [
|
||||
{
|
||||
url: "/",
|
||||
lastModified: new Date()
|
||||
}
|
||||
];
|
||||
const blogs = await getAllBlogs(settings);
|
||||
const selectedBlogs = blogs.slice(
|
||||
((Number(currentPage) - 1) * settings.sitemap.perPage),
|
||||
Number(currentPage) * settings.sitemap.perPage - 1
|
||||
);
|
||||
|
||||
let pages: SitemapPage[] = [];
|
||||
|
||||
selectedBlogs.forEach((blog) => {
|
||||
pages.push({
|
||||
url: getBlogRoute(settings.blog, blog),
|
||||
lastModified: blog.lastModified
|
||||
});
|
||||
})
|
||||
|
||||
let sitemapContent = `
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@@ -44,8 +54,9 @@ export const GET = (async ({ params }) => {
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const settings = await getSettings();
|
||||
const blogs = await getAllBlogs(settings);
|
||||
|
||||
const blogCount = 250;
|
||||
const blogCount = blogs.length;
|
||||
const perPage = settings.sitemap.perPage;
|
||||
const pages = Math.ceil(blogCount / perPage);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getAllBlogs } from "@/content/blogs/blogs";
|
||||
import { getSettings } from "@/content/settings/settings";
|
||||
import type { APIRoute } from "astro";
|
||||
import minifyXML from "minify-xml";
|
||||
@@ -12,7 +13,8 @@ export const GET = (async () => {
|
||||
});
|
||||
}
|
||||
|
||||
const blogCount = 250;
|
||||
const blogs = await getAllBlogs(settings);
|
||||
const blogCount = blogs.length;
|
||||
const perPage = settings.sitemap.perPage;
|
||||
const pages = Math.ceil(blogCount / perPage);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user