Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite 6 + Hono sub app + fixes #50

Merged
merged 12 commits into from
Jan 5, 2025
Merged

Vite 6 + Hono sub app + fixes #50

merged 12 commits into from
Jan 5, 2025

Conversation

rphlmr
Copy link
Owner

@rphlmr rphlmr commented Jan 4, 2025

Fixes #49 #44 #47

Basename and Hono sub apps

Note

By default, the React Router app is mounted at / (default basename value).

You may not need to use this option. It's for advanced use cases.

Tip

Check this example to see how to use it.

You can use the basename option in your React Router config (react-router.config.ts) to mount your React Router app on a subpath.

It will automatically mount the app on the subpath.

// react-router.config.ts
import type { Config } from "@react-router/dev/config";

export default {
  basename: "/app", // Now the React Router app will be mounted on /app
} satisfies Config;

Then, you can use the app option in createHonoServer to pass your "root" Hono app. This will be used to mount the React Router app on the basename path.

import { Hono } from "hono";
import { createHonoServer } from "react-router-hono-server/node";
import { API_BASENAME, api } from "./api";
import { getLoadContext } from "./context";

// Create a root Hono app
const app = new Hono();

// Mount the API app at /api
app.route(API_BASENAME, api);

export default await createHonoServer({
  // Pass the root Hono app to the server.
  // It will be used to mount the React Router app on the `basename` defined in react-router.config.ts
  app,
  getLoadContext,
});

Note

You now have two entry points!

  • /api - for your API
  • /app - for your React Router app

Cloudflare custom assets serving

You can set Cloudflare experimental_serve_directly and delegate assets serving to Hono, like for Node and Bun.

Tip

Check this example to see how to use it.

[assets]
directory = "./build/client/"
binding = "ASSETS"
experimental_serve_directly = false

Copy link

pkg-pr-new bot commented Jan 4, 2025

Open in Stackblitz

npm i https://pkg.pr.new/rphlmr/react-router-hono-server@50

commit: e8bc65a

@rphlmr rphlmr changed the title Vite 6 Vite 6 + Hono sub app + fixes Jan 4, 2025
@abn5x
Copy link

abn5x commented Jan 5, 2025

Hello, seems like this fixes the build process, but now all the cache-control max-age is set to 0, in the previous version it was set to 3600

@rphlmr rphlmr mentioned this pull request Jan 5, 2025
@rphlmr
Copy link
Owner Author

rphlmr commented Jan 5, 2025

@abn5x Wow thanks! I think my new dev server regex is too broad and make assets rendered by the vite http server.
Fixing this thanks!

@rphlmr
Copy link
Owner Author

rphlmr commented Jan 5, 2025

CleanShot 2025-01-05 at 10 59 21@2x

Looks like ok in prod. Do you have a screenshot?
Depending on what you use (npm, pnpm), there is sometime a cache issue with pkg.pr.new

@rphlmr
Copy link
Owner Author

rphlmr commented Jan 5, 2025

Oh you may mean Cloudflare? Ok that's really strange, I can reproduce only for it (but It was 0 before too, something to configure in wrangler?)

@rphlmr
Copy link
Owner Author

rphlmr commented Jan 5, 2025

Cloudflare:

You can now set (wrangler) experimental_serve_directly to false and use your Hono server to serve assets and public files. It has the same cache maxAge as Bun and Node.

@rphlmr rphlmr merged commit e048945 into main Jan 5, 2025
5 checks passed
@rphlmr rphlmr deleted the vite-6 branch January 5, 2025 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cloudlfare: TypeError: Cannot read properties of undefined (reading 'routes')
2 participants