Skip to content

Commit

Permalink
refactor : 미들웨어 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-hak committed Jun 27, 2024
1 parent b784180 commit a130d2a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
27 changes: 17 additions & 10 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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({
Expand Down
33 changes: 33 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit a130d2a

Please sign in to comment.