Skip to content

Commit

Permalink
fix: node static files missing content-type headers (QwikDev#2624)
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinJSilk authored Jan 17, 2023
1 parent 8a68252 commit 27ed55f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/qwik-city/middleware/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { RenderOptions } from '@builder.io/qwik';
import type { ServerRenderOptions } from '@builder.io/qwik-city/middleware/request-handler';
import { requestHandler } from '@builder.io/qwik-city/middleware/request-handler';
import type { Render } from '@builder.io/qwik/server';
import { getNotFound } from '@qwik-city-not-found-paths';
import qwikCityPlan from '@qwik-city-plan';
import { isStaticPath } from '@qwik-city-static-paths';
import { createReadStream } from 'node:fs';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { join } from 'node:path';
import { extname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { requestHandler } from '@builder.io/qwik-city/middleware/request-handler';
import type { ServerRenderOptions } from '@builder.io/qwik-city/middleware/request-handler';
import { fromNodeHttp, getUrl } from './http';
import { MIME_TYPES } from './mime-types';
import { patchGlobalThis } from './node-fetch';

// @builder.io/qwik-city/middleware/node
Expand Down Expand Up @@ -67,6 +68,13 @@ export function createQwikCity(opts: QwikCityNodeRequestOptions) {
if (isStaticPath(req.method || 'GET', url)) {
const target = join(staticFolder, url.pathname);
const stream = createReadStream(target);
const ext = extname(url.pathname).replace(/^\./, '');

const contentType = MIME_TYPES[ext];

if (contentType) {
res.setHeader('Content-Type', contentType);
}

if (opts.static?.cacheControl) {
res.setHeader('Cache-Control', opts.static.cacheControl);
Expand Down
53 changes: 53 additions & 0 deletions packages/qwik-city/middleware/node/mime-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Common mime types mapped to Content-Type headers
*/
export const MIME_TYPES: { [ext: string]: string } = {
'3gp': 'video/3gpp',
'3gpp': 'video/3gpp',
asf: 'video/x-ms-asf',
asx: 'video/x-ms-asf',
avi: 'video/x-msvideo',
avif: 'image/avif',
bmp: 'image/x-ms-bmp',
css: 'text/css',
flv: 'video/x-flv',
gif: 'image/gif',
htm: 'text/html',
html: 'text/html',
ico: 'image/x-icon',
jng: 'image/x-jng',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
js: 'application/javascript',
json: 'application/json',
kar: 'audio/midi',
m4a: 'audio/x-m4a',
m4v: 'video/x-m4v',
mid: 'audio/midi',
midi: 'audio/midi',
mng: 'video/x-mng',
mov: 'video/quicktime',
mp3: 'audio/mpeg',
mp4: 'video/mp4',
mpeg: 'video/mpeg',
mpg: 'video/mpeg',
ogg: 'audio/ogg',
pdf: 'application/pdf',
png: 'image/png',
rar: 'application/x-rar-compressed',
shtml: 'text/html',
svg: 'image/svg+xml',
svgz: 'image/svg+xml',
tif: 'image/tiff',
tiff: 'image/tiff',
ts: 'video/mp2t',
txt: 'text/plain',
wbmp: 'image/vnd.wap.wbmp',
webm: 'video/webm',
webp: 'image/webp',
wmv: 'video/x-ms-wmv',
woff: 'font/woff',
woff2: 'font/woff2',
xml: 'text/xml',
zip: 'application/zip',
};

0 comments on commit 27ed55f

Please sign in to comment.