File size: 1,803 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { ServerResponse, IncomingMessage } from 'http'
import path from 'path'
import * as fs from 'fs/promises'
import { constants } from 'fs'
import * as Log from '../../../build/output/log'
import { middlewareResponse } from '../middleware-response'

const FONT_PREFIX = '/__nextjs_font/'

const VALID_FONTS = [
  'geist-latin-ext.woff2',
  'geist-mono-latin-ext.woff2',
  'geist-latin.woff2',
  'geist-mono-latin.woff2',
]

const FONT_HEADERS = {
  'Content-Type': 'font/woff2',
  'Cache-Control': 'public, max-age=31536000, immutable',
} as const

export function getDevOverlayFontMiddleware() {
  return async function devOverlayFontMiddleware(
    req: IncomingMessage,
    res: ServerResponse,
    next: () => void
  ): Promise<void> {
    try {
      const { pathname } = new URL(`http://n${req.url}`)

      if (!pathname.startsWith(FONT_PREFIX)) {
        return next()
      }

      const fontFile = pathname.replace(FONT_PREFIX, '')
      if (!VALID_FONTS.includes(fontFile)) {
        return middlewareResponse.notFound(res)
      }

      const fontPath = path.resolve(__dirname, fontFile)
      const fileExists = await checkFileExists(fontPath)

      if (!fileExists) {
        return middlewareResponse.notFound(res)
      }

      const fontData = await fs.readFile(fontPath)
      Object.entries(FONT_HEADERS).forEach(([key, value]) => {
        res.setHeader(key, value)
      })
      res.end(fontData)
    } catch (err) {
      Log.error(
        'Failed to serve font:',
        err instanceof Error ? err.message : err
      )
      return middlewareResponse.internalServerError(res)
    }
  }
}

async function checkFileExists(filePath: string): Promise<boolean> {
  try {
    await fs.access(filePath, constants.F_OK)
    return true
  } catch {
    return false
  }
}