简体   繁体   中英

How to export nextjs static HTML to multiple folders?

I have static nextjs website which have 1 million pages, The problem with that is nextjs export pages in the same out folder which needs lots of ram to access files.

Is there a way to export pages in multiple folders like 100k pages each folder?

This site is only have two pages home page and posts page:

index.js
[postPage].js

In home page I used this code:

export async function getStaticProps() {
  const { db } = await connectToDatabase();

  const postsFeed = await db
    .collection("myCollection")
    .aggregate([{ $sample: { size: 100 } }])
    .toArray();

  return {
    props: {
      postsFeed: JSON.parse(JSON.stringify(postsFeed)),
    },
  };
}

In posts page I used this code:

export async function getStaticPaths() {

  const { db } = await connectToDatabase();
  const posts = await db
    .collection("myCollection")
    .find({})
    .toArray();

  const paths = posts.map((data) => {
    return {
      params: {
        postPage: data.slug.toString(),
      }
    }
  })

  return {
    paths,
    fallback: 'blocking'
  }
}

export async function getStaticProps(context) {

  const postSlug = context.params.postPage;

  const { db } = await connectToDatabase();

  const posts = await db
    .collection("myCollection")
    .find({ slug: { $eq: postsSlug } })
    .toArray();

  const postsFeed = await db
    .collection("myCollection")
    .aggregate([{ $sample: { size: 100 } }])
    .toArray();

  return {
    props: {
      posts: JSON.parse(JSON.stringify(posts)),
      postsFeed: JSON.parse(JSON.stringify(postsFeed)),
    },
  };
}

You can use the next export command to produce a static build (I presume you're already doing this).

To control the output path of files, you can use exportPathMap to remap the routes:

module.exports = {
  exportPathMap: async function (
    defaultPathMap,
    { dev, dir, outDir, distDir, buildId }
  ) {
    return {
      '/': { page: '/' },
      '/postPage': { page: '/new/post/page' },},
    }
  },
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM