diff --git a/edgeone.json b/edgeone.json new file mode 100644 index 0000000..11830c7 --- /dev/null +++ b/edgeone.json @@ -0,0 +1,8 @@ +{ + "rewrites": [ + { + "source": "/", + "destination": "/index.html" + } + ] +} \ No newline at end of file diff --git a/functions/[[default]].ts b/functions/[[default]].ts new file mode 100644 index 0000000..6d3256f --- /dev/null +++ b/functions/[[default]].ts @@ -0,0 +1 @@ +export { onRequest } from './index'; diff --git a/functions/index.ts b/functions/index.ts index d6da38f..299e86d 100644 --- a/functions/index.ts +++ b/functions/index.ts @@ -1,5 +1,139 @@ -import {app} from '../src' +import { app } from '../src' +import { Context } from 'hono' +const notFound = async (c: Context) => { + return c.html( + ` + + + + + + 404 - Page Not Found + + + +
+

404

+

Page Not Found

+

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

+

← Go back to home

+
+ + + `, + 404 + ); +}; + +// Fallback to static directory +app.notFound(async (c) => { + const url = new URL(c.req.url); + + if (url.pathname === '/') { + url.pathname = '/index.html'; + } + + try { + const res = await fetch(url.toString(), { + headers: c.req.header() + }); + + if (res.ok) { + const contentType = res.headers.get('Content-Type')!; + const body = await res.arrayBuffer(); + + return new Response(body, { + status: res.status, + headers: { + 'Content-Type': contentType, + 'Cache-Control': 'public, max-age=3600', + }, + }); + } + } catch (error) { + return notFound(c); + } + return notFound(c); +}); + +app.onError((err, c) => { + return c.html( + ` + + + + + + 500 - Internal Server Error + + + +
+

500

+

Internal Server Error

+

Something went wrong on our server. Please try again later.

+

Error: ${err.message}

+

← Go back to home

+
+ + + `, + 500 + ); +}); export function onRequest(context: { request: Request; params: Record; diff --git a/package.json b/package.json index 89efe1a..44e9850 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "scripts": { "dev-js": "tsx watch src/basic.ts", "build-js": "webpack --mode production", + "build-eo": "edgeone pages build", "dev-cf": "wrangler dev", "deploy-cf": "wrangler deploy --minify", "dev-eo": "edgeone pages dev",