Skip to content

Commit 6743c53

Browse files
authored
chore: Remix to React Router 7 (#50)
1 parent fd2c34f commit 6743c53

40 files changed

+93
-138
lines changed

README.md

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# hono-remix-adapter
1+
# hono-react-router-adapter
22

3-
`hono-remix-adapter` is a set of tools for adapting between Hono and React Router. It is composed of a Vite plugin and handlers that enable it to support platforms like Cloudflare Workers and Node.js. You just create Hono app, and it will be applied to your React Router app.
3+
`hono-react-router-adapter` is a set of tools for adapting between Hono and React Router. It is composed of a Vite plugin and handlers that enable it to support platforms like Cloudflare Workers and Node.js. You just create Hono app, and it will be applied to your React Router app.
44

55
```ts
66
// server/index.ts
@@ -10,7 +10,7 @@ const app = new Hono()
1010

1111
app.use(async (c, next) => {
1212
await next()
13-
c.header('X-Powered-By', 'Remix and Hono')
13+
c.header('X-Powered-By', 'React Router and Hono')
1414
})
1515

1616
app.get('/api', (c) => {
@@ -26,12 +26,12 @@ This means you can create API routes with Hono's syntax and use a lot of Hono's
2626

2727
> [!WARNING]
2828
>
29-
> `hono-remix-adapter` is currently unstable. The API may be changed without announcement in the future.
29+
> `hono-react-router-adapter` is currently unstable. The API may be changed without announcement in the future.
3030
3131
## Install
3232

3333
```bash
34-
npm i hono-remix-adapter hono
34+
npm i hono-react-router-adapter hono
3535
```
3636

3737
## How to use
@@ -40,12 +40,12 @@ Edit your `vite.config.ts`:
4040

4141
```ts
4242
// vite.config.ts
43-
import serverAdapter from 'hono-remix-adapter/vite'
43+
import serverAdapter from 'hono-react-router-adapter/vite'
4444

4545
export default defineConfig({
4646
plugins: [
4747
// ...
48-
remix(),
48+
reactRouter(),
4949
serverAdapter({
5050
entry: 'server/index.ts',
5151
}),
@@ -73,12 +73,12 @@ To support Cloudflare Workers and Cloudflare Pages, add the adapter in `@hono/vi
7373
```ts
7474
// vite.config.ts
7575
import adapter from '@hono/vite-dev-server/cloudflare'
76-
import serverAdapter from 'hono-remix-adapter/vite'
76+
import serverAdapter from 'hono-react-router-adapter/vite'
7777

7878
export default defineConfig({
7979
plugins: [
8080
// ...
81-
remix(),
81+
reactRouter(),
8282
serverAdapter({
8383
adapter, // Add Cloudflare adapter
8484
entry: 'server/index.ts',
@@ -91,7 +91,7 @@ To deploy your app to Cloudflare Workers, you can write the following handler on
9191

9292
```ts
9393
// worker.ts
94-
import handle from 'hono-remix-adapter/cloudflare-workers'
94+
import handle from 'hono-react-router-adapter/cloudflare-workers'
9595
import * as build from './build/server'
9696
import server from './server'
9797

@@ -113,7 +113,7 @@ To deploy your app to Cloudflare Pages, you can write the following handler on `
113113

114114
```ts
115115
// functions/[[path]].ts
116-
import handle from 'hono-remix-adapter/cloudflare-pages'
116+
import handle from 'hono-react-router-adapter/cloudflare-pages'
117117
import * as build from '../build/server'
118118
import server from '../server'
119119

@@ -122,13 +122,13 @@ export const onRequest = handle(build, server)
122122

123123
## Node.js
124124

125-
If you want to run your app on Node.js, you can use `hono-remix-adapter/node`. Write `main.ts`:
125+
If you want to run your app on Node.js, you can use `hono-react-router-adapter/node`. Write `main.ts`:
126126

127127
```ts
128128
// main.ts
129129
import { serve } from '@hono/node-server'
130130
import { serveStatic } from '@hono/node-server/serve-static'
131-
import handle from 'hono-remix-adapter/node'
131+
import handle from 'hono-react-router-adapter/node'
132132
import * as build from './build/server'
133133
import { getLoadContext } from './load-context'
134134
import server from './server'
@@ -158,19 +158,18 @@ esbuild main.ts --bundle --outfile=main.mjs --platform=node --target=node16.8 --
158158

159159
## `getLoadContext`
160160

161-
If you want to add extra context values when you use Remix routes, like in the following use case:
161+
If you want to add extra context values when you use React Router routes, like in the following use case:
162162

163163
```ts
164164
// app/routes/_index.tsx
165-
import type { LoaderFunctionArgs } from '@remix-run/cloudflare'
166-
import { useLoaderData } from '@remix-run/react'
165+
import type { Route } from './+types/_index'
167166

168-
export const loader = ({ context }) => {
167+
export const loader = (args: Route.LoaderArgs) => {
169168
return { extra: context.extra }
170169
}
171170

172-
export default function Index() {
173-
const { extra } = useLoaderData<typeof loader>()
171+
export default function Index({ loaderData }: Route.ComponentProps) {
172+
const { extra } = loaderData
174173
return <h1>Extra is {extra}</h1>
175174
}
176175
```
@@ -210,7 +209,7 @@ Then import the `getLoadContext` and add it to the `serverAdapter` as an argumen
210209
// vite.config.ts
211210
import adapter from '@hono/vite-dev-server/cloudflare'
212211
import { reactRouter } from '@react-router/dev'
213-
import serverAdapter from 'hono-remix-adapter/vite'
212+
import serverAdapter from 'hono-react-router-adapter/vite'
214213
import { defineConfig } from 'vite'
215214
import { getLoadContext } from './load-context'
216215

@@ -231,7 +230,7 @@ For Cloudflare Workers, you can add it to the `handler` function:
231230

232231
```ts
233232
// worker.ts
234-
import handle from 'hono-remix-adapter/cloudflare-workers'
233+
import handle from 'hono-react-router-adapter/cloudflare-workers'
235234
import * as build from './build/server'
236235
import { getLoadContext } from './load-context'
237236
import app from './server'
@@ -243,7 +242,7 @@ You can also add it for Cloudflare Pages:
243242

244243
```ts
245244
// functions/[[path]].ts
246-
import handle from 'hono-remix-adapter/cloudflare-pages'
245+
import handle from 'hono-react-router-adapter/cloudflare-pages'
247246
import { getLoadContext } from 'load-context'
248247
import * as build from '../build/server'
249248
import server from '../server'
@@ -255,7 +254,7 @@ This way is almost the same as [Remix](https://remix.run/docs/en/main/guides/vit
255254

256255
### Getting Hono context
257256

258-
You can get the Hono context in Remix routes. For example, you can pass the value with `c.set()` from your Hono instance in the `server/index.ts`:
257+
You can get the Hono context in React Router routes. For example, you can pass the value with `c.set()` from your Hono instance in the `server/index.ts`:
259258

260259
```ts
261260
// server/index.ts
@@ -279,14 +278,14 @@ In the React Router route, you can get the context from `args.context.hono.conte
279278

280279
```ts
281280
// app/routes/_index.tsx
282-
import { Router } from "./types/_index"
281+
import { Router } from './types/_index'
283282

284283
export const loader = ({ context }) => {
285284
const message = args.context.hono.context.get('message')
286285
return { message }
287286
}
288287

289-
export default function Index({ loaderData }:Route.ComponentProps) {
288+
export default function Index({ loaderData }: Route.ComponentProps) {
290289
const { message } = loaderData
291290
return <h1>Message is {message}</h1>
292291
}
@@ -364,7 +363,7 @@ app.use(async (c, next) => {
364363
export default app
365364
```
366365

367-
You can retrieve and process the context saved in Hono from Remix as follows:
366+
You can retrieve and process the context saved in Hono from React Router as follows:
368367

369368
```ts
370369
// app/routes/_index.tsx
@@ -377,7 +376,7 @@ export const loader = () => {
377376
}
378377
```
379378

380-
## Auth middleware for Remix routes
379+
## Auth middleware for React Router routes
381380

382381
If you want to add Auth Middleware, e.g. Basic Auth middleware, please be careful that users can access the protected pages with SPA tradition. To prevent this, add a `loader` to the page:
383382

examples/cloudflare-pages/app/entry.client.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* By default, Remix will handle hydrating your app on the client for you.
3-
* You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
4-
* For more information, see https://remix.run/file-conventions/entry.client
5-
*/
6-
71
import { StrictMode, startTransition } from 'react'
82
import { hydrateRoot } from 'react-dom/client'
93
import { HydratedRouter } from 'react-router/dom'

examples/cloudflare-pages/app/entry.server.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* By default, Remix will handle generating the HTTP Response for you.
3-
* You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
4-
* For more information, see https://remix.run/file-conventions/entry.server
5-
*/
6-
71
import { isbot } from 'isbot'
82
import { renderToReadableStream } from 'react-dom/server'
93
import type { AppLoadContext, EntryContext } from 'react-router'

examples/cloudflare-pages/app/routes/_index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Route } from './+types/_index'
2-
import logoDark from '/logo-dark.png?inline'
2+
import logoDark from '/logo.png?inline'
33

44
export const loader = (args: Route.LoaderArgs) => {
55
const extra = args.context.extra
@@ -13,7 +13,7 @@ export default function Index({ loaderData }: Route.ComponentProps) {
1313
const { cloudflare, extra, myVarInVariables, isWaitUntilDefined } = loaderData
1414
return (
1515
<div>
16-
<h1>Remix and Hono</h1>
16+
<h1>React Router and Hono</h1>
1717
<h2>Var is {cloudflare.env.MY_VAR}</h2>
1818
<h3>
1919
{cloudflare.cf ? 'cf,' : ''}
@@ -23,7 +23,7 @@ export default function Index({ loaderData }: Route.ComponentProps) {
2323
<h4>Extra is {extra}</h4>
2424
<h5>Var in Variables is {myVarInVariables}</h5>
2525
<h6>waitUntil is {isWaitUntilDefined ? 'defined' : 'not defined'}</h6>
26-
<img src={logoDark} alt='Remix' />
26+
<img src={logoDark} alt='React Router' />
2727
</div>
2828
)
2929
}

examples/cloudflare-pages/e2e.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ test('Should return 200 response - /', async ({ page }) => {
55
expect(response?.status()).toBe(200)
66

77
const headers = response?.headers() ?? {}
8-
expect(headers['x-powered-by']).toBe('Remix and Hono')
8+
expect(headers['x-powered-by']).toBe('React Router and Hono')
99

1010
const contentH1 = await page.textContent('h1')
11-
expect(contentH1).toBe('Remix and Hono')
11+
expect(contentH1).toBe('React Router and Hono')
1212

1313
const contentH2 = await page.textContent('h2')
1414
expect(contentH2).toBe('Var is My Value')
@@ -25,7 +25,7 @@ test('Should return 200 response - /', async ({ page }) => {
2525
const contentH6 = await page.textContent('h6')
2626
expect(contentH6).toBe('waitUntil is defined')
2727

28-
const imageResponse = await page.goto('/logo-dark.png?inline')
28+
const imageResponse = await page.goto('/logo.png?inline')
2929
expect(imageResponse?.status()).toBe(200)
3030
})
3131

examples/cloudflare-pages/functions/[[path]].ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// functions/[[path]].ts
2-
import handle from 'hono-remix-adapter/cloudflare-pages'
2+
import handle from 'hono-react-router-adapter/cloudflare-pages'
33
import { getLoadContext } from 'load-context'
44
import * as build from '../build/server'
55
import server from '../server'
-15.6 KB
Binary file not shown.
-78.4 KB
Binary file not shown.
12.8 KB
Loading

examples/cloudflare-pages/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const app = new Hono<{
1313
app.use(async (c, next) => {
1414
c.set('MY_VAR_IN_VARIABLES', 'My variable set in c.set')
1515
await next()
16-
c.header('X-Powered-By', 'Remix and Hono')
16+
c.header('X-Powered-By', 'React Router and Hono')
1717
})
1818

1919
app.get('/api', (c) => {

0 commit comments

Comments
 (0)