Skip to content

Commit

Permalink
Fix dev server not correctly handling binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Mar 23, 2023
1 parent 3050057 commit 8bac709
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
17 changes: 10 additions & 7 deletions website/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Path from "path";
import {Blob} from "buffer";
import * as MimeTypes from "mime-types";

import {jsx} from "@b9g/crank/standalone";
Expand All @@ -24,12 +25,17 @@ export default {
const mimeType = MimeTypes.lookup(path) || "application/octet-stream";
const charset = MimeTypes.charset(mimeType) || "binary";
if (source) {
return new Response(source, {
const blob = new Blob([source], {
type: `${mimeType}; charset=${charset}`,
});
return new Response(blob, {
status: 200,
headers: {
"Content-Type": `${mimeType}; charset=${charset}`,
},
});
} else {
return new Response("Not found", {status: 404});
}
}

Expand All @@ -56,21 +62,18 @@ export default {
},

async *staticPaths(outDir) {
yield *["/", "/blog", "/playground"];
yield* ["/", "/blog", "/playground"];
const blogDocs = await collectDocuments(
Path.join(__dirname, "../documents/blog"),
Path.join(__dirname, "../documents"),
);
yield *blogDocs.map((doc) => doc.url);
yield* blogDocs.map((doc) => doc.url);

const guideDocs = await collectDocuments(
Path.join(__dirname, "../documents/guides"),
Path.join(__dirname, "../documents"),
);
yield *guideDocs.map((doc) => doc.url);

console.log(`Writing static files to ${outDir}`);
yield* guideDocs.map((doc) => doc.url);
await storage.write(Path.join(outDir, storage.publicPath));
},
};

7 changes: 2 additions & 5 deletions website/src/components/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class Storage {
);
}

async serve(inputPath: string): Promise<Uint8Array | string | null> {
async serve(inputPath: string): Promise<Uint8Array | null> {
inputPath = inputPath.replace(new RegExp("^" + this.publicPath), "");
const outputs: Array<OutputFile> = [];
for (const ctx of this.cache.values()) {
Expand All @@ -162,10 +162,7 @@ export class Storage {
try {
const mimeType = mime.lookup(inputPath) || "application/octet-stream";
const charset = mime.charset(mimeType) || "binary";
return await FS.readFile(
Path.join(staticPath, inputPath),
charset as any,
);
return await FS.readFile(Path.join(staticPath, inputPath));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
Expand Down

0 comments on commit 8bac709

Please sign in to comment.