Skip to content

Commit c52adb9

Browse files
committed
feat: not found handler for Workers SPA/SSG
1 parent b74c5ba commit c52adb9

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

apps/backend/src/app.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { appFactory, triggerFactory } from '#src/helpers/factory.js'
33
import { cookieSession } from '#src/middlewares/session.js'
44
import { cors } from 'hono/cors'
55
import { logger as loggerMiddleware } from 'hono/logger'
6-
import { env } from 'std-env'
6+
import { env, isWorkerd } from 'std-env'
77
import { apiApp } from './api/$'
88
import { logger } from './helpers/logger'
99
import { setupOpenAPI } from './openAPI'
@@ -14,7 +14,15 @@ export const app = appFactory.createApp()
1414
.use(providersInit)
1515

1616
// Register global not found handler
17-
.notFound(c => c.text('four-o-four', 404))
17+
.notFound((c) => {
18+
if (c.req.path.startsWith('/api'))
19+
return c.text('four-o-four', 404)
20+
21+
if (isWorkerd)
22+
return c.env.ASSETS.fetch('/200.html')
23+
24+
return c.text('four-o-four', 404)
25+
})
1826

1927
// Register global error handler
2028
.onError(errorHandler)

apps/backend/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export interface HonoEnv {
77
Bindings: {
88
event: LambdaEvent
99
context: LambdaContext
10+
11+
/**
12+
* Cloudflare Workers binding
13+
*/
14+
ASSETS: { fetch: (reqOrUrl: Request | string) => Promise<Response> }
1015
}
1116
Variables: {
1217
session: Session

0 commit comments

Comments
 (0)