Created
March 10, 2023 17:27
-
-
Save rayhanadev/e0b24127b6b9f29635094035daa0cd9d to your computer and use it in GitHub Desktop.
Koa-Astro SSR Server with Compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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