refact sitmap generation

This commit is contained in:
oiov
2025-07-06 11:24:07 +08:00
parent bb957c9ee6
commit dc06fa446e
16 changed files with 98 additions and 2624 deletions

View File

@@ -2,6 +2,8 @@ import { getUserRecords } from "@/lib/dto/cloudflare-dns-record";
import { checkUserStatus } from "@/lib/dto/user";
import { getCurrentUser } from "@/lib/session";
export const dynamic = "force-dynamic";
export async function GET(req: Request) {
try {
const user = checkUserStatus(await getCurrentUser());

View File

@@ -12,7 +12,7 @@ import { getMultipleConfigs } from "@/lib/dto/system-config";
import { checkUserStatus } from "@/lib/dto/user";
import { CloudStorageCredentials, createS3Client, getFileInfo } from "@/lib/r2";
import { getCurrentUser } from "@/lib/session";
import { extractFileNameAndExtension } from "@/lib/utils";
import { extractFileNameAndExtension, generateFileKey } from "@/lib/utils";
export async function POST(request: Request): Promise<Response> {
const user = checkUserStatus(await getCurrentUser());
@@ -220,16 +220,3 @@ async function uploadPart(formData: FormData, R2: S3Client): Promise<Response> {
});
}
}
export function generateFileKey(fileName: string, prefix?: string): string {
if (prefix) {
return `${prefix}/${fileName}`;
}
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}/${month}/${day}/${fileName}`;
}

View File

@@ -1,10 +1,12 @@
import { MetadataRoute } from "next"
import { MetadataRoute } from "next";
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: "*",
allow: "/",
disallow: "/admin/*",
},
}
sitemap: process.env.NEXT_PUBLIC_APP_URL + "/sitemap.xml",
};
}

76
app/sitemap.ts Normal file
View File

@@ -0,0 +1,76 @@
import { MetadataRoute } from "next";
import { allDocs, allPages } from "contentlayer/generated";
async function getDocumentSlugs() {
return allDocs.map((doc) => ({
slug: doc.slugAsParams,
}));
}
async function getStaticPageSlugs() {
return allPages.map((page) => ({
slug: page.slugAsParams,
}));
}
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "https://wr.do";
const currentDate = new Date();
// static
const staticPages: MetadataRoute.Sitemap = [
{
url: baseUrl,
lastModified: currentDate,
changeFrequency: "daily",
priority: 1.0,
},
{
url: `${baseUrl}/login`,
lastModified: currentDate,
changeFrequency: "monthly",
priority: 0.8,
},
{
url: `${baseUrl}/feedback`,
lastModified: currentDate,
changeFrequency: "monthly",
priority: 0.8,
},
];
// (docs)/[slug]
const documentSlugs = await getDocumentSlugs();
const documentPages: MetadataRoute.Sitemap = documentSlugs.map((slug) => ({
url: `${baseUrl}/docs/${slug.slug}`,
lastModified: currentDate,
changeFrequency: "weekly" as const,
priority: 0.7,
}));
// (marketing)/[slug]
const marketingPageSlugs = await getStaticPageSlugs();
const marketingPages: MetadataRoute.Sitemap = marketingPageSlugs.map(
(slug) => ({
url: `${baseUrl}/${slug.slug}`,
lastModified: currentDate,
changeFrequency: "weekly" as const,
priority: 0.7,
}),
);
const protectedPages: MetadataRoute.Sitemap = [
{
url: `${baseUrl}/dashboard`,
lastModified: currentDate,
changeFrequency: "daily",
priority: 0.7,
},
];
return [
...staticPages,
...documentPages,
...marketingPages,
...protectedPages,
];
}

View File

@@ -510,3 +510,16 @@ export function extractFileNameAndExtension(filePath: string): {
nameWithoutExtension: fileName.substring(0, lastDotIndex),
};
}
export function generateFileKey(fileName: string, prefix?: string): string {
if (prefix) {
return `${prefix}/${fileName}`;
}
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}/${month}/${day}/${fileName}`;
}

View File

@@ -1,5 +0,0 @@
module.exports = {
siteUrl: "https://wr.do",
generateRobotsTxt: true, // (optional)
sitemapSize: 7000, // Number of URLs per sitemap file
};

View File

@@ -8,7 +8,6 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"postbuild": "next-sitemap",
"turbo": "next dev --turbo",
"start": "next start",
"start-docker": "npm-run-all check-db start-server",
@@ -102,7 +101,6 @@
"next-contentlayer2": "^0.5.0",
"next-intl": "^4.1.0",
"next-pwa": "^5.6.0",
"next-sitemap": "^4.2.3",
"next-themes": "^0.3.0",
"next-view-transitions": "^0.3.0",
"nodemailer": "^6.9.14",

23
pnpm-lock.yaml generated
View File

@@ -239,9 +239,6 @@ importers:
next-pwa:
specifier: ^5.6.0
version: 5.6.0(@babel/core@7.24.5)(esbuild@0.19.11)(next@14.2.28(@babel/core@7.24.5)(@opentelemetry/api@1.8.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1))(webpack@5.90.3(@swc/core@1.3.101(@swc/helpers@0.5.5))(esbuild@0.19.11))
next-sitemap:
specifier: ^4.2.3
version: 4.2.3(next@14.2.28(@babel/core@7.24.5)(@opentelemetry/api@1.8.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1))
next-themes:
specifier: ^0.3.0
version: 0.3.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
@@ -1345,9 +1342,6 @@ packages:
'@effect-ts/otel-node':
optional: true
'@corex/deepmerge@4.0.43':
resolution: {integrity: sha512-N8uEMrMPL0cu/bdboEWpQYb/0i2K5Qn8eCsxzOmxSggJbbQte7ljMRoXm917AbntqTGOzdTu+vP3KOOzoC70HQ==}
'@dimforge/rapier3d-compat@0.12.0':
resolution: {integrity: sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow==}
@@ -6495,13 +6489,6 @@ packages:
peerDependencies:
next: '>=9.0.0'
next-sitemap@4.2.3:
resolution: {integrity: sha512-vjdCxeDuWDzldhCnyFCQipw5bfpl4HmZA7uoo3GAaYGjGgfL4Cxb1CiztPuWGmS+auYs7/8OekRS8C2cjdAsjQ==}
engines: {node: '>=14.18'}
hasBin: true
peerDependencies:
next: '*'
next-themes@0.3.0:
resolution: {integrity: sha512-/QHIrsYpd6Kfk7xakK4svpDI5mmXP0gfvCoJdGpZQ2TOrQZmsW0QxjaiLn8wbIKjtm4BTSqLoix4lxYYOnLJ/w==}
peerDependencies:
@@ -9863,8 +9850,6 @@ snapshots:
ts-pattern: 5.1.1
type-fest: 4.18.1
'@corex/deepmerge@4.0.43': {}
'@dimforge/rapier3d-compat@0.12.0': {}
'@effect-ts/core@0.60.5':
@@ -16020,14 +16005,6 @@ snapshots:
- uglify-js
- webpack
next-sitemap@4.2.3(next@14.2.28(@babel/core@7.24.5)(@opentelemetry/api@1.8.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)):
dependencies:
'@corex/deepmerge': 4.0.43
'@next/env': 13.5.11
fast-glob: 3.3.2
minimist: 1.2.8
next: 14.2.28(@babel/core@7.24.5)(@opentelemetry/api@1.8.0)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
next-themes@0.3.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1):
dependencies:
react: 18.3.1

View File

@@ -1,9 +0,0 @@
# *
User-agent: *
Allow: /
# Host
Host: https://wr.do
# Sitemaps
Sitemap: https://wr.do/sitemap.xml

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url><loc>https://wr.do/robots.txt</loc><lastmod>2025-07-01T11:16:19.769Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
<url><loc>https://wr.do/manifest.json</loc><lastmod>2025-07-01T11:16:19.769Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
<url><loc>https://wr.do/opengraph-image.jpg</loc><lastmod>2025-07-01T11:16:19.769Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
</urlset>

View File

@@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>https://wr.do/sitemap-0.xml</loc></sitemap>
</sitemapindex>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long