Add gzip compression to responses
This commit is contained in:
60
index.tsx
60
index.tsx
@@ -7,6 +7,18 @@ import { NotFound } from "./src/frontend/pages/not-found";
|
|||||||
import { Post } from "./src/frontend/pages/post";
|
import { Post } from "./src/frontend/pages/post";
|
||||||
import { dbConnection } from "./src/db";
|
import { dbConnection } from "./src/db";
|
||||||
|
|
||||||
|
function compressResponse(html: string, status?: number) {
|
||||||
|
const compressed = Bun.gzipSync(Buffer.from(html));
|
||||||
|
return new Response(compressed, {
|
||||||
|
status,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "text/html",
|
||||||
|
"Content-Encoding": "gzip",
|
||||||
|
"Vary": "Accept-Encoding"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function blogPosts(hmr: boolean) {
|
async function blogPosts(hmr: boolean) {
|
||||||
const glob = new Bun.Glob("**/*.md");
|
const glob = new Bun.Glob("**/*.md");
|
||||||
const blogPosts: Record<string, any> = {};
|
const blogPosts: Record<string, any> = {};
|
||||||
@@ -24,10 +36,7 @@ async function blogPosts(hmr: boolean) {
|
|||||||
const path = new URL(req.url).pathname;
|
const path = new URL(req.url).pathname;
|
||||||
const post = dbConnection.getPost(path);
|
const post = dbConnection.getPost(path);
|
||||||
if (!post)
|
if (!post)
|
||||||
return new Response(renderToString(<NotFound />), {
|
return compressResponse(renderToString(<NotFound />), 404);
|
||||||
status: 404,
|
|
||||||
headers: { "Content-Type": "text/html" },
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get adjacent posts for navigation
|
// Get adjacent posts for navigation
|
||||||
const { previousPost, nextPost } = dbConnection.getAdjacentPosts(post.path);
|
const { previousPost, nextPost } = dbConnection.getAdjacentPosts(post.path);
|
||||||
@@ -44,18 +53,13 @@ async function blogPosts(hmr: boolean) {
|
|||||||
|
|
||||||
// AppShell is already loaded, just send the <main> content
|
// AppShell is already loaded, just send the <main> content
|
||||||
if (req.headers.get("shell-loaded") === "true") {
|
if (req.headers.get("shell-loaded") === "true") {
|
||||||
return new Response(
|
return compressResponse(
|
||||||
renderToString(<Post meta={data} children={post.content} />),
|
renderToString(<Post meta={data} children={post.content} />)
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "text/html",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppShell is not loaded, send the <AppShell> with the <main> content inside
|
// AppShell is not loaded, send the <AppShell> with the <main> content inside
|
||||||
return new Response(
|
return compressResponse(
|
||||||
renderToString(
|
renderToString(
|
||||||
<AppShell>
|
<AppShell>
|
||||||
<Post
|
<Post
|
||||||
@@ -63,12 +67,7 @@ async function blogPosts(hmr: boolean) {
|
|||||||
children={post.content}
|
children={post.content}
|
||||||
/>
|
/>
|
||||||
</AppShell>,
|
</AppShell>,
|
||||||
),
|
)
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "text/html",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -99,24 +98,15 @@ Bun.serve({
|
|||||||
const searchParams = new URLSearchParams(req.url.split('?')[1]);
|
const searchParams = new URLSearchParams(req.url.split('?')[1]);
|
||||||
|
|
||||||
if (req.headers.get("shell-loaded") === "true") {
|
if (req.headers.get("shell-loaded") === "true") {
|
||||||
return new Response(renderToString(<Home searchParams={searchParams} />), {
|
return compressResponse(renderToString(<Home searchParams={searchParams} />));
|
||||||
headers: {
|
|
||||||
"Content-Type": "text/html",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(
|
return compressResponse(
|
||||||
renderToString(
|
renderToString(
|
||||||
<AppShell searchParams={searchParams}>
|
<AppShell searchParams={searchParams}>
|
||||||
<Home searchParams={searchParams} />
|
<Home searchParams={searchParams} />
|
||||||
</AppShell>,
|
</AppShell>,
|
||||||
),
|
)
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "text/html",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
"/profile-picture.webp": () => {
|
"/profile-picture.webp": () => {
|
||||||
@@ -128,15 +118,9 @@ Bun.serve({
|
|||||||
},
|
},
|
||||||
"/*": (req) => {
|
"/*": (req) => {
|
||||||
if(req.headers.get("shell-loaded") === "true") {
|
if(req.headers.get("shell-loaded") === "true") {
|
||||||
return new Response(renderToString(<NotFound />), {
|
return compressResponse(renderToString(<NotFound />), 404);
|
||||||
status: 404,
|
|
||||||
headers: { "Content-Type": "text/html" },
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return new Response(renderToString(<AppShell><NotFound /></AppShell>), {
|
return compressResponse(renderToString(<AppShell><NotFound /></AppShell>), 404);
|
||||||
status: 404,
|
|
||||||
headers: { "Content-Type": "text/html" },
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user