由于新的新兴库具有相同的功能性,该库被标记为弃用,并考虑使用NextSitemap库。
我们正在寻找维护人员,因为我没有足够的时间来维护包裹。
请考虑为维护项目捐款。
捐
Simple sitemap.xml映射器Next.js Projects。
如果您使用的是纱线,则安装软件包在终端中执行此操作:
yarn add nextjs-sitemap-generator
如果您使用的是NPM:
npm i --save-dev nextjs-sitemap-generator
NextJS启动其自己的服务器,以服务所有创建的文件。但是还有另一个名为“自定义服务器”的选项,该选项使用文件启动下一个服务器。
如果您想使用此软件包,则必须创建SEVE文件。您可以在此处找到如何在此处找到nextjs自定义服务器
该模块已被创建为在NextJS的节点自定义服务器端使用。
它应在index.js/server.js中使用,以便在初始化服务器时只能运行一次。
如果将其放置在节点服务器性能的任何请求处理程序中,可能会受到影响。
对于那些在Vercel中部署的人:
可以在Vercel上部署自定义服务器,而是为next.js提供了。
例如:
如果您有此示例服务器文件
// server.js
const sitemap = require ( 'nextjs-sitemap-generator' ) ; // Import the package
const { createServer } = require ( 'http' )
const { parse } = require ( 'url' )
const next = require ( 'next' )
const dev = process . env . NODE_ENV !== 'production'
const app = next ( { dev } )
const handle = app . getRequestHandler ( )
/*
Here you is you have to use the sitemap function.
Using it here you are allowing to generate the sitemap file
only once, just when the server starts.
*/
sitemap ( {
alternateUrls : {
en : 'https://example.en' ,
es : 'https://example.es' ,
ja : 'https://example.jp' ,
fr : 'https://example.fr' ,
} ,
baseUrl : 'https://example.com' ,
ignoredPaths : [ 'admin' ] ,
extraPaths : [ '/extraPath' ] ,
pagesDirectory : __dirname + "\pages" ,
targetDirectory : 'static/' ,
sitemapFilename : 'sitemap.xml' ,
nextConfigPath : __dirname + "\next.config.js"
} ) ;
app . prepare ( ) . then ( ( ) => {
createServer ( ( req , res ) => {
const parsedUrl = parse ( req . url , true )
const { pathname , query } = parsedUrl
if ( pathname === '/a' ) {
app . render ( req , res , '/a' , query )
}
else if ( pathname === '/b' ) {
app . render ( req , res , '/b' , query )
} else {
handle ( req , res , parsedUrl )
} } ) . listen ( 3000 , ( err ) => {
if ( err ) throw err
console . log ( '> Ready on http://localhost:3000' )
} )
} ) 如果将下一个项目导出为静态HTML应用程序,请在基本目录中创建一个sideMap-gainer脚本文件。
选项pagesDirectory应指向静态文件输出文件夹。
生成输出文件后,运行node your_nextjs_sitemap_generator.js生成站点地图。
如果您的页面是静态提供的,则需要将allowFileExtensions选项设置为true ,以便页面包含扩展名,大多数情况为.html 。
getStaticPaths如果您使用的是next@^9.4.0 ,则可能将网站配置为GetStatic Pathers,以在动态路线上预生化页面。要将它们添加到您的站点地图中,您需要将build_id文件加载到配置中,以到达内部带有静态页面的生成的构建目录,同时排除所有不是静态页面的所有内容:
const sitemap = require ( "nextjs-sitemap-generator" ) ;
const fs = require ( "fs" ) ;
const BUILD_ID = fs . readFileSync ( ".next/BUILD_ID" ) . toString ( ) ;
sitemap ( {
baseUrl : "https://example.com" ,
// If you are using Vercel platform to deploy change the route to /.next/serverless/pages
pagesDirectory : __dirname + "/.next/server/static/" + BUILD_ID + "/pages" ,
targetDirectory : "public/" ,
ignoredExtensions : [ "js" , "map" ] ,
ignoredPaths : [ "assets" ] , // Exclude everything that isn't static page
} ) ; // your_nextjs_sitemap_generator.js
const sitemap = require ( "nextjs-sitemap-generator" ) ;
sitemap ( {
alternateUrls : {
en : "https://example.en" ,
es : "https://example.es" ,
ja : "https://example.jp" ,
fr : "https://example.fr" ,
} ,
baseUrl : "https://example.com" ,
ignoredPaths : [ "admin" ] ,
extraPaths : [ "/extraPath" ] ,
pagesDirectory : __dirname + "\pages" ,
targetDirectory : "static/" ,
sitemapFilename : "sitemap.xml" ,
nextConfigPath : __dirname + "\next.config.js" ,
ignoredExtensions : [ "png" , "jpg" ] ,
pagesConfig : {
"/login" : {
priority : "0.5" ,
changefreq : "daily" ,
} ,
} ,
sitemapStylesheet : [
{
type : "text/css" ,
styleFile : "/test/styles.css" ,
} ,
{
type : "text/xsl" ,
styleFile : "test/test/styles.xls" ,
} ,
] ,
} ) ;
console . log ( `✅ sitemap.xml generated!` ) ;
替代功能:您可以添加与可用语言相对应的替代域。 (选修的)
baseurl :将在每页开头使用的URL。
iNAREINDEXFILES :索引文件是否应在URL中或仅以斜杠结尾的目录(可选)
忽略路径:不映射的文件或目录(例如管理路线)。(可选)
Exterapths :额外的路径数组,要包括在站点地图中(即使在PagesDirectory中不存在)(可选)(可选)
忽略续文:通过扩展忽略文件。(可选)
PagesDirectory :NextJS页面寿命的目录。您可以使用另一个目录是NextJS页面。它必须成为绝对路径。
TargetDirectory :SiteMap.xml将要编写的目录。
SiteMapFilename :SiteMap的文件名。默认为sitemap.xml 。 (选修的)
PAGESCONFIG :每个路由的优先级和ChangeFreq的对象配置。接受正则表达式(可选)路径键必须是小写
SiteMapStyleSheet :将应用于站点地图的样式对象数组。(可选)
NextConfigPath (用于动态路由):如果从nextConfigPath JS文件导出,请致电exportPathMap 。
请参阅此信息以了解如何做(https://nextjs.org/docs/api-reference/next.config.js/exportpathmap)(可选)
允许fileextensions (用于静态应用程序):确保在SiteMap中的路径显示文件扩展名。如果您在下一个配置中使用extrailtrailingslash使用NextConfigPath,则允许fileextensions将被忽略。 (选修的)
目前,忽略Paths与您放置的东西匹配的任何坐标,如果有文件或目录,则忽略了。在接下来的版本中,这将被修复。