-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (36 loc) · 1.03 KB
/
Copy pathmiddleware.ts
File metadata and controls
41 lines (36 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// List of image content types we want to cache
const IMAGE_TYPES = [
'image/jpeg',
'image/png',
'image/webp',
'image/svg+xml',
'image/gif',
]
// Function to handle caching headers
export function middleware(request: NextRequest) {
const response = NextResponse.next()
// Check if the request is for an image in the public folder
if (
request.nextUrl.pathname.startsWith('/themes/') &&
IMAGE_TYPES.some((type) => request.headers.get('accept')?.includes(type))
) {
// Set caching headers
response.headers.set('Cache-Control', 'public, max-age=31536000, immutable') // 1 year
response.headers.set(
'CDN-Cache-Control',
'public, max-age=31536000, immutable'
)
response.headers.set(
'Vercel-CDN-Cache-Control',
'public, max-age=31536000, immutable'
)
}
return response
}
// Only run middleware on image routes
export const config = {
matcher: '/themes/:path*',
}