同じ機能性を実行する新しい新しいライブラリのため、このライブラリは非推奨としてマークされており、Next-Sitemapライブラリの使用を検討します。
パッケージを維持するのに十分な時間がないので、メンテナーを探しています。
プロジェクトのメンテナンスのために寄付をすることを検討してください。
寄付する
next.jsプロジェクト用のSimple sitemap.xmlマッパー。
パッケージをインストールするには、糸を使用している場合はターミナルでこれを実行します。
yarn add nextjs-sitemap-generator
そして、これはあなたがNPMを使用している場合:
npm i --save-dev nextjs-sitemap-generator
NextJSは、すべての作成されたファイルを提供するために独自のサーバーを起動します。ただし、ファイルを使用して次のサーバーを起動するカスタムサーバーと呼ばれる別のオプションがあります。
このパッケージを使用したい場合は、Severファイルを作成する必要があります。ここでそれを行う方法を見つけることができますnextjsカスタムサーバー
このモジュールは、NextJSのノードカスタムサーバー側で使用されるように作成されています。
index.js/server.jsで使用することを目的としているため、サーバーが初期化されたときに1回しか実行されません。
ノードサーバーのパフォーマンスのリクエストハンドラーのいずれかにそれを配置すると、影響を受ける可能性があります。
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アプリとしてエクスポートする場合は、Base DirectoryにNext-Sitemap-Generator Scriptファイルを作成します。
オプションpagesDirectory 、静的ファイル出力フォルダーを指す必要があります。
出力ファイルを生成した後、 node your_nextjs_sitemap_generator.jsを実行してサイトマップを生成します。
ページが静的に提供されている場合は、ページに拡張機能が含まれるように、 allowFileExtensionsオプションをtrueとして設定する必要があります。ほとんどの場合、 .htmlです。
getStaticPathsでの使用next@^9.4.0使用している場合、動的ルートでページを事前に設定するようにGetStaticPathsを使用してサイトを構成することができます。それらをサイトマップに追加するには、build_idファイルを構成にロードして、静的ページではないすべてのものを除外しながら、staticsページを内部に生成したビルドディレクトリに到達する必要があります。
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!` ) ;
Alternateurls :利用可能な言語に対応する代替ドメインを追加できます。 (オプション)
baseurl :各ページの先頭に使用されるというURL。
IngroreIndexfiles :インデックスファイルがURLにあるか、スラッシュで終了するディレクトリだけであるか(オプション)
無視されたパス:マッピングしないためのファイルまたはディレクトリ(管理ルートのように)。(オプション)
外部アパス:サイトマップに含めるための余分なパスの配列(PagesDirectoryに存在しない場合でも)(オプション)
無視されたテクステン:拡張によってファイルを無視します。(オプション)
PagesDirectory :nextjsページがライブであるディレクトリ。次のJSページである間に別のディレクトリを使用できます。それは絶対的な道でなければなりません。
TargetDirectory :sitemap.xmlが記述されるディレクトリ。
SiteMapFileName :サイトマップのファイル名。デフォルトはsitemap.xmlになります。 (オプション)
PagesConfig :ルートごとの優先度とChangeFreqのオブジェクト構成。受け入れるRegexパターン(オプション)パスキーは小文字である必要があります
SiteMapStylesheet :SiteMapに適用されるスタイルオブジェクトの配列。(オプション)
NextConfigPath (動的ルートに使用): nextConfigPath JSファイルからエクスポートされた場合、コールexportPathMapマップ。
それを行う方法を理解してください(https://nextjs.org/docs/api-reference/next.config.js/exportpathmap)(オプション)
AlowFileExtensions (静的アプリケーションに使用):サイトマップのパスでファイル拡張子が表示されるようにします。次の構成でexporttrailingslashを使用してNextConfigPathを使用している場合、AllowFileExtensionsは無視されます。 (オプション)
今のところ、無視されたパスは、ファイルやディレクトリがあるかどうかを無視して、あなたが置いたものと一致するものと一致します。次のバージョンでは、これが修正されます。