diff --git a/packages/waku/src/config.ts b/packages/waku/src/config.ts index 828c1712e..139342ebd 100644 --- a/packages/waku/src/config.ts +++ b/packages/waku/src/config.ts @@ -49,7 +49,7 @@ export interface Config { */ middleware?: () => Promise<{ default: Middleware }>[]; /** - * Enhander for Hono + * Enhancer for Hono * Defaults to `undefined` */ unstable_honoEnhancer?: diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts index baa50ce20..b53284289 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-aws-lambda.ts @@ -17,7 +17,12 @@ const getServeJsContent = ( ) => ` import path from 'node:path'; import { existsSync, readFileSync } from 'node:fs'; -import { serverEngine, importHono, importHonoNodeServerServeStatic, importHonoAwsLambda } from 'waku/unstable_hono'; +import { + serverEngine, + importHono, + importHonoNodeServerServeStatic, + importHonoAwsLambda, +} from 'waku/unstable_hono'; const { Hono } = await importHono(); const { serveStatic } = await importHonoNodeServerServeStatic(); @@ -27,18 +32,25 @@ const distDir = '${distDir}'; const publicDir = '${distPublic}'; const loadEntries = () => import('${srcEntriesFile}'); -const app = new Hono(); -app.use(serveStatic({ root: distDir + '/' + publicDir })); -app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env })); -app.notFound(async (c) => { - const file = path.join(distDir, publicDir, '404.html'); - if (existsSync(file)) { - return c.html(readFileSync(file, 'utf8'), 404); - } - return c.text('404 Not Found', 404); -}); +const configPromise = loadEntries().then((entries) => entries.loadConfig()); -export const handler = handle(app); +const createApp = (app) => { + app.use(serveStatic({ root: distDir + '/' + publicDir })); + app.use(serverEngine({ cmd: 'start', loadEntries, env: process.env })); + app.notFound(async (c) => { + const file = path.join(distDir, publicDir, '404.html'); + if (existsSync(file)) { + return c.html(readFileSync(file, 'utf8'), 404); + } + return c.text('404 Not Found', 404); + }); + return app; +}; + +const honoEnhancer = + (await configPromise).unstable_honoEnhancer || ((createApp) => createApp); + +export const handler = handle(honoEnhancer(createApp)(new Hono())); `; export function deployAwsLambdaPlugin(opts: { diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts index 48d567d1b..8503902d0 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-cloudflare.ts @@ -23,27 +23,40 @@ const { Hono } = await importHono(); const loadEntries = () => import('${srcEntriesFile}'); let serve; - -const app = new Hono(); -app.use((c, next) => serve(c, next)); -app.notFound(async (c) => { - const assetsFetcher = c.env.ASSETS; - const url = new URL(c.req.raw.url); - const errorHtmlUrl = url.origin + '/404.html'; - const notFoundStaticAssetResponse = await assetsFetcher.fetch( - new URL(errorHtmlUrl), - ); - if (notFoundStaticAssetResponse && notFoundStaticAssetResponse.status < 400) { - return c.body(notFoundStaticAssetResponse.body, 404); - } - return c.text('404 Not Found', 404); -}); +let app; + +const createApp = (app) => { + app.use((c, next) => serve(c, next)); + app.notFound(async (c) => { + const assetsFetcher = c.env.ASSETS; + const url = new URL(c.req.raw.url); + const errorHtmlUrl = url.origin + '/404.html'; + const notFoundStaticAssetResponse = await assetsFetcher.fetch( + new URL(errorHtmlUrl), + ); + if ( + notFoundStaticAssetResponse && + notFoundStaticAssetResponse.status < 400 + ) { + return c.body(notFoundStaticAssetResponse.body, 404); + } + return c.text('404 Not Found', 404); + }); + return app; +}; export default { async fetch(request, env, ctx) { if (!serve) { serve = serverEngine({ cmd: 'start', loadEntries, env }); } + if (!app) { + const entries = await loadEntries(); + const config = await entries.loadConfig(); + const honoEnhancer = + config.unstable_honoEnhancer || ((createApp) => createApp); + app = honoEnhancer(createApp)(new Hono()); + } return app.fetch(request, env, ctx); }, }; diff --git a/packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts b/packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts index 9a17938e5..866e81717 100644 --- a/packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts +++ b/packages/waku/src/lib/plugins/vite-plugin-deploy-partykit.ts @@ -15,30 +15,40 @@ const { Hono } = await importHono(); const loadEntries = () => import('${srcEntriesFile}'); let serve; +let app; -const app = new Hono(); -app.use((c, next) => serve(c, next)); -app.notFound(async (c) => { - const assetsFetcher = c.env.assets; - // check if there's a 404.html in the static assets - const notFoundStaticAssetResponse = await assetsFetcher.fetch('/404.html'); - // if there is, return it - if (notFoundStaticAssetResponse) { - return new Response(notFoundStaticAssetResponse.body, { - status: 404, - statusText: 'Not Found', - headers: notFoundStaticAssetResponse.headers, - }); - } - // otherwise, return a simple 404 response - return c.text('404 Not Found', 404); -}); +const createApp = (app) => { + app.use((c, next) => serve(c, next)); + app.notFound(async (c) => { + const assetsFetcher = c.env.ASSETS; + const url = new URL(c.req.raw.url); + const errorHtmlUrl = url.origin + '/404.html'; + const notFoundStaticAssetResponse = await assetsFetcher.fetch( + new URL(errorHtmlUrl), + ); + if ( + notFoundStaticAssetResponse && + notFoundStaticAssetResponse.status < 400 + ) { + return c.body(notFoundStaticAssetResponse.body, 404); + } + return c.text('404 Not Found', 404); + }); + return app; +}; export default { - onFetch(request, lobby, ctx) { + async onFetch(request, lobby, ctx) { if (!serve) { serve = serverEngine({ cmd: 'start', loadEntries, env: lobby }); } + if (!app) { + const entries = await loadEntries(); + const config = await entries.loadConfig(); + const honoEnhancer = + config.unstable_honoEnhancer || ((createApp) => createApp); + app = honoEnhancer(createApp)(new Hono()); + } return app.fetch(request, lobby, ctx); }, };