Skip to content

Commit e8726aa

Browse files
committed
Production settings.
1 parent debef37 commit e8726aa

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

packages/magazin/client.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hydrateRoot } from "react-dom/client";
22
import { flushSync } from "react-dom";
3-
import { Router, type NavigateOptions, type AroundNavHandler } from "wouter";
3+
import { Router, type AroundNavHandler } from "wouter";
44
import { HelmetProvider } from "@dr.pogodin/react-helmet";
55
import { App } from "./App";
66

packages/magazin/index.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import { App } from "./App.tsx";
88
import tailwind from "bun-plugin-tailwind";
99

1010
// Build the HTML and all its assets before starting the server
11+
const isProduction = process.env.NODE_ENV === "production";
12+
1113
const build = await Bun.build({
1214
entrypoints: ["./index.html"],
1315
// No outdir = files are kept in memory, not written to disk
14-
minify: false,
16+
minify: isProduction,
1517
publicPath: "/",
18+
sourcemap: "linked",
1619
plugins: [tailwind],
20+
define: {
21+
"process.env.NODE_ENV": JSON.stringify(
22+
process.env.NODE_ENV || "development"
23+
),
24+
},
1725
});
1826

1927
if (!build.success) {
@@ -51,13 +59,27 @@ Bun.serve({
5159
// Check if this is a request for a built asset
5260
const asset = assets.get(url.pathname);
5361
if (asset) {
54-
return new Response(asset);
62+
const headers = isProduction
63+
? {
64+
// Built assets have content hashes, so they can be cached indefinitely
65+
"Cache-Control": "public, max-age=31536000, immutable",
66+
}
67+
: {};
68+
69+
return new Response(asset, { headers });
5570
}
5671

5772
// Check if this is a request for a static file from public/
5873
const publicFile = Bun.file(`./public${url.pathname}`);
5974
if (await publicFile.exists()) {
60-
return new Response(publicFile);
75+
const headers = new Headers();
76+
77+
// Add 24h caching for static assets in production
78+
if (isProduction) {
79+
headers.set("Cache-Control", "public, max-age=86400");
80+
}
81+
82+
return new Response(publicFile, { headers });
6183
}
6284

6385
// Otherwise, it's a page request - render with SSR

packages/magazin/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"private": true,
66
"scripts": {
77
"dev": "bun --watch index.tsx",
8-
"start": "bun index.tsx"
8+
"start": "bun index.tsx",
9+
"prod": "NODE_ENV=production bun index.tsx"
910
},
1011
"devDependencies": {
1112
"@types/bun": "latest"

0 commit comments

Comments
 (0)