From 6b5866c5de77aa517a7c3a0e96e37062e080481c Mon Sep 17 00:00:00 2001 From: barakcodes <35469348+barakcodes@users.noreply.github.com> Date: Fri, 29 Nov 2024 09:51:45 +0300 Subject: [PATCH] fix: Remove remix-hono dependency (#36) * Remove remix-hon dependency Removes `remix-hon` as a dependency to allow for a smoother upgrade to react-router 7 * fix formatting * Remove remix-hono from examples --- examples/cloudflare-pages/package.json | 5 +- examples/cloudflare-workers/package.json | 5 +- package-lock.json | 66 ++---------------------- package.json | 3 +- src/handlers/cloudflare-pages.ts | 3 +- src/middleware.ts | 45 ++++++++++++++++ 6 files changed, 54 insertions(+), 73 deletions(-) diff --git a/examples/cloudflare-pages/package.json b/examples/cloudflare-pages/package.json index d735ca0..017cb18 100644 --- a/examples/cloudflare-pages/package.json +++ b/examples/cloudflare-pages/package.json @@ -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", @@ -37,4 +36,4 @@ "engines": { "node": ">=20.0.0" } -} \ No newline at end of file +} diff --git a/examples/cloudflare-workers/package.json b/examples/cloudflare-workers/package.json index 4b70814..78db868 100644 --- a/examples/cloudflare-workers/package.json +++ b/examples/cloudflare-workers/package.json @@ -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", @@ -37,4 +36,4 @@ "engines": { "node": ">=20.0.0" } -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 0cc2196..4af5efe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,8 +16,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" }, "devDependencies": { "@cloudflare/vitest-pool-workers": "^0.4.23", @@ -81,8 +80,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", @@ -406,8 +404,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", @@ -13671,18 +13668,6 @@ "url": "https://github.com/prettier/prettier?sponsor=1" } }, - "node_modules/pretty-cache-header": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/pretty-cache-header/-/pretty-cache-header-1.0.0.tgz", - "integrity": "sha512-xtXazslu25CdnGnUkByU1RoOjK55TqwatJkjjJLg5ZAdz2Lngko/mmaUgeET36P2GMlNwh3fdM7FWBO717pNcw==", - "license": "MIT", - "dependencies": { - "timestring": "^6.0.0" - }, - "engines": { - "node": ">=12.13" - } - }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", @@ -14296,42 +14281,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/remix-hono": { - "version": "0.0.16", - "resolved": "https://registry.npmjs.org/remix-hono/-/remix-hono-0.0.16.tgz", - "integrity": "sha512-IPooI2E0eSFRV9wAgTzpsklSoOyij6Nsk6ANIugGhD9vYlovbYb2BT+siz++sLzWseZSPEtIzx/LBel203jBfQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/sergiodxa" - } - ], - "dependencies": { - "@remix-run/server-runtime": "^2.6.0", - "hono": "^4.0.0", - "pretty-cache-header": "^1.0.0" - }, - "peerDependencies": { - "@remix-run/cloudflare": "^2.0.0", - "i18next": "^23.0.0", - "remix-i18next": "^6.0.0", - "zod": "^3.0.0" - }, - "peerDependenciesMeta": { - "@remix-run/cloudflare": { - "optional": true - }, - "i18next": { - "optional": true - }, - "remix-i18next": { - "optional": true - }, - "zod": { - "optional": true - } - } - }, "node_modules/require-like": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/require-like/-/require-like-0.1.2.tgz", @@ -15763,15 +15712,6 @@ "safe-buffer": "~5.1.0" } }, - "node_modules/timestring": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/timestring/-/timestring-6.0.0.tgz", - "integrity": "sha512-wMctrWD2HZZLuIlchlkE2dfXJh7J2KDI9Dwl+2abPYg0mswQHfOAyQW3jJg1pY5VfttSINZuKcXoB3FGypVklA==", - "license": "MIT", - "engines": { - "node": ">=8" - } - }, "node_modules/tinybench": { "version": "2.9.0", "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", diff --git a/package.json b/package.json index 9ae36df..6673ff6 100644 --- a/package.json +++ b/package.json @@ -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": "*" diff --git a/src/handlers/cloudflare-pages.ts b/src/handlers/cloudflare-pages.ts index c36d5fd..112ba0a 100644 --- a/src/handlers/cloudflare-pages.ts +++ b/src/handlers/cloudflare-pages.ts @@ -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' diff --git a/src/middleware.ts b/src/middleware.ts index 7354bb2..06446dd 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -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() + } + }) +}