簡體   English   中英

如何在 next.js 中創建站點地圖

[英]How can I create a sitemap in next.js

我正在嘗試將站點地圖添加到我的 next.js 網站。 我將以下代碼添加到server.js

server.get('/sitemap.xml',  (req, res) => {
    const options = {
        root:'./',
        headers: {
          'Content-Type': 'text/plain;charset=UTF-8',
        }
      };

    return res.status(200).sendFile('sitemap.xml', options)
});

sitemap.xml文件位於根文件夾,即與server.js文件相同的位置。 當我使用node server在本地運行它時,go 到http://localhost:5000/sitemap.xml它工作正常。 當我將文件上傳到服務器時,我從服務器收到一個not found的錯誤。

我做錯了什么,如何使用 nextjs 添加站點地圖?

大概就是把public/sitemap.xml等公用文件夾下的sitemap.xml文件移走,然后刪除這個自定義的動態路由。

Next.js static 文件服務文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM