简体   繁体   English

NextJS - 使用 static 导出一次构建多个项目?

[英]NextJS - build multiple projects at once with static export?

I have a small Next.js project, all the data comes from multiple API endpoints that look like this:我有一个小型 Next.js 项目,所有数据来自多个 API 端点,如下所示:

https://enpoint.com/some-query/project1

The API is projected in a way that it could reply with different data for different sites: API 的投影方式可以为不同的站点回复不同的数据:

https://enpoint.com/some-query/project1
https://enpoint.com/some-query/project2
https://enpoint.com/some-query/project3

Right now I'm doing next build && next export as all I need is a static export.现在我正在做next build && next export ,因为我只需要一个 static 导出。 Once I run this command I'm getting /out/project1 with all the files necessary to run the site.运行此命令后,我将获得/out/project1以及运行该站点所需的所有文件。

My question is - what's the preferred way of altering Next.js build process so during next build && next export it will run API calls to https://enpoint.com/some-query/project1 , build out/project1 and then repeat the same steps for project2 and project3 ?我的问题是-更改 Next.js 构建过程的首选方法是什么,因此next build && next export期间它将运行 API 调用https://enpoint.com/some-query/project1 out/project1对于project2project3

Once I build my project I want to end up with:一旦我建立了我的项目,我想最终得到:

/out/project1
/out/project2
/out/project3

Any ideas?有任何想法吗? I've been looking in the docs but with no luck.我一直在寻找文档,但没有运气。

The most straightforward solution I know is to run the build process multiple times with different environmental variables.我知道的最直接的解决方案是使用不同的环境变量多次运行构建过程。

Setup build commands设置构建命令

"build": "npm-run-all --parallel build:project1 build:project2 build:project3",
"build:project1": "PROJECT_ENV=project1 next build",
"build:project2": "PROJECT_ENV=project2 next build",
"build:project2": "PROJECT_ENV=project3 next build",

Call different API endpoints based on the env variables根据环境变量调用不同的 API 端点

export async function getStaticProps(context) {
  switch(process.env.PROJECT_ENV) {
    case 'project1': 
      // fetch project 1 data
    case 'project2': 
      // fetch project 2 data
    case 'project3': 
      // fetch project 3 data
  }
  return {
    props: {...}
  }
}

This might not be the best answer but I hope it helps.这可能不是最好的答案,但我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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