deployEdit this page

Adapter API

By using the Adapter API available in the @lazarv/react-server-adapter-core package, you can easily create a deployment adapter for any deployment target.

Define the adapter handler

The adapter handler is a function that you need to implement to handle the deployment of your application. It will be called by the build process and it will receive the information needed to prepare the application for deployment.

An example of an adapter handler implementation can be found in the Vercel adapter implementation.

You can start by copying the Vercel adapter implementation and then modify it to fit your needs.

You need to export the adapter handler function from the file and then use the createAdapter function to create the adapter instance.

You also need to export default a function that will be used to create the adapter instance when adapter options are provided by the user in the react-server.config.js file.

import { createAdapter } from "@lazarv/react-server-adapter-core";

export const adapter = createAdapter({
  name: "Vercel",
  outDir: ".vercel",
  outStaticDir: "static",
  handler: async ({ adapterOptions, files, copy, config, reactServerDir, reactServerOutDir, root, options }) => {
    // Your adapter handler implementation
  },
  deploy: {
    command: "vercel",
    args: ["deploy", "--prebuilt"],
  },
});

export default function defineConfig(adapterOptions) {
  return async (_, root, options) => adapter(adapterOptions, root, options);
}

You need to pass adapter properties to the createAdapter function to configure the adapter. These properties are:

name: The name of the adapter.

outDir: The directory where the adapter will output the deployment configuration.

outStaticDir: The directory where the static files will be output. This is optional. When provided, the adapter will copy the static files to the output directory.

handler: The adapter handler function.

deploy: The deployment command and arguments. This is optional. When provided, the adapter will show what command the developer needs to run to deploy the application after it has been built. If the --deploy flag is provided during the build, the adapter will run this command.

Adapter handler

The adapter handler function will receive the following properties:

The files object contains the following functions:

const staticFiles = await files.static();

The copy object contains the following functions:

await copy.server(outServerDir);

Helper functions

banner

Shows a banner in the console.

banner("building serverless functions");

message

Shows a message in the console. Primary and secondary colors used to show the action and the message.

message("creating", "index.func module");

success

Shows a success message in the console.

success("index.func serverless function initialized.");

clearDirectory

Clears a directory.

await clearDirectory(outServerDir);

writeJSON

Writes a JSON file.

await writeJSON(join(outServerDir, ".vc-config.json"), {
  runtime: "nodejs20.x",
  handler: "index.mjs",
  launcherType: "Nodejs",
  shouldAddHelpers: true,
  supportsResponseStreaming: true,
  ...adapterOptions?.serverlessFunctions?.index,
});