Skip to content

Commit 3518817

Browse files
middleware & nextconfig fixed
1 parent d9b86f2 commit 3518817

File tree

2 files changed

+32
-61
lines changed

2 files changed

+32
-61
lines changed

middleware.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,29 @@ import { NextResponse } from 'next/server'
22
import type { NextRequest } from 'next/server'
33

44
export function middleware(request: NextRequest) {
5-
65
const requestId = `mid_${crypto.randomUUID()}`
76
const startTime = Date.now()
87

98
console.log(`[Middleware ${requestId}] ${request.method} ${request.nextUrl.pathname}`)
109

11-
1210
if (request.nextUrl.pathname.startsWith('/api/')) {
1311
const response = NextResponse.next()
1412

15-
1613
response.headers.set('Access-Control-Allow-Origin', '*')
1714
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
1815
response.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With')
1916
response.headers.set('Access-Control-Max-Age', '86400')
2017

21-
2218
response.headers.set('X-Request-ID', requestId)
2319
response.headers.set('X-Response-Time', `${Date.now() - startTime}ms`)
2420

25-
2621
if (request.method === 'OPTIONS') {
2722
return new Response(null, { status: 200, headers: response.headers })
2823
}
2924

30-
3125
if (request.method === 'POST') {
3226
const contentType = request.headers.get('content-type')
3327

34-
3528
if (request.nextUrl.pathname === '/api/chat') {
3629
if (!contentType?.includes('application/json')) {
3730
console.warn(`[Middleware ${requestId}] Invalid content type for /api/chat: ${contentType}`)
@@ -49,7 +42,6 @@ export function middleware(request: NextRequest) {
4942
}
5043
}
5144

52-
5345
if (request.nextUrl.pathname === '/api/project/analyze' ||
5446
request.nextUrl.pathname === '/api/upload-files') {
5547
if (!contentType?.includes('multipart/form-data')) {
@@ -96,7 +88,7 @@ export function middleware(request: NextRequest) {
9688

9789
response.headers.set('Content-Security-Policy', csp)
9890

99-
91+
// Add request tracking
10092
response.headers.set('X-Request-ID', requestId)
10193

10294
console.log(`[Middleware ${requestId}] Processed in ${Date.now() - startTime}ms`)
@@ -106,7 +98,13 @@ export function middleware(request: NextRequest) {
10698

10799
export const config = {
108100
matcher: [
109-
101+
/*
102+
* Match all request paths except for the ones starting with:
103+
* - _next/static (static files)
104+
* - _next/image (image optimization files)
105+
* - favicon.ico (favicon file)
106+
* - public folder
107+
*/
110108
'/((?!_next/static|_next/image|favicon.ico|public|icons).*)',
111109
],
112110
}

next.config.mjs

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,6 @@
1-
import { readFile } from 'fs/promises';
2-
import { join } from 'path';
3-
41
/** @type {import('next').NextConfig} */
5-
const nextConfig = {
6-
webpack: config => {
7-
config.externals.push({
8-
'@supabase/realtime-js': 'commonjs @supabase/realtime-js',
9-
});
10-
11-
// Handling large JSON files by reading them at build time
12-
const templatesPath = join(process.cwd(), 'lib', 'templates.json');
13-
config.plugins.push(
14-
new (class {
15-
apply(compiler) {
16-
compiler.hooks.thisCompilation.tap(
17-
'InjectTemplatesPlugin',
18-
compilation => {
19-
compilation.hooks.processAssets.tapAsync(
20-
{
21-
name: 'InjectTemplatesPlugin',
22-
stage:
23-
compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
24-
},
25-
async (assets, callback) => {
26-
try {
27-
const templatesContent = await readFile(
28-
templatesPath,
29-
'utf-8',
30-
);
31-
const templatesJson = JSON.stringify(
32-
JSON.parse(templatesContent),
33-
);
34-
const assetId = 'lib/templates.json';
35-
assets[assetId] = {
36-
source: () => `export default ${templatesJson}`,
37-
size: () => templatesJson.length,
38-
};
39-
callback();
40-
} catch (error) {
41-
callback(error);
42-
}
43-
},
44-
);
45-
},
46-
);
47-
}
48-
})(),
49-
);
502

51-
return config;
52-
},
3+
const nextConfig = {
534
env: {
545
SANDBOX_ID: 'ina8cw5gcg0ts1as7iz7o-5b7f1102',
556
},
@@ -69,6 +20,28 @@ const nextConfig = {
6920
},
7021
];
7122
},
23+
webpack: (config, { isServer, webpack }) => {
24+
// The "Critical dependency: the request of a dependency is an expression" warning
25+
// often originates from libraries like @supabase/realtime-js when they use
26+
// dynamic requires or imports that Webpack cannot statically analyze.
27+
28+
// If this warning appears during server-side bundling (e.g., for API routes, as in this case),
29+
// making @supabase/realtime-js external can resolve it. This means Node.js
30+
// will require() it at runtime instead of Webpack trying to bundle it.
31+
// This is generally safe if its real-time client features aren't actively used
32+
// in the server-side context where it's imported.
33+
if (isServer) {
34+
config.externals = [...config.externals, '@supabase/realtime-js'];
35+
}
36+
37+
// Additionally, @supabase/realtime-js or its dependencies (like WebSocket libraries)
38+
// might try to optionally include native modules like 'bufferutil' and 'utf-8-validate'.
39+
// Making these external tells Webpack not to bundle them and can prevent related warnings.
40+
// This is a common fix for issues involving the 'ws' package or similar.
41+
config.externals.push('bufferutil', 'utf-8-validate');
42+
43+
return config;
44+
},
7245
};
7346

74-
export default nextConfig;
47+
export default nextConfig;

0 commit comments

Comments
 (0)