Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/cloudflare-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"hono": "^4.5.11",
"isbot": "^4.1.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"remix-hono": "^0.0.16"
"react-dom": "^18.3.1"
},
"devDependencies": {
"@hono/vite-dev-server": "^0.16.0",
Expand All @@ -37,4 +36,4 @@
"engines": {
"node": ">=20.0.0"
}
}
}
5 changes: 2 additions & 3 deletions examples/cloudflare-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"hono": "^4.6.9",
"isbot": "^4.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"remix-hono": "^0.0.16"
"react-dom": "^18.2.0"
},
"devDependencies": {
"@hono/vite-dev-server": "^0.16.0",
Expand All @@ -37,4 +36,4 @@
"engines": {
"node": ">=20.0.0"
}
}
}
66 changes: 3 additions & 63 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
},
"dependencies": {
"@hono/vite-dev-server": "^0.17.0",
"@remix-run/server-runtime": "^2.15.0",
"remix-hono": "^0.0.16"
"@remix-run/server-runtime": "^2.15.0"
},
"peerDependencies": {
"hono": "*"
Expand Down
3 changes: 1 addition & 2 deletions src/handlers/cloudflare-pages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Hono } from 'hono'
import { handle } from 'hono/cloudflare-pages'
import { staticAssets } from 'remix-hono/cloudflare'
import { remix } from '../middleware'
import { remix, staticAssets } from '../middleware'
import { defaultGetLoadContext } from '../remix'
import type { GetLoadContext } from '../remix'

Expand Down
45 changes: 45 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,48 @@ export const remix = ({ mode, build, getLoadContext }: RemixMiddlewareOptions) =
)
})
}

/**
* A string of directives for the Cache-Control header.
* See the [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) docs for more information.
*/

type CacheControl = string

interface StaticAssetsOptions {
cache?: CacheControl
}

export function staticAssets(options: StaticAssetsOptions = {}) {
return createMiddleware(async (c, next) => {
const binding = c.env?.ASSETS as Fetcher | undefined

if (!binding) {
throw new ReferenceError('The binding ASSETS is not set.')
}

let response: Response

c.req.raw.headers.delete('if-none-match')

try {
response = await binding.fetch(c.req.url, c.req.raw.clone())

// If the request failed, we just call the next middleware
if (response.status >= 400) {
return await next()
}

response = new Response(response.body, response)

// If cache options are configured, we set the cache-control header
if (options.cache) {
response.headers.set('cache-control', options.cache)
}

return response
} catch {
return await next()
}
})
}