Skip to content

Instantly share code, notes, and snippets.

@rayhanadev
Created March 10, 2023 17:27
Show Gist options
  • Save rayhanadev/e0b24127b6b9f29635094035daa0cd9d to your computer and use it in GitHub Desktop.
Save rayhanadev/e0b24127b6b9f29635094035daa0cd9d to your computer and use it in GitHub Desktop.
Koa-Astro SSR Server with Compression
import { handler as middleware } from '../dist/server/entry.mjs';
export const handler = async (req, res, next) => {
const response = await middleware(req, res, next);
return response;
};
import Koa from 'koa';
import cors from 'koa2-cors';
import cash from 'koa-cash';
import compress from 'koa-compress';
import m from 'koa-connect';
import serve from 'koa-static';
import LRU from 'lru-cache';
import path from 'node:path';
import { handler as ssrHandler } from './astro.mjs';
const app = new Koa();
app.use(cors());
const cache = new LRU({
ttl: 1000 * 60 * 60 * 4,
max: 50,
maxSize: 5000,
});
app.use(
cash({
get: (key) => cache.get(key),
set: (key, value) => cache.set(key, value),
}),
);
app.use(compress());
app.use(serve(path.join(process.cwd(), 'dist/', 'client/')));
app.use(async (ctx) => {
if (await ctx.cashed()) return;
return await m(ssrHandler)(ctx);
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment