由於新的新興庫具有相同的功能性,該庫被標記為棄用,並考慮使用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與您放置的東西匹配的任何坐標,如果有文件或目錄,則忽略了。在接下來的版本中,這將被修復。