服务器包

服务器包

¥Server Bundles

这是一项专为托管服务提供商集成而设计的高级功能。当你将应用编译成多个服务器包时,需要在应用前面有一个自定义路由层,将请求定向到正确的包。

Remix 通常将你的服务器代码构建到一个 bundle 中,该 bundle 公开一个请求处理函数。然而,在某些情况下,你可能需要将路由树拆分为多个服务器包,每个服务器包都公开一个用于路由子集的请求处理函数。为了提供这种级别的灵活性,Remix Vite 插件 支持 serverBundles 选项,该选项用于将路由分配给不同的服务器包。

¥Remix typically builds your server code into a bundle that exposes a single request handler function. However, there are some scenarios where you might want to split your route tree into multiple server bundles that expose a request handler function for a subset of routes. To provide this level of flexibility, the Remix Vite plugin supports an serverBundles option which is a function for assigning routes to different server bundles.

提供的 serverBundles 函数会被树中的每条路由调用(不可寻址的路由除外,例如无路径布局路由),并返回你想要分配给它的服务器包 ID。这些 bundle ID 将用作服务器构建目录中的目录名。

¥The provided serverBundles function is called for each route in the tree (except for routes that aren't addressable, e.g., pathless layout routes) and returns a server bundle ID that you'd like to assign it to. These bundle IDs will be used as directory names in your server build directory.

对于每个路由,此函数都会传递一个指向并包含该路由的路由数组,称为路由 branch。这允许你为路由树的不同部分创建服务器包。例如,你可以使用它来创建一个单独的服务器包,其中包含特定布局路由内的所有路由:

¥For each route, this function is passed an array of routes leading to and including that route, referred to as the route branch. This allows you to create server bundles for different portions of the route tree. For example, you could use this to create a separate server bundle containing all routes within a particular layout route:

import { vitePlugin as remix } from "@remix-run/dev";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    remix({
      serverBundles: ({ branch }) => {
        const isAuthenticatedRoute = branch.some((route) =>
          route.id.split("/").includes("_authenticated")
        );

        return isAuthenticatedRoute
          ? "authenticated"
          : "unauthenticated";
      },
    }),
  ],
});

branch 数组中的每个 route 包含以下属性:

¥Each route in the branch array contains the following properties:

  • id — 此路由的唯一 ID,命名方式与 file 类似,但相对于应用目录,且不带扩展名,例如 app/routes/gists.$username.tsxid 将是 routes/gists.$username

    ¥id — The unique ID for this route, named like its file but relative to the app directory and without the extension, e.g. app/routes/gists.$username.tsx will have an id of routes/gists.$username.

  • path — 此路由用于匹配 URL 路径名的路径。

    ¥path — The path this route uses to match on the URL pathname.

  • file — 此路由入口点的绝对路径。

    ¥file — The absolute path to the entry point for this route.

  • index — 此路由是否为索引路由。

    ¥index — Whether this route is an index route.

构建清单

¥Build manifest

构建完成后,Remix 将调用 Vite 插件的 buildEnd 钩子,并传递一个 buildManifest 对象。如果你需要检查构建清单以确定如何将请求路由到正确的服务器包,这将非常有用。

¥When the build is complete, Remix will call the Vite plugin's buildEnd hook passing a buildManifest object. This is useful if you need to inspect the build manifest to determine how to route requests to the correct server bundle.

import { vitePlugin as remix } from "@remix-run/dev";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    remix({
      // ...
      buildEnd: async ({ buildManifest }) => {
        // ...
      },
    }),
  ],
});

使用服务器包时,构建清单包含以下属性:

¥When using server bundles, the build manifest contains the following properties:

  • serverBundles — 一个将 bundle ID 映射到 bundle 的 idfile 的对象。

    ¥serverBundles — An object that maps bundle IDs to the bundle's id and file.

  • routeIdToServerBundleId — 一个将路由 ID 映射到其服务器 bundle ID 的对象。

    ¥routeIdToServerBundleId — An object that maps route IDs to its server bundle ID.

  • routes — 路由清单,将路由 ID 映射到路由元数据。这可用于在 Remix 请求处理程序之前驱动自定义路由层。

    ¥routes — A route manifest that maps route IDs to route metadata. This can be used to drive a custom routing layer in front of your Remix request handlers.

或者,你可以在 Vite 插件上启用 manifest 选项,将此构建清单对象作为 .remix/manifest.json 写入构建目录中的磁盘。

¥Alternatively, you can enable the manifest option on the Vite plugin to write this build manifest object to disk as .remix/manifest.json in your build directory.

Remix v2.17 中文网 - 粤ICP备13048890号