下一个身份验证 API 路由处理程序超过大小限制。

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

next auth api route handler exceeds size limit

问题

如何减小 Vercel 无服务器函数的捆绑大小?

在 nx monorepo 中 Messing around with next-auth 和 nextjs 应用程序目录,当部署带有电子邮件提供程序身份验证的应用程序时,我遇到了一个问题。出于某种原因,依赖项的大小会急剧增加并超出限制。

从 Vercel 仪表板的日志中看到的情况如下:

无服务器函数的页面:api/auth/[...nextauth].js
大型依赖项 未压缩大小 压缩大小
node_modules/@swc/core-linux-x64-gnu 49.75 MB 16.52 MB
node_modules/@swc/core-linux-x64-musl 49.36 MB 16.39 MB
node_modules/.prisma/client 16.25 MB 7.23 MB
node_modules/next/dist 19.67 MB 4.87 MB
node_modules/@esbuild/linux-x64 8.68 MB 3.64 MB
node_modules/webpack/lib 3.31 MB 871.18 KB
node_modules/sass/sass.dart.js 4.27 MB 669.82 KB
node_modules/react-dom/cjs 1.64 MB 404.02 KB
node_modules/caniuse-lite/data 882.5 KB 316.5 KB
dist/apps/manager-dashboard 1.48 MB 253.23 KB
node_modules/terser/dist 957.25 KB 183.23 KB
node_modules/uglify-js/lib 972.25 KB 166.19 KB
node_modules/webpack/schemas 606.81 KB 93.78 KB
所有依赖项 161.46 MB 52.47 MB

我尝试使用 vercel.json 来排除最大的依赖项,但没有成功。

在 nextauth 中的代码基本上遵循以下文档:

apps/my-app/app/api/auth/[...nextauth]/route.ts

import NextAuth from 'next-auth';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { PrismaClient } from '@prisma/client';
import EmailProvider from 'next-auth/providers/email';

const emailProvider = EmailProvider({
  from: 'no-reply@exanubes.com',
  type: 'email',
  server: {
    host: process.env.EMAIL_SERVER_HOST,
    port: process.env.EMAIL_SERVER_PORT,
    maxAge: 24 * 60 * 60, // 24 hours
    auth: {
      user: process.env.EMAIL_SERVER_USER,
      pass: process.env.EMAIL_SERVER_PASSWORD,
    },
  },
});

const prisma = new PrismaClient();

const handler = NextAuth({
  secret: process.env.NEXTAUTH_SECRET,
  session: {
    strategy: 'jwt',
  },
  adapter: PrismaAdapter(prisma),
  providers: [emailProvider],
});

export { handler as GET, handler as POST };
英文:

How do I reduce the bundle size of a vercel serverless function?

Been messing around with next-auth and nextjs app dir inside nx monorepo and I've hit a wall when it comes to deploying the app with email provider auth. For some reason the dependencies get blown up in size and exceed the limit.

// logs from vercel dashboard
Serverless Function's page: api/auth/[...nextauth].js
Large Dependencies                     Uncompressed size  Compressed size
node_modules/@swc/core-linux-x64-gnu            49.75 MB         16.52 MB
node_modules/@swc/core-linux-x64-musl           49.36 MB         16.39 MB
node_modules/.prisma/client                     16.25 MB          7.23 MB
node_modules/next/dist                          19.67 MB          4.87 MB
node_modules/@esbuild/linux-x64                  8.68 MB          3.64 MB
node_modules/webpack/lib                         3.31 MB        871.18 KB
node_modules/sass/sass.dart.js                   4.27 MB        669.82 KB
node_modules/react-dom/cjs                       1.64 MB        404.02 KB
node_modules/caniuse-lite/data                  882.5 KB         316.5 KB
dist/apps/manager-dashboard                      1.48 MB        253.23 KB
node_modules/terser/dist                       957.25 KB        183.23 KB
node_modules/uglify-js/lib                     972.25 KB        166.19 KB
node_modules/webpack/schemas                   606.81 KB         93.78 KB
All dependencies                               161.46 MB         52.47 MB

I've tried using vercel.json to exclude the biggest offenders but it didn't work.

// apps/my-app/vercel.json
{
  "functions": {
    "app/api/auth/[...nextauth].js": {
      "excludeFiles": "**/node_modules/{@swc,@esbuild}/**"
    }
  }
}

The code in nextauth is basically following the documentation:

// apps/my-app/app/api/auth/[...nextauth]/route.ts

import NextAuth from 'next-auth';
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { PrismaClient } from '@prisma/client';
import EmailProvider from 'next-auth/providers/email';

const emailProvider = EmailProvider({
  from: 'no-reply@exanubes.com',
  type: 'email',
  server: {
    host: process.env.EMAIL_SERVER_HOST,
    port: process.env.EMAIL_SERVER_PORT,
    maxAge: 24 * 60 * 60, // 24 hours
    auth: {
      user: process.env.EMAIL_SERVER_USER,
      pass: process.env.EMAIL_SERVER_PASSWORD,
    },
  },
});

const prisma = new PrismaClient();

const handler = NextAuth({
  secret: process.env.NEXTAUTH_SECRET,
  session: {
    strategy: 'jwt',
  },
  adapter: PrismaAdapter(prisma),
  providers: [emailProvider],
});

export { handler as GET, handler as POST };

答案1

得分: 0

我通过删除锁定文件并将所有依赖项移至devDependencies成功解决了这个问题。有趣的是,在vercel部署日志中,它注意到了缺少swc linux依赖项,但编译时没有任何问题。

- info 正在下载WASM swc包...
- warn 发现锁定文件缺少swc依赖项,请在本地运行next以自动修复
- info 使用next-swc的WASM构建
- warn 尝试加载@next/swc-linux-x64-gnu,但未安装
- warn 尝试加载@next/swc-linux-x64-gnux32,但未安装
- warn 尝试加载@next/swc-linux-x64-musl,但未安装
英文:

I was able to fix this issue by deleting the lock file and moving all my dependencies to devDependencies. Interestingly, in vercel deployment logs, it noticed swc linux deps missing but it compiled just fine without them.

- info Downloading WASM swc package...
- warn Found lockfile missing swc dependencies, run next locally to automatically patch
- info Using wasm build of next-swc
- warn Attempted to load @next/swc-linux-x64-gnu, but it was not installed
- warn Attempted to load @next/swc-linux-x64-gnux32, but it was not installed
- warn Attempted to load @next/swc-linux-x64-musl, but it was not installed

huangapple
  • 本文由 发表于 2023年5月30日 02:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359605.html
匿名

发表评论

匿名网友

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

确定