首页 > 解决方案 > 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 中删除所有文件并只创建一个帖子。

我需要做些什么来保留旧帖子并在下次导出时添加新帖子?

例子:

怎么做?

标签: next.js

解决方案


Next 无法立即执行此操作,但您可以设置它来执行此操作。首先,您需要一个已构建页面的系统(数据库)。其次,您需要某种与该数据库 (api) 通信的方法来询问应该构建哪些页面(例如,发送一个页面列表,然后 api 会响应告诉您哪些页面尚未构建)。然后,告诉您exportPathMap要构建哪些页面。最后,将您构建的页面移出out并移入新的 final/public 目录。

默认情况下,Next 将构建/导出pages目录中的任何内容以及您在 中设置的任何内容exportPathMap,并将所有这些内容放在out目录中。您可以通过传递 custom来覆盖它构建的内容exportPathMap,以及如何处理进入out目录的内容取决于您,因此您可以将这些文件移动到不同的实际公共目录并将它们与旧文件合并。


推荐阅读