|
1 | | -import {app} from '../src' |
| 1 | +import { app } from '../src' |
| 2 | +import { Context } from 'hono' |
2 | 3 |
|
| 4 | +const notFound = async (c: Context) => { |
| 5 | + return c.html( |
| 6 | + ` |
| 7 | + <!DOCTYPE html> |
| 8 | + <html> |
| 9 | + <head> |
| 10 | + <meta charset="UTF-8"> |
| 11 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 12 | + <title>404 - Page Not Found</title> |
| 13 | + <style> |
| 14 | + body { |
| 15 | + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
| 16 | + line-height: 1.6; |
| 17 | + margin: 0; |
| 18 | + padding: 20px; |
| 19 | + background-color: #f5f5f5; |
| 20 | + text-align: center; |
| 21 | + } |
| 22 | + .container { |
| 23 | + max-width: 600px; |
| 24 | + margin: 100px auto; |
| 25 | + background: white; |
| 26 | + padding: 40px; |
| 27 | + border-radius: 8px; |
| 28 | + box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
| 29 | + } |
| 30 | + h1 { |
| 31 | + color: #e74c3c; |
| 32 | + font-size: 72px; |
| 33 | + margin: 0; |
| 34 | + } |
| 35 | + h2 { |
| 36 | + color: #333; |
| 37 | + margin: 20px 0; |
| 38 | + } |
| 39 | + </style> |
| 40 | + </head> |
| 41 | + <body> |
| 42 | + <div class="container"> |
| 43 | + <h1>404</h1> |
| 44 | + <h2>Page Not Found</h2> |
| 45 | + <p>The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.</p> |
| 46 | + <p><a href="/">← Go back to home</a></p> |
| 47 | + </div> |
| 48 | + </body> |
| 49 | + </html> |
| 50 | + `, |
| 51 | + 404 |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +// Fallback to static directory |
| 56 | +app.notFound(async (c) => { |
| 57 | + const url = new URL(c.req.url); |
| 58 | + |
| 59 | + if (url.pathname === '/') { |
| 60 | + url.pathname = '/index.html'; |
| 61 | + } |
| 62 | + |
| 63 | + try { |
| 64 | + const res = await fetch(url.toString(), { |
| 65 | + headers: c.req.header() |
| 66 | + }); |
| 67 | + |
| 68 | + if (res.ok) { |
| 69 | + const contentType = res.headers.get('Content-Type')!; |
| 70 | + const body = await res.arrayBuffer(); |
| 71 | + |
| 72 | + return new Response(body, { |
| 73 | + status: res.status, |
| 74 | + headers: { |
| 75 | + 'Content-Type': contentType, |
| 76 | + 'Cache-Control': 'public, max-age=3600', |
| 77 | + }, |
| 78 | + }); |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + return notFound(c); |
| 82 | + } |
| 83 | + return notFound(c); |
| 84 | +}); |
| 85 | + |
| 86 | +app.onError((err, c) => { |
| 87 | + return c.html( |
| 88 | + ` |
| 89 | + <!DOCTYPE html> |
| 90 | + <html> |
| 91 | + <head> |
| 92 | + <meta charset="UTF-8"> |
| 93 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 94 | + <title>500 - Internal Server Error</title> |
| 95 | + <style> |
| 96 | + body { |
| 97 | + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
| 98 | + line-height: 1.6; |
| 99 | + margin: 0; |
| 100 | + padding: 20px; |
| 101 | + background-color: #f5f5f5; |
| 102 | + text-align: center; |
| 103 | + } |
| 104 | + .container { |
| 105 | + max-width: 600px; |
| 106 | + margin: 100px auto; |
| 107 | + background: white; |
| 108 | + padding: 40px; |
| 109 | + border-radius: 8px; |
| 110 | + box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
| 111 | + } |
| 112 | + h1 { |
| 113 | + color: #e74c3c; |
| 114 | + font-size: 72px; |
| 115 | + margin: 0; |
| 116 | + } |
| 117 | + h2 { |
| 118 | + color: #333; |
| 119 | + margin: 20px 0; |
| 120 | + } |
| 121 | + </style> |
| 122 | + </head> |
| 123 | + <body> |
| 124 | + <div class="container"> |
| 125 | + <h1>500</h1> |
| 126 | + <h2>Internal Server Error</h2> |
| 127 | + <p>Something went wrong on our server. Please try again later.</p> |
| 128 | + <p>Error: ${err.message}</p> |
| 129 | + <p><a href="/">← Go back to home</a></p> |
| 130 | + </div> |
| 131 | + </body> |
| 132 | + </html> |
| 133 | + `, |
| 134 | + 500 |
| 135 | + ); |
| 136 | +}); |
3 | 137 | export function onRequest(context: { |
4 | 138 | request: Request; |
5 | 139 | params: Record<string, string>; |
|
0 commit comments