Skip to content

Commit

Permalink
Add Content-Type header to wrangler pages dev's static assets (#161)
Browse files Browse the repository at this point in the history
* Add mime types to Content-Type header in pages dev

* Add changeset
  • Loading branch information
GregBrimble authored Dec 22, 2021
1 parent 43fb76a commit 0330ecf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/flat-wombats-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Adds the Content-Type header when serving assets with `wrangler pages dev`. It guesses the mime-type based on the asset's file extension.
36 changes: 35 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"devDependencies": {
"@iarna/toml": "^2.2.5",
"@types/mime": "^2.0.3",
"@types/react": "^17.0.37",
"@types/serve-static": "^1.13.10",
"@types/signal-exit": "^3.0.1",
Expand All @@ -63,6 +64,7 @@
"ink-select-input": "^4.2.1",
"ink-table": "^3.0.0",
"ink-text-input": "^4.0.2",
"mime": "^3.0.0",
"node-fetch": "^3.1.0",
"open": "^8.4.0",
"path-to-regexp": "^6.2.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { execSync, spawn } from "child_process";
import { Headers, Request, Response } from "undici";
import type { MiniflareOptions } from "miniflare";
import type { RequestInfo, RequestInit } from "undici";
import { getType } from "mime";
import open from "open";
import { watch } from "chokidar";

Expand Down Expand Up @@ -466,12 +467,20 @@ const generateAssetsFetch = async (
if ((asset = getAsset(`${cwd}/404.html`))) {
deconstructedResponse.status = 404;
deconstructedResponse.body = serveAsset(asset);
deconstructedResponse.headers.set(
"Content-Type",
getType(asset) || "application/octet-stream"
);
return deconstructedResponse;
}
}

if ((asset = getAsset(`/index.html`))) {
deconstructedResponse.body = serveAsset(asset);
deconstructedResponse.headers.set(
"Content-Type",
getType(asset) || "application/octet-stream"
);
return deconstructedResponse;
}

Expand All @@ -484,6 +493,10 @@ const generateAssetsFetch = async (
if (url.pathname.endsWith("/")) {
if ((asset = getAsset(`${url.pathname}/index.html`))) {
deconstructedResponse.body = serveAsset(asset);
deconstructedResponse.headers.set(
"Content-Type",
getType(asset) || "application/octet-stream"
);
return deconstructedResponse;
} else if (
(asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
Expand Down Expand Up @@ -511,6 +524,10 @@ const generateAssetsFetch = async (
const extensionlessPath = url.pathname.slice(0, -".html".length);
if (getAsset(extensionlessPath) || extensionlessPath === "/") {
deconstructedResponse.body = serveAsset(asset);
deconstructedResponse.headers.set(
"Content-Type",
getType(asset) || "application/octet-stream"
);
return deconstructedResponse;
} else {
deconstructedResponse.status = 301;
Expand All @@ -522,6 +539,10 @@ const generateAssetsFetch = async (
}
} else {
deconstructedResponse.body = serveAsset(asset);
deconstructedResponse.headers.set(
"Content-Type",
getType(asset) || "application/octet-stream"
);
return deconstructedResponse;
}
} else if (hasFileExtension(url.pathname)) {
Expand All @@ -531,6 +552,10 @@ const generateAssetsFetch = async (

if ((asset = getAsset(`${url.pathname}.html`))) {
deconstructedResponse.body = serveAsset(asset);
deconstructedResponse.headers.set(
"Content-Type",
getType(asset) || "application/octet-stream"
);
return deconstructedResponse;
}

Expand Down

0 comments on commit 0330ecf

Please sign in to comment.