next.js - Nextjs 导出清除“out”文件夹
问题描述
我正在使用 nextjs 和这个例子https://github.com/zeit/next.js/tree/master/examples/with-static-export
在 next.config.js 我有代码:
module.exports = {
async exportPathMap(defaultPathMap, { dev, dir, outDir, distDir, buildId, incremental }) {
// we fetch our list of posts, this allow us to dynamically generate the exported pages
const response = await fetch(
'https://jsonplaceholder.typicode.com/posts?_limit=3'
)
const postList = await response.json()
// tranform the list of posts into a map of pages with the pathname `/post/:id`
const pages = postList.reduce(
(pages, post) =>
Object.assign({}, pages, {
[`/post/${post.id}`]: { page: '/post/[id]' },
}),
{}
)
// combine the map of post pages with the home
return Object.assign({}, pages, {
'/': { page: '/' },
})
},
}
它获取 3 个帖子并生成文件 - [id].html - 太棒了!
但是现在我只需要为这个新帖子获取新帖子并构建页面,但是 commad next export会从 out 中删除所有文件并只创建一个帖子。
我需要做些什么来保留旧帖子并在下次导出时添加新帖子?
例子:
- 第一个下一个导出请求来自 api 的 3 个帖子
- 在文件夹“out”中生成 3 个帖子
- 更改 api url 并为 1 个新帖子运行下一个导出
- 摘要我的“out”目录中有 3 个旧帖子页面和 1 个新页面
怎么做?
解决方案
Next 无法立即执行此操作,但您可以设置它来执行此操作。首先,您需要一个已构建页面的系统(数据库)。其次,您需要某种与该数据库 (api) 通信的方法来询问应该构建哪些页面(例如,发送一个页面列表,然后 api 会响应告诉您哪些页面尚未构建)。然后,告诉您exportPathMap
要构建哪些页面。最后,将您构建的页面移出out
并移入新的 final/public 目录。
默认情况下,Next 将构建/导出pages
目录中的任何内容以及您在 中设置的任何内容exportPathMap
,并将所有这些内容放在out
目录中。您可以通过传递 custom来覆盖它构建的内容exportPathMap
,以及如何处理进入out
目录的内容取决于您,因此您可以将这些文件移动到不同的实际公共目录并将它们与旧文件合并。
推荐阅读
- scala - 带有逗号分隔的其他列的 Spark DataFrame 列,这些列需要使用另一列中给出的值进行更新
- javascript - Puppeteer 和 react-router (BrowserRouter)
- python - GCP Python 在存储桶之间复制大文件
- android-studio - 在 AS 3.6.1 中移动资源管理器选项卡
- php - 如何在 smart3 中解码和打印 json
- sql - 固定标头到 oracle sql 查询
- arrays - 谷歌表格数组公式拆分和导入范围
- image - 如何从 Qt 应用程序与 wirh ROS 通信以传输视频和 CAN 消息
- c++ - 为什么在这段代码中没有调用复制构造函数?
- java - spring-data JpaRepository save() 方法副作用