Skip to content

Commit 58253e2

Browse files
barakcodesyusukebe
andauthored
feat: Add support for react router 7 (#39)
Adds support for react-router 7. Not sure it warrants a package name change, this could just be a major version update, since all the "remix" apps will need to be updated to react router 7 as the next phase of their lifecyle Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
1 parent 6e61599 commit 58253e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+4664
-8703
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
.wrangler
33
.mf
44
*.tgz
5-
dist
5+
dist
6+
.DS_Store

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# hono-remix-adapter
22

3-
`hono-remix-adapter` is a set of tools for adapting between Hono and Remix. 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 Remix app.
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.
44

55
```ts
66
// server/index.ts
@@ -179,12 +179,12 @@ First, create the `getLoadContext` function and export it:
179179

180180
```ts
181181
// load-context.ts
182-
import type { AppLoadContext } from '@remix-run/cloudflare'
182+
import type { AppLoadContext } from 'react-router'
183183
import type { PlatformProxy } from 'wrangler'
184184

185185
type Cloudflare = Omit<PlatformProxy, 'dispose'>
186186

187-
declare module '@remix-run/cloudflare' {
187+
declare module 'react-router' {
188188
interface AppLoadContext {
189189
cloudflare: Cloudflare
190190
extra: string
@@ -209,15 +209,15 @@ Then import the `getLoadContext` and add it to the `serverAdapter` as an argumen
209209
```ts
210210
// vite.config.ts
211211
import adapter from '@hono/vite-dev-server/cloudflare'
212-
import { vitePlugin as remix } from '@remix-run/dev'
212+
import { reactRouter } from '@react-router/dev'
213213
import serverAdapter from 'hono-remix-adapter/vite'
214214
import { defineConfig } from 'vite'
215215
import { getLoadContext } from './load-context'
216216

217217
export default defineConfig({
218218
plugins: [
219219
// ...
220-
remix(),
220+
reactRouter(),
221221
serverAdapter({
222222
adapter,
223223
getLoadContext,
@@ -275,20 +275,19 @@ app.use(async (c, next) => {
275275
export default app
276276
```
277277

278-
In the Remix route, you can get the context from `args.context.hono.context`:
278+
In the React Router route, you can get the context from `args.context.hono.context`:
279279

280280
```ts
281281
// app/routes/_index.tsx
282-
import type { LoaderFunctionArgs } from '@remix-run/cloudflare'
283-
import { useLoaderData } from '@remix-run/react'
282+
import { Router } from "./types/_index"
284283

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

290-
export default function Index() {
291-
const { message } = useLoaderData<typeof loader>()
289+
export default function Index({ loaderData }:Route.ComponentProps) {
290+
const { message } = loaderData
292291
return <h1>Message is {message}</h1>
293292
}
294293
```
@@ -297,7 +296,7 @@ To enable type inference, config the `load-context.ts` like follows:
297296

298297
```ts
299298
// load-context.ts
300-
import type { AppLoadContext } from '@remix-run/cloudflare'
299+
import type { AppLoadContext } from 'react-router'
301300
import type { Context } from 'hono'
302301
import type { PlatformProxy } from 'wrangler'
303302

@@ -309,7 +308,7 @@ type Env = {
309308

310309
type Cloudflare = Omit<PlatformProxy, 'dispose'>
311310

312-
declare module '@remix-run/cloudflare' {
311+
declare module 'react-router' {
313312
interface AppLoadContext {
314313
cloudflare: Cloudflare
315314
hono: {

examples/cloudflare-pages/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ test-results
66
/build
77
.env
88
.dev.vars
9+
.react-router
910

1011
.wrangler

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* For more information, see https://remix.run/file-conventions/entry.client
55
*/
66

7-
import { RemixBrowser } from '@remix-run/react'
87
import { StrictMode, startTransition } from 'react'
98
import { hydrateRoot } from 'react-dom/client'
9+
import { HydratedRouter } from 'react-router/dom'
1010

1111
startTransition(() => {
1212
hydrateRoot(
1313
document,
1414
<StrictMode>
15-
<RemixBrowser />
15+
<HydratedRouter />
1616
</StrictMode>
1717
)
1818
})

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
* For more information, see https://remix.run/file-conventions/entry.server
55
*/
66

7-
import type { AppLoadContext, EntryContext } from '@remix-run/cloudflare'
8-
import { RemixServer } from '@remix-run/react'
97
import { isbot } from 'isbot'
108
import { renderToReadableStream } from 'react-dom/server'
9+
import type { AppLoadContext, EntryContext } from 'react-router'
10+
import { ServerRouter } from 'react-router'
1111

1212
export default async function handleRequest(
1313
request: Request,
1414
responseStatusCode: number,
1515
responseHeaders: Headers,
16-
remixContext: EntryContext,
16+
reactRouterContext: EntryContext,
1717
// This is ignored so we can keep it in the template for visibility. Feel
1818
// free to delete this parameter in your app if you're not using it!
1919
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2020
loadContext: AppLoadContext
2121
) {
2222
const body = await renderToReadableStream(
23-
<RemixServer context={remixContext} url={request.url} />,
23+
<ServerRouter context={reactRouterContext} url={request.url} />,
2424
{
2525
signal: request.signal,
2626
onError(error: unknown) {

examples/cloudflare-pages/app/root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Outlet, Scripts } from '@remix-run/react'
1+
import { Outlet, Scripts } from 'react-router'
22

33
export function Layout({ children }: { children: React.ReactNode }) {
44
return (
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { RouteConfig } from '@react-router/dev/routes'
2+
import { flatRoutes } from '@react-router/fs-routes'
3+
4+
export default flatRoutes() satisfies RouteConfig

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import type { LoaderFunctionArgs } from '@remix-run/cloudflare'
2-
import { useLoaderData } from '@remix-run/react'
1+
import type { Route } from './+types/_index'
32
import logoDark from '/logo-dark.png?inline'
43

5-
export const loader = (args: LoaderFunctionArgs) => {
4+
export const loader = (args: Route.LoaderArgs) => {
65
const extra = args.context.extra
76
const cloudflare = args.context.cloudflare
87
const myVarInVariables = args.context.hono.context.get('MY_VAR_IN_VARIABLES')
98
const isWaitUntilDefined = !!cloudflare.ctx.waitUntil
109
return { cloudflare, extra, myVarInVariables, isWaitUntilDefined }
1110
}
1211

13-
export default function Index() {
14-
const { cloudflare, extra, myVarInVariables, isWaitUntilDefined } = useLoaderData<typeof loader>()
12+
export default function Index({ loaderData }: Route.ComponentProps) {
13+
const { cloudflare, extra, myVarInVariables, isWaitUntilDefined } = loaderData
1514
return (
1615
<div>
1716
<h1>Remix and Hono</h1>

examples/cloudflare-pages/load-context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { AppLoadContext } from '@remix-run/cloudflare'
21
import type { Context } from 'hono'
2+
import type { AppLoadContext } from 'react-router'
33
import type { PlatformProxy } from 'wrangler'
44

55
type Env = {
@@ -13,7 +13,7 @@ type Env = {
1313

1414
type Cloudflare = Omit<PlatformProxy<Env['Bindings']>, 'dispose'>
1515

16-
declare module '@remix-run/cloudflare' {
16+
declare module 'react-router' {
1717
interface AppLoadContext {
1818
cloudflare: Cloudflare
1919
extra: string

examples/cloudflare-pages/package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44
"sideEffects": false,
55
"type": "module",
66
"scripts": {
7-
"build": "remix vite:build",
7+
"build": "react-router build",
88
"deploy": "npm run build && wrangler pages deploy",
9-
"dev": "remix vite:dev",
9+
"dev": "react-router dev",
1010
"start": "wrangler pages dev ./build/client",
11-
"typecheck": "tsc",
11+
"typecheck": "react-router typegen && tsc",
1212
"preview": "npm run build && wrangler pages dev",
1313
"test:e2e:vite": "playwright test -c playwright-vite.config.ts e2e.test.ts",
14-
"test:e2e:pages": "npm run build && playwright test -c playwright-pages.config.ts e2e.test.ts"
14+
"test:e2e:pages": "npm run build && playwright test -c playwright-pages.config.ts e2e.test.ts",
15+
"typegen": "react-router typegen"
1516
},
1617
"dependencies": {
17-
"@remix-run/cloudflare": "^2.14.0",
18-
"@remix-run/react": "^1.19.3",
18+
"@react-router/cloudflare": "^7.0.1",
19+
"@react-router/fs-routes": "^7.0.1",
1920
"hono": "^4.5.11",
2021
"isbot": "^4.1.0",
2122
"react": "^18.3.1",
22-
"react-dom": "^18.3.1"
23+
"react-dom": "^18.3.1",
24+
"react-router": "^7.0.1"
2325
},
2426
"devDependencies": {
2527
"@hono/vite-dev-server": "^0.16.0",
2628
"@playwright/test": "^1.48.2",
27-
"@remix-run/dev": "^2.14.0",
29+
"@react-router/dev": "^7.0.1",
2830
"@types/react": "^18.2.20",
2931
"@types/react-dom": "^18.2.7",
3032
"playwright": "^1.47.0",

0 commit comments

Comments
 (0)