Skip to content

mediaTypeOfPath

Finds the media type of a file from the extension of its path or URL: images, fonts, web documents, scripts and styles, data, audio and video. Case is ignored, and a query string or fragment after the path is skipped. Pass it where a server or a Blob needs a Content-Type.

Available since v3.1.10

import { mediaTypeOfPath } from '@helpers4/binary';
// or, from the all-in-one package (same code, one install):
import { mediaTypeOfPath } from 'helpers4/binary';
mediaTypeOfPath(path: string): string | undefined
ParameterTypeDescription
pathstringA file path or URL

string | undefined — The media type ('image/webp', …), or undefined for an unknown or missing extension

Case and a query string are ignored.

mediaTypeOfPath('/fonts/Inter.WOFF2?v=3') // => 'font/woff2'

View source on GitHub