npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@resolid/react-router-hono

v1.1.1

Published

React Router Hono Server

Downloads

189

Readme

@resolid/react-router-hono

English

React Router Hono 服务包

功能

安装

pnpm add -D @resolid/react-router-hono

安装相关依赖

pnpm add hono @hono/node-server

开发服务器

修改 vite.config.ts 配置开发服务器

// vite.config.ts

import { reactRouter } from "@react-router/dev/vite";
import { reactRouterHonoServer } from "@resolid/react-router-hono/dev";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  plugins: [
    reactRouterHonoServer({
      // 入口文件
      entryFile: "server.node.ts", // 默认为 server.ts
    }),
    reactRouter(),
    tsconfigPaths(),
  ],
});

Node.js 服务

创建 Node.js 服务

// app/server.node.ts

import { createHonoNodeServer } from "@resolid/react-router-hono/node-server";

export default await createHonoNodeServer();

配置 Node.js Preset

// react-router.config.ts

import type { Config } from "@react-router/dev/config";
import { nodePreset } from "@resolid/react-router-hono/node-preset";

export default {
  presets: [
    nodePreset({
      // 入口文件
      entryFile: "server.node.ts", // 默认为 server.ts
      nodeVersion: 22, // 默认为 22
    }),
  ],
} satisfies Config;

支持 SERVER_PORTSERVER_PATH 环境变量, SERVER_PATH 环境变量用于 React Router 的 serverBundles 功能

运行 build 成功以后自动会在 build/server 目录下生成 server.mjspackage.json 文件, package.json 文件里面定义了 Vite 设置的 ssr.external, 在服务器目录下运行 npm install 即可安装构建时排除的依赖

Vercel 服务

你可以使用 https://github.com/huijiewei/react-router-hono-vercel-template 模版快速部署到 Vercel.

创建 Vercel 服务

// app/server.vercel.ts

import { createHonoVercelServer } from "@resolid/react-router-hono/vercel-server";

export default await createHonoVercelServer();

配置 Vercel Preset

// react-router.config.ts

import type { Config } from "@react-router/dev/config";
import { vercelPreset } from "@resolid/remix-plugins/vercel-preset";

export default {
  presets: [
    vercelPreset({
      // 部署区域
      regions: ["sin1"],
      // 有些包会根据不同平台引入二进制包, 安装的时候二进制包不在同一目录, 比如 @node-rs/bcrypt
      copyParentModules: ["@node-rs/bcrypt"],
      // 入口文件
      entryFile: "server.vercel.ts", // 默认为 server.ts
      nodeVersion: 22, // 默认为 22
    }),
  ],
} satisfies Config;

Vercel 项目 Framework Preset 需设置为 Vite, Node.js Version 需设置为一样

如果你使用的是 monorepo 结构, 请设置 Root Directory 为需要部署的项目目录, 然后自定义相关命令, Resolid 的配置如下图 Vercel相关设置

Hono 中间件

中间件是在 React Router 调用加载器/操作之前调用的函数, 请参阅 Hono 文档 以获取更多信息。

你可以配置 createHonoNodeServer 或者 createHonoVercelServer 的 configure 来方便使用 Hono 中间件

import { createHonoNodeServer } from "@resolid/react-router-hono/node-server";

export default await createHonoNodeServer({
  configure: (server) => {
    server.use(/* Hono 中间件 */);
  },
});

React Router 负载上下文

如果您想向 React Router 加载上下文添加其他属性,你可以配置 createHonoNodeServer 或者 createHonoVercelServer 的 getLoadContext 来增加加载上下文

import { createHonoNodeServer } from "@resolid/react-router-hono/node-server";
import type { HttpBindings } from "@hono/node-server";
import type { Context } from "hono";

export default await createHonoNodeServer({
  getLoadContext: (c: Context<{ Bindings: HttpBindings }>) => {
    return {
      remoteAddress: c.env.incoming.socket.remoteAddress,
    };
  },
});

致谢