From a130d2a4c523858db11fc4eef4aa1f50ce61e8d2 Mon Sep 17 00:00:00 2001 From: eunhak Date: Thu, 27 Jun 2024 14:58:35 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=AF=B8=EB=93=A4=EC=9B=A8?= =?UTF-8?q?=EC=96=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- next.config.mjs | 27 +++++++++++++++++---------- src/middleware.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 src/middleware.ts diff --git a/next.config.mjs b/next.config.mjs index 9c96d67..e2b9b0f 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -2,16 +2,6 @@ import withPWA from 'next-pwa'; const nextConfig = { - async redirects() { - return [ - { - source: '/', - destination: '/sign', - permanent: true // true로 설정하면 308 리다이렉션, false로 설정하면 307 리다이렉션 - } - ]; - }, - images: { domains: [ 'bzbz-file-bucket.s3.ap-northeast-2.amazonaws.com', @@ -32,6 +22,23 @@ const nextConfig = { config.resolve.alias.push({ name: 'msw/node', alias: false }); else config.resolve.alias['msw/node'] = false; } + + if (!isServer) { + // splitChunks가 객체인지 확인하고, 필요하다면 초기화 + if ( + !config.optimization.splitChunks || + typeof config.optimization.splitChunks !== 'object' + ) { + config.optimization.splitChunks = { cacheGroups: {} }; + } + + config.optimization.splitChunks.cacheGroups.commons = { + test: /[\\/]node_modules[\\/]/, + name: 'vendors', + chunks: 'all' + }; + } + return config; }, pwa: withPWA({ diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..defbfca --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,33 @@ +import { NextResponse } from 'next/server'; +import type { NextRequest } from 'next/server'; + +export function middleware(request: NextRequest) { + const token = request.cookies.get('token'); + const pathname = request.nextUrl.pathname; + + // API 라우트나 정적 파일, 특정 정적 자원에 대한 요청을 건너뛰고, 페이지 요청에만 미들웨어를 적용합니다. + if ( + pathname.startsWith('/api') || + pathname.includes('.') || + pathname.startsWith('/_next/static/') || + pathname.startsWith('/favicon') || + pathname === '/manifest.json' || + pathname === '/firebase-messaging-sw.js' || + pathname.startsWith('/icons/') + ) { + return NextResponse.next(); + } + console.log('middleware', token, pathname); + + if ( + !token && + pathname !== '/sign' && + pathname !== '/signin' && + pathname !== '/signup' && + pathname !== '/sign/findpassword' + ) { + return NextResponse.redirect(new URL('/sign', request.url)); + } + + return NextResponse.next(); +}