-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathhandle-invalid-nextjs-paths.ts
More file actions
33 lines (27 loc) · 1.03 KB
/
handle-invalid-nextjs-paths.ts
File metadata and controls
33 lines (27 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
import type { Response, NextFunction } from 'express'
import statsd from '@/observability/lib/statsd.js'
import { defaultCacheControl } from '@/frame/middleware/cache-control.js'
import { ExtendedRequest } from '@/types'
const STATSD_KEY = 'middleware.handle_invalid_nextjs_paths'
export default function handleInvalidNextPaths(
req: ExtendedRequest,
res: Response,
next: NextFunction,
) {
// For example, `/_next/bin/junk.css`.
// The reason for depending on checking NODE_ENV is that in development,
// the Nextjs server will send things like /_next/static/webpack/...
// or /_next/webpack-hmr.
// In local dev, we don't get these penetration-testing looking requests.
if (
process.env.NODE_ENV !== 'development' &&
((req.path.startsWith('/_next/') && !req.path.startsWith('/_next/data')) ||
req.query?.['__nextFallback'])
) {
defaultCacheControl(res)
const tags = [`path:${req.path}`]
statsd.increment(STATSD_KEY, 1, tags)
return res.status(404).type('text').send('Not found')
}
return next()
}