feat(backend): ⚡ add well cache control for assets
This commit is contained in:
parent
972a94f5dc
commit
5263280085
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -18,3 +18,4 @@ static/dev
|
|||
|
||||
# Dev cache files
|
||||
cache/
|
||||
!src/cache/
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { FreshContext } from '$fresh/server.ts'
|
||||
import { useCache } from ':src/cache/middleware.ts'
|
||||
import { useCsp } from ':src/csp/middleware.ts'
|
||||
import { useSecurityHeaders } from ':src/security_headers/middleware.ts'
|
||||
import { useServiceworker } from ':src/serviceworker/middleware.ts'
|
||||
|
@ -16,7 +17,8 @@ export async function handler(request: Request, ctx: FreshContext) {
|
|||
useSecurityHeaders(request, response, ctx)
|
||||
await useCsp(request, response, ctx)
|
||||
useSession(request, response, ctx)
|
||||
useServiceworker(request, response, ctx)
|
||||
useServiceworker(request, response, ctx)
|
||||
useCache(request, response, ctx)
|
||||
|
||||
return response
|
||||
}
|
||||
|
|
19
src/cache/middleware.ts
vendored
Normal file
19
src/cache/middleware.ts
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { FreshContext } from '$fresh/server.ts'
|
||||
|
||||
export function useCache(
|
||||
_request: Request,
|
||||
response: Response,
|
||||
ctx: FreshContext,
|
||||
) {
|
||||
if (ctx.config.dev) return
|
||||
if (
|
||||
ctx.url.pathname.match(
|
||||
/(.+\.|_)((css)|(ttf)|(woff2)|(png)|(svg)|(jpe?g)|(avif))/,
|
||||
)
|
||||
) {
|
||||
response.headers.set(
|
||||
'Cache-Control',
|
||||
'public, max-age=31536000, immutable',
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue