Skip to content

Commit

Permalink
Handle 'common' web file MIME types
Browse files Browse the repository at this point in the history
This handles serving 'common' (js, css, html) file types within the static assets middleware.
The returned MIME type for these files is `text/plain` and based on this we attempt to determine the correct MIME type via their file extension.
This is by no means an exhaustive list but provides support for files which would be typical for a web application.
  • Loading branch information
eddmann committed Jan 16, 2025
1 parent 39508e0 commit dc7832a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Http/Middleware/ServeStaticAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ public function handle(Request $request, Closure $next)
protected function getMimeType(string $file)
{
$mimeType = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
$mimeType = strstr($mimeType, ';', true);

return str_replace(
['image/vnd.microsoft.icon'],
['image/x-icon'],
$mimeType
);
if ($mimeType === 'image/vnd.microsoft.icon') {
return 'image/x-icon';
}

if ($mimeType !== 'text/plain') {
return $mimeType;
}

return match (pathinfo($file, PATHINFO_EXTENSION)) {
'js' => 'text/javascript',
'css' => 'text/css',
'html' => 'text/html',
default => 'text/plain',
};
}
}

0 comments on commit dc7832a

Please sign in to comment.