如何在Vercel上扫描生产目录(使用Next.js)



我开发了一个JS脚本,当通过URL(例如:mydomain.com/sitemap.xml(访问时,它可以作为sitemap.xml使用,该URL将列出我项目的页面。当在开发(yarn dev(上运行时,它工作得很好,当它在构建并在我的pc上启动(yarn build&yarn start(后运行时,也可以工作。问题是在Vercel上它不起作用,错误是:

[GET] /sitemap.xml
19:39:16:85
Function Status:
None
Edge Status:
500
Duration:
146.30 ms
Init Duration:
446.50 ms
Memory Used:
80 MB
info  - Loaded env from /var/task/.env.production
info  - Loaded env from /var/task/.env
2021-11-18T19:39:17.673Z    68ecb71f-eede-4cbd-81a9-5c677f11e556    ERROR   Error: ENOENT: no such file or directory, scandir '/var/task/.next/server/pages/projects'
at Object.readdirSync (fs.js:1047:3)
at getServerSideProps (/var/task/.next/server/pages/sitemap.xml.js:38:56)
at Object.renderToHTML (/var/task/node_modules/next/dist/server/render.js:504:26)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async doRender (/var/task/node_modules/next/dist/server/next-server.js:1428:38)
at async /var/task/node_modules/next/dist/server/next-server.js:1523:28
at async /var/task/node_modules/next/dist/server/response-cache.js:63:36 {
errno: -2,
syscall: 'scandir',
path: '/var/task/.next/server/pages/projects',
page: '/sitemap.xml'
}
RequestId: 68ecb71f-eede-4cbd-81a9-5c677f11e556 Error: Runtime exited with error: exit status 1
Runtime.ExitError

我的sitemap.xml.js上完成这项工作的代码是:

const staticPagesProjects = fs
.readdirSync({
development: 'src/pages/projects',
production: __dirname + '/projects',
}[process.env.NODE_ENV])
.filter((staticPagesProjects) => {
return regex.test(staticPagesProjects) && (staticPagesProjects != "index.html") && (staticPagesProjects != "404.html") && (staticPagesProjects != "500.html");
})
.map((staticPagePath) => {
return `${baseUrl}/projects/${staticPagePath.replace(".html", "")}`;
});

有人能告诉我这个路径出了什么问题吗?为什么它在我的电脑上有效,但在Vercel上无效?非常感谢。

我遇到了同样的问题,不幸的是我也没有找到解决方案,但我找到了一种可以帮助的方法:

对于静态页面,我将它们手动添加到一个数组中。

对于动态页面,它通过连接到DB并获取数组中的页面来工作。

既然已经一个月了,你找到解决方案了吗?

我通过添加来解决它

path.resolve(process.cwd(), "your folder")

这意味着你将找出你的源的当前路径,而不是构建的源

最新更新