Next.js如何忽略应用中的整个文件夹?

huangapple go评论77阅读模式
英文:

Next.js how to ignore a complete folder in app?

问题

在下一个13版本中,当nextConfig.output为"export"时,在app/api文件夹中创建构建时会出现错误。

在我的项目中,根据环境变量需要不同的构建类型。

是否有办法在"output"为"export"时忽略构建中的"api"文件夹?

当我使用nextConfig.output为"export"运行构建时,我会得到以下错误:

在以下路径上出现导出错误:
/api/revalidate/route: /api/revalidate

src/app/api/revalidate/route.ts 文件

import { NextRequest, NextResponse } from 'next/server';
import { revalidateTag } from 'next/cache';

export async function GET(request: NextRequest) {
  const tag = request.nextUrl.searchParams.get('tag');
  if(tag){
    revalidateTag(tag);
  }
  return NextResponse.json({ revalidated: true, now: Date.now() });
}

Next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: process.env.NEXT_OUTPUT_MODE,
};

module.exports = nextConfig;

可复现的存储库

这是一个可复现此错误的存储库
https://github.com/zeckaissue/next-export-api-crash

英文:

On next 13, app/api folder creates an error during build when nextConfig.output is "export".

In my project, I need different build type depending on environnement variable.

Any way to ignore "api" folder during build when "output" is "export" ?

When I run build with nextConfig.output as "export" I got following error:

> Export encountered errors on following paths:
/api/revalidate/route: /api/revalidate

src/app/api/revalidate/route.ts file

import { NextRequest, NextResponse } from 'next/server';
import { revalidateTag } from 'next/cache';
 
export async function GET(request: NextRequest) {
  const tag = request.nextUrl.searchParams.get('tag');
  if(tag){
    revalidateTag(tag);
  }
  return NextResponse.json({ revalidated: true, now: Date.now() });
}

Next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: process.env.NEXT_OUTPUT_MODE,
};

module.exports = nextConfig;

Reproducible repository

Here is a repository to reproduce this error
https://github.com/zeckaissue/next-export-api-crash

答案1

得分: 3

我找到了一个解决方法,使用ignore-loader。但也许有一种更好的方法可以通过next.js的内置功能来实现我的目标。

这是我的更新后的next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: process.env.NEXT_OUTPUT_MODE,
  /**
   *
   * @param {import('webpack').Configuration} config
   * @param {import('next/dist/server/config-shared').WebpackConfigContext} context
   * @returns {import('webpack').Configuration}
   */
  webpack: (config) => {
    if (process.env.NEXT_OUTPUT_MODE !== "export" || !config.module) {
      return config;
    }
    config.module.rules?.push({
      test: /src\/app\/api/,
      loader: "ignore-loader",
    });
    return config;
  },
};

module.exports = nextConfig;
英文:

I found a workaround with ignore-loader. But maybe there is a better way to achieve my goal with a built-in feature of next.js

Here is my updated next.config.js

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: process.env.NEXT_OUTPUT_MODE,
  /**
   *
   * @param {import('webpack').Configuration} config
   * @param {import('next/dist/server/config-shared').WebpackConfigContext} context
   * @returns {import('webpack').Configuration}
   */
  webpack: (config) => {
    if (process.env.NEXT_OUTPUT_MODE !== "export" || !config.module) {
      return config;
    }
    config.module.rules?.push({
      test: /src\/app\/api/,
      loader: "ignore-loader",
    });
    return config;
  },
};

module.exports = nextConfig;

答案2

得分: 0

你可以在Next.js配置文件(next.config.js)中使用ignore选项。
如果你还没有创建配置文件,你需要创建一个。
打开next.config.js文件并添加以下代码:

module.exports = {
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.module.rules.push({
        test: /YOUR FOLDER NAME\/.*/,
        loader: 'ignore-loader',
      });
    }
  }
}
英文:

You can use the ignore option in the Next.js configuration file (next.config.js).
You will have to create one config file if you havent already.
Open the next.config.js file and add the following code:

module.exports = {
  webpack: (config, { isServer }) => {
    if (!isServer) {
      config.module.rules.push({
        test: /YOUR FOLDER NAME\/.*/,
        loader: 'ignore-loader',
      });
}

答案3

得分: -5

这是我的答案。

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: process.env.NEXT_OUTPUT_MODE,
  exclude: [/node_modules/, /api/], // 将此行添加以排除 api 文件夹
};

module.exports = nextConfig;
英文:

Here is my answer.

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: process.env.NEXT_OUTPUT_MODE,
  exclude: [/node_modules/, /api/], // Add this line to exclude the api folder
};

module.exports = nextConfig;

huangapple
  • 本文由 发表于 2023年6月5日 14:25:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定