Xml 如何使用custom express server和next.js构建动态站点地图?
我在跟踪 使用next.js构建动态站点地图。我有一个action文件夹,在其中我从自定义API中提取了所有操作。在本文中,他将graphql用于动态URL,但我没有使用graphql,因此在下面显示的代码中,我从操作中导入了list函数,我从数据库中获取了所有博客。现在我无法提取这些动态URL。我肯定是列表功能出了问题。告诉我一种方法,以便我可以从列表中获取动态URL pages/sitemap.xml.jsXml 如何使用custom express server和next.js构建动态站点地图?,xml,express,next.js,sitemap,Xml,Express,Next.js,Sitemap,我在跟踪 使用next.js构建动态站点地图。我有一个action文件夹,在其中我从自定义API中提取了所有操作。在本文中,他将graphql用于动态URL,但我没有使用graphql,因此在下面显示的代码中,我从操作中导入了list函数,我从数据库中获取了所有博客。现在我无法提取这些动态URL。我肯定是列表功能出了问题。告诉我一种方法,以便我可以从列表中获取动态URL pages/sitemap.xml.js import React,{useffect,useState}来自“React”;
import React,{useffect,useState}来自“React”;
从“fs”导入fs;
从“../actions/job”导入{list}
const Sitemap=()=>{};
export const getServerSideProps=async({res})=>{
常量baseUrl=http://localhost:3000'
常量staticPages=fs
.readdirSync(“页面”)
.filter((静态页面)=>{
回来[
“_app.js”,
“_document.js”,
“_error.js”,
“sitemap.xml.js”,
]。包括(第页);
})
.map((staticPagePath)=>{
返回`${baseUrl}/${staticPagePath}`;
});
常数站点地图=`
${staticPages
.map((url)=>{
返回`
${url}
${new Date().toISOString()}
月刊
1
`;
})
.join(“”)
//从这里开始动态url功能,当前显示错误列表。映射不是一个函数
${列表
.map((作业)=>{
返回`
${baseUrl}/jobs/${job.slug}
${job.updatedAt}
月刊
1
`;
})
.join(“”)
`;
res.setHeader(“内容类型”、“文本/xml”);
res.write(站点地图);
res.end();
返回{
道具:{},
};
};
导出默认站点地图;
import React ,{useEffect,useState} from "react";
import fs from "fs";
import {list} from '../actions/job'
const Sitemap = () => {};
export const getServerSideProps = async ({ res }) => {
const baseUrl = 'http://localhost:3000'
const staticPages = fs
.readdirSync("pages")
.filter((staticPage) => {
return ![
"_app.js",
"_document.js",
"_error.js",
"sitemap.xml.js",
].includes(staticPage);
})
.map((staticPagePath) => {
return `${baseUrl}/${staticPagePath}`;
});
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${staticPages
.map((url) => {
return `
<url>
<loc>${url}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
//From here the dynamic url functionality starts,Currently it is showing the error list.map is not a function
${list
.map((job) => {
return `
<url>
<loc>${baseUrl}/jobs/${job.slug}</loc>
<lastmod>${job.updatedAt}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
</urlset>
`;
res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();
return {
props: {},
};
};
export default Sitemap;