Unverified Commit fe24bb86 authored by Sukka's avatar Sukka Committed by GitHub

perf: improve next server static performance (#64098)

Previously, the `serve-static` utility function in Next.js would
determine which codepath to use based on the version of `mime` **for
every incoming request**. The PR changes so that the codepath **will be
determined in advance**, thereby reducing the overhead.
parent 073cd744
...@@ -27,22 +27,12 @@ export function serveStatic( ...@@ -27,22 +27,12 @@ export function serveStatic(
}) })
} }
export function getContentType(extWithoutDot: string): string | null { export const getContentType: (extWithoutDot: string) => string | null =
const { mime } = send 'getType' in send.mime
if ('getType' in mime) { ? (extWithoutDot: string) => send.mime.getType(extWithoutDot)
// 2.0 : (extWithoutDot: string) => (send.mime as any).lookup(extWithoutDot)
return mime.getType(extWithoutDot)
}
// 1.0
return (mime as any).lookup(extWithoutDot)
}
export function getExtension(contentType: string): string | null { export const getExtension: (contentType: string) => string | null =
const { mime } = send 'getExtension' in send.mime
if ('getExtension' in mime) { ? (contentType: string) => send.mime.getExtension(contentType)
// 2.0 : (contentType: string) => (send.mime as any).extension(contentType)
return mime.getExtension(contentType)
}
// 1.0
return (mime as any).extension(contentType)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment