Get all projects and update sitemaps for it immediatly as well
This commit is contained in:
73
astro/src/content/projects/projects.ts
Normal file
73
astro/src/content/projects/projects.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { formatDate } from "@/lib/dates";
|
||||||
|
import { createDirectusConnection } from "@/lib/directus";
|
||||||
|
import { print } from "graphql";
|
||||||
|
import getProjects from '@/graphql/projects/getProjects.graphql';
|
||||||
|
|
||||||
|
|
||||||
|
export async function getAllProjects(settings: GlobalSettings): Promise<ProjectPost[]> {
|
||||||
|
const client = await createDirectusConnection();
|
||||||
|
const result = await client.query(print(getProjects), {
|
||||||
|
date: formatDate(new Date(), "%Y-%M-%D")
|
||||||
|
});
|
||||||
|
|
||||||
|
let projects: ProjectPost[] = [];
|
||||||
|
|
||||||
|
result["Projects"].forEach((projectRecord: any) => {
|
||||||
|
let dates: string[] = [
|
||||||
|
settings.project.lastModified.toISOString(),
|
||||||
|
settings.website.lastModified.toISOString(),
|
||||||
|
projectRecord["date_created"],
|
||||||
|
projectRecord["date_updated"],
|
||||||
|
projectRecord["search_engine"][0]["date_created"],
|
||||||
|
projectRecord["search_engine"][0]["date_updated"],
|
||||||
|
projectRecord["search_engine"][0]["thumbnail"]["created_on"]
|
||||||
|
];
|
||||||
|
|
||||||
|
const project: ProjectPost = {
|
||||||
|
lastModified: new Date(),
|
||||||
|
title: projectRecord["title"],
|
||||||
|
content: projectRecord["content"],
|
||||||
|
date: projectRecord["date"],
|
||||||
|
url: projectRecord["url"],
|
||||||
|
searchEngine: {
|
||||||
|
title: projectRecord["search_engine"][0]["title"],
|
||||||
|
description: projectRecord["search_engine"][0]["description"],
|
||||||
|
allowCrawlers: projectRecord["search_engine"][0]["allow_crawler"],
|
||||||
|
canonical: projectRecord["search_engine"][0]["canonical"],
|
||||||
|
priority: projectRecord["search_engine"][0]["priority"],
|
||||||
|
thumbnail: {
|
||||||
|
url: projectRecord["search_engine"][0]["thumbnail"]["filename_disk"],
|
||||||
|
height: projectRecord["search_engine"][0]["thumbnail"]["height"],
|
||||||
|
width: projectRecord["search_engine"][0]["thumbnail"]["width"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
tags: []
|
||||||
|
};
|
||||||
|
|
||||||
|
projectRecord["tags"].forEach((tagRecord: any) => {
|
||||||
|
project["tags"].push({
|
||||||
|
text: tagRecord["Tags_id"]["text"],
|
||||||
|
code: tagRecord["Tags_id"]["code"],
|
||||||
|
color: tagRecord["Tags_id"]["color"]
|
||||||
|
});
|
||||||
|
|
||||||
|
dates.push(tagRecord["Tags_id"]["date_created"]);
|
||||||
|
dates.push(tagRecord["Tags_id"]["date_updated"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (dates.filter(e => e !== null).length === 0) {
|
||||||
|
project.lastModified = new Date();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
||||||
|
return new Date(b).getTime() - new Date(a).getTime();
|
||||||
|
});
|
||||||
|
|
||||||
|
project.lastModified = new Date(sortedDates[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
projects.push(project);
|
||||||
|
});
|
||||||
|
|
||||||
|
return projects;
|
||||||
|
}
|
||||||
39
astro/src/graphql/projects/getProjects.graphql
Normal file
39
astro/src/graphql/projects/getProjects.graphql
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
query getAllProjects($date: String!) {
|
||||||
|
Projects(sort: ["-date", "-date_created"], filter: { status: { _eq: "published" }, date: { _gte: $date } }) {
|
||||||
|
id,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
status,
|
||||||
|
title,
|
||||||
|
url,
|
||||||
|
date,
|
||||||
|
content,
|
||||||
|
tags {
|
||||||
|
Tags_id {
|
||||||
|
id,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
text,
|
||||||
|
code,
|
||||||
|
color
|
||||||
|
}
|
||||||
|
},
|
||||||
|
search_engine {
|
||||||
|
id,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
thumbnail {
|
||||||
|
id,
|
||||||
|
created_on,
|
||||||
|
filename_download,
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
},
|
||||||
|
canonical,
|
||||||
|
allow_crawler,
|
||||||
|
priority
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,3 +8,14 @@ export function getBlogRoute(blogSettings: BlogSettings, blog: BlogPost) {
|
|||||||
.replaceAll("%R", blog.url)
|
.replaceAll("%R", blog.url)
|
||||||
.replace(/\/+/g, '/');
|
.replace(/\/+/g, '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getProjectRoute(projectSettings: ProjectSettings, project: ProjectPost) {
|
||||||
|
const date = new Date(project.date);
|
||||||
|
|
||||||
|
return projectSettings.projectRouteTemplate
|
||||||
|
.replaceAll("%Y", date.getFullYear().toString())
|
||||||
|
.replaceAll("%M", (date.getMonth() + 1).toString().padStart(2, '0'))
|
||||||
|
.replaceAll("%D", date.getDate().toString().padStart(2, '0'))
|
||||||
|
.replaceAll("%R", project.url)
|
||||||
|
.replace(/\/+/g, '/');
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { getAllBlogs } from "@/content/blogs/blogs";
|
import { getAllBlogs } from "@/content/blogs/blogs";
|
||||||
|
import { getAllProjects } from "@/content/projects/projects";
|
||||||
import { getSettings } from "@/content/settings/settings";
|
import { getSettings } from "@/content/settings/settings";
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import minifyXML from "minify-xml";
|
import minifyXML from "minify-xml";
|
||||||
@@ -36,9 +37,25 @@ export const GET = (async () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
if (settings.project.enabled) {
|
if (settings.project.enabled) {
|
||||||
|
const projectLastModifieds = [
|
||||||
|
settings.project.lastModified,
|
||||||
|
settings.sitemap.lastModified,
|
||||||
|
settings.website.lastModified
|
||||||
|
];
|
||||||
|
|
||||||
|
let projects = await getAllProjects(settings);
|
||||||
|
|
||||||
|
projects.forEach((project) => {
|
||||||
|
projectLastModifieds.push(project.lastModified);
|
||||||
|
});
|
||||||
|
|
||||||
|
const lastModifiedProjects = projectLastModifieds.sort((a: Date, b: Date) => {
|
||||||
|
return b.getTime() - a.getTime();
|
||||||
|
});
|
||||||
|
|
||||||
sitemapIndex.push({
|
sitemapIndex.push({
|
||||||
url: "/sitemap/projects.xml",
|
url: "/sitemap/projects.xml",
|
||||||
lastModified: new Date()
|
lastModified: lastModifiedProjects[0]
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
if (settings.photo.enabled) {
|
if (settings.photo.enabled) {
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import { getAllProjects } from "@/content/projects/projects";
|
||||||
import { getSettings } from "@/content/settings/settings";
|
import { getSettings } from "@/content/settings/settings";
|
||||||
|
import { getProjectRoute } from "@/lib/routing";
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import minifyXML from "minify-xml";
|
import minifyXML from "minify-xml";
|
||||||
|
|
||||||
@@ -14,12 +16,20 @@ export const GET = (async ({ params }) => {
|
|||||||
|
|
||||||
const currentPage = params.page;
|
const currentPage = params.page;
|
||||||
|
|
||||||
let pages: SitemapPage[] = [
|
const projects = await getAllProjects(settings);
|
||||||
{
|
const selectedProjects = projects.slice(
|
||||||
url: "/",
|
((Number(currentPage) - 1) * settings.sitemap.perPage),
|
||||||
lastModified: new Date()
|
Number(currentPage) * settings.sitemap.perPage - 1
|
||||||
}
|
);
|
||||||
];
|
|
||||||
|
let pages: SitemapPage[] = [];
|
||||||
|
|
||||||
|
selectedProjects.forEach((project) => {
|
||||||
|
pages.push({
|
||||||
|
url: getProjectRoute(settings.project, project),
|
||||||
|
lastModified: project.lastModified
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
let sitemapContent = `
|
let sitemapContent = `
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
@@ -44,8 +54,9 @@ export const GET = (async ({ params }) => {
|
|||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
const settings = await getSettings();
|
const settings = await getSettings();
|
||||||
|
const projects = await getAllProjects(settings);
|
||||||
|
|
||||||
const projectCount = 250;
|
const projectCount = projects.length;
|
||||||
const perPage = settings.sitemap.perPage;
|
const perPage = settings.sitemap.perPage;
|
||||||
const pages = Math.ceil(projectCount / perPage);
|
const pages = Math.ceil(projectCount / perPage);
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { getAllProjects } from "@/content/projects/projects";
|
||||||
import { getSettings } from "@/content/settings/settings";
|
import { getSettings } from "@/content/settings/settings";
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import minifyXML from "minify-xml";
|
import minifyXML from "minify-xml";
|
||||||
@@ -12,16 +13,36 @@ export const GET = (async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const projectCount = 250;
|
const projects = await getAllProjects(settings);
|
||||||
|
const projectCount = projects.length;
|
||||||
const perPage = settings.sitemap.perPage;
|
const perPage = settings.sitemap.perPage;
|
||||||
const pages = Math.ceil(projectCount / perPage);
|
const pages = Math.ceil(projectCount / perPage);
|
||||||
|
|
||||||
let sitemaps: SitemapIndex[] = [];
|
let sitemaps: SitemapIndex[] = [];
|
||||||
|
|
||||||
for (let i = 0; i < pages; i++) {
|
for (let i = 0; i < pages; i++) {
|
||||||
|
const selectedProjects = projects.slice(
|
||||||
|
((Number(i + 1) - 1) * settings.sitemap.perPage),
|
||||||
|
Number(i + 1) * settings.sitemap.perPage - 1
|
||||||
|
);
|
||||||
|
|
||||||
|
let dates = [
|
||||||
|
settings.sitemap.lastModified,
|
||||||
|
settings.project.lastModified,
|
||||||
|
settings.website.lastModified
|
||||||
|
];
|
||||||
|
|
||||||
|
selectedProjects.forEach((project) => {
|
||||||
|
dates.push(project.lastModified);
|
||||||
|
});
|
||||||
|
|
||||||
|
const lastModified = dates.sort((a: Date, b: Date) => {
|
||||||
|
return b.getTime() - a.getTime();
|
||||||
|
});
|
||||||
|
|
||||||
sitemaps.push({
|
sitemaps.push({
|
||||||
url: `/sitemap/projects-${i + 1}.xml`,
|
url: `/sitemap/projects-${i + 1}.xml`,
|
||||||
lastModified: new Date()
|
lastModified: lastModified[0]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
astro/src/types/projects/project.d.ts
vendored
Normal file
12
astro/src/types/projects/project.d.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
type ProjectPost = {
|
||||||
|
title: string;
|
||||||
|
url: string;
|
||||||
|
date: string;
|
||||||
|
content: string;
|
||||||
|
|
||||||
|
tags: Tag[];
|
||||||
|
|
||||||
|
searchEngine: SearchEngine;
|
||||||
|
|
||||||
|
lastModified: Date;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user