[Next] Next 빌드시 마주친 오류

임홍원·2024년 1월 16일
post-thumbnail

Next.js 빌드시 마주친 오류 해결법에 대해 기억하려고 이 글을 쓴다.

Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime

개발을 마치고 빌드를 하던 중 이런 오류를 마주쳤다.

Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime

원인은 바로 Middleware에서 session 관련한 코드인 getSession이 원인이었다.
Middleware에서는 JWT토큰만 지원하고 있어서 이러한 에러가 발생했던 것이다.

에러 발생 코드

// middleware.ts
import { getToken } from 'next-auth/jwt';
import { getSession } from 'next-auth/react';
import { NextRequest, NextResponse } from 'next/server';

export const middleware = async (req: NextRequest) => {
  const token = await getToken({
    req,
    secret: process.env.NEXTAUTH_SECRET,
    raw: true,
  });
  const session = getSession();
  console.log('token =================', token);
  console.log('session =================', session);
  const { pathname } = req.nextUrl;

  if (pathname.startsWith('/auth')) {
    if (token) {
      return NextResponse.redirect(new URL('/', req.url));
    }
  }
};

export const config = {
  matcher: ['/auth/:path*'],
};

에러 해결 코드

// middleware.ts
import { getToken } from 'next-auth/jwt';
import { getSession } from 'next-auth/react';
import { NextRequest, NextResponse } from 'next/server';

export const middleware = async (req: NextRequest) => {
  const token = await getToken({
    req,
    secret: process.env.NEXTAUTH_SECRET,
    raw: true,
  });
  const { pathname } = req.nextUrl;

  if (pathname.startsWith('/auth')) {
    if (token) {
      return NextResponse.redirect(new URL('/', req.url));
    }
  }
};

export const config = {
  matcher: ['/auth/:path*'],
};
profile
Frontend Developer

0개의 댓글