当前位置: 首页 > news >正文

nextjs站点地图sitemap添加

app/sitemap.xml/route.ts (主站点地图索引)
sitemap.xml 为文件夹名称

route.ts代码如下:


import { NextResponse } from 'next/server';
import { url } from '@/config/navigation';
export async function GET() {//   const entries = generateMonthlyEntries();const xmlContent = `<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>${url}/sitemap/static.xml</loc><lastmod>${new Date().toISOString()}</lastmod></sitemap><sitemap><loc>${url}/sitemap/clubes.xml</loc><lastmod>${new Date().toISOString()}</lastmod></sitemap><sitemap><loc>${url}/sitemap/galleries.xml</loc><lastmod>${new Date().toISOString()}</lastmod></sitemap><sitemap><loc>${url}/sitemap/articles.xml</loc><lastmod>${new Date().toISOString()}</lastmod></sitemap></sitemapindex>`;return new NextResponse(xmlContent, {headers: {'Content-Type': 'text/xml; charset=utf-8',},});
}

访问 http://localhost/sitemap.xml 得到 下面效果

app\sitemap\static.xml\route.ts (静态站点地图)

代码如下:

import { NextResponse } from 'next/server';
import { url } from '@/config/navigation';let listStatic: { id: number | string; slug: string; updated: Date }[] = [];
listStatic = [{ id: 1, slug: 'latest-news', updated: new Date() }];const staticHtml = ['en/about-us.html','en/contact-us.html','en/cookies-policy.html','en/privacy-policy.html','sobre-nos.html','contate-nos.html','politica-de-cookies.html','politica-de-privacidade.html',
];staticHtml.forEach((html, index) => {listStatic.push({ id: index, slug: html, updated: new Date() });
});export async function GET() {const xmlContent = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${listStatic.map((item) => `<url><loc>${url}/${item.slug}</loc><lastmod>${item.updated.toISOString()}</lastmod><changefreq>monthly</changefreq><priority>0.8</priority></url>`).join('')}</urlset>`;return new NextResponse(xmlContent, {headers: {'Content-Type': 'text/xml; charset=utf-8',},});
}

访问 http://localhost/sitemap/static.xml 得到 下面效果

动态接口请求

app\sitemap\[dynamic]\route.ts

代码如下:

// src/app/sitemap/[dynamic]/route.ts
import { NextResponse } from 'next/server';import { seoApi } from '@/lib/http/api/seo';function convertDate(inputDate: any) {// 创建一个 Date 对象const date = new Date(inputDate);// 获取年、月、日、时、分、秒const year = date.getUTCFullYear();const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // 月份从 0 开始,需要加 1const day = String(date.getUTCDate()).padStart(2, '0');const hours = String(date.getUTCHours()).padStart(2, '0');const minutes = String(date.getUTCMinutes()).padStart(2, '0');const seconds = String(date.getUTCSeconds()).padStart(2, '0');// 获取时区偏移量(以分钟为单位)const offset = date.getTimezoneOffset();const offsetSign = offset > 0 ? '-' : '+';const offsetHours = String(Math.floor(Math.abs(offset) / 60)).padStart(2, '0');const offsetMinutes = String(Math.abs(offset) % 60).padStart(2, '0');// 构造目标格式的日期字符串const formattedDate = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${offsetSign}${offsetHours}:${offsetMinutes}`;return formattedDate;
}export async function GET(request: Request, { params }: { params: { dynamic: string } }) {// 解析动态路径参数const pattern = /sitemap-articles-(\d{4})-(\d{2})\.xml/;const match = params.dynamic.match(pattern);if (!match) {return new NextResponse('Invalid sitemap format', {status: 400,headers: { 'Content-Type': 'text/plain' },});}const [, year, month] = match;let start_date = `${year}-${month}-01`;let end_date = `${year}-${month}-31`;if (month === '02') {end_date = `${year}-${month}-28`; // 假设不是闰年}if (month === '04' || month === '06' || month === '09' || month === '11') {end_date = `${year}-${month}-30`; // 30天的月份}const response = await seoApi.getsitemap({start_date: start_date,end_date: end_date,});// 获取当月数据(示例数据)const articles = response.list;const xmlContent = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${articles.map((article) => `<url><loc>https://example.com/articles/${article.jump_url}</loc><lastmod>${convertDate(article.publish_time)}</lastmod><changefreq>daily</changefreq><priority>0.8</priority></url>`).join('')}</urlset>`;return new NextResponse(xmlContent, {headers: {'Content-Type': 'text/xml; charset=utf-8','Cache-Control': 'public, max-age=86400',},});
}

效果如下

相关文章:

  • 算法题(142):木材加工
  • Music AI Sandbox:打开你的创作新世界
  • java使用MinIO,虚拟机时间异常
  • Typora安装使用教程,图片相对储存路径路径设置
  • 滚珠导轨:电子制造领域精密运动的核心支撑
  • 跨线程和跨进程通信还有多种方式对比
  • 大模型(LLMs)agent
  • C++之“继承”
  • 年龄估计数据集
  • 深入探索 JavaScript 中的模块对象
  • 面试问题(连载。。。。)
  • 最优化方法Python计算:有约束优化应用——线性Lasso回归预测器
  • LeetCode算法题(Go语言实现)_62
  • 使用JAVA对接Deepseek API实现首次访问和提问
  • 【MySQL】第二弹——MySQL表的增删改查(CRUD)
  • 【Linux】module list的用法
  • 流批了,低调使用
  • 使用Hyper-V 安装Windows11操作系统
  • 初学python的我开始Leetcode题8-4
  • 图形移动处理
  • 工程院院士葛世荣获聘任为江西理工大学校长
  • 长期对组织隐瞒真实年龄,广元市城发集团原董事韩治成被双开
  • 新疆维吾尔自治区乌鲁木齐市米东区政协原副主席朱文智被查
  • 九家企业与上海静安集中签约,投资额超10亿元
  • 公示!17个新职业、42个新工种亮相
  • 【社论】以法治力量促进民企长远健康发展