Build and route Pages projects

更新时间:
复制 MD 格式

Configure build settings in the esa.jsonc file and customize routing behavior for static assets in Pages projects.

Build Pages with esa.jsonc

Create an esa.jsonc file in the root directory of your GitHub project. This file lets you define and override default project settings.

Benefits

  • Infrastructure as code: Configuration is version-controlled with your code, making every change traceable.

  • Seamless team collaboration: All team members share one configuration file, ensuring consistency between local development and cloud deployment.

  • Convenient rollbacks: Roll back to any historical commit and reproduce its exact build environment.

Configuration priority

esa.jsonc settings take precedence over console settings:

  • If your project does not have an esa.jsonc file:

    • You can adjust the build options on the project's details page. For more information, see Modify build settings in the console.

    • Console settings apply to the current and all subsequent deployments.

  • If an esa.jsonc file exists in your project:

    • The system detects the file and uses it as the sole configuration source.

    • On the project details page, console settings managed by esa.jsonc are ignored.

    • To modify the configuration, edit the esa.jsonc file and push the changes to your GitHub repository.

Configuration example

Add an esa.jsonc file to your Pages repository on GitHub. Example:

{
  "name": "vite-react-template",
  "entry": "./src/index.js",
  "installCommand": "npm install",
  "buildCommand": "npm run build",
  "assets": {
    "directory": "./dist",
    "notFoundStrategy": "singlePageApplication"
  }
}

Parameter

Description

name

The target project for deployment. If the project exists, the deployment applies to it. Otherwise, ESA creates a new project with this name.

entry

Path to the Edge Function entry file, such as ./src/index.ts.

installCommand

Custom installation command, such as npm install. Overrides the console installation command. Leave empty to skip installation. Supported package managers: npm, pnpm, yarn, cnpm, and bun.

buildCommand

Custom build command, such as npm run build. Overrides the console build command. Set to an empty string to skip the build step.

assets

Static asset hosting runs front-end websites on ESA Functions and Pages. Each Pages project supports one asset directory. Options:

  • directory: The build output directory to host statically, such as ./public, ./dist, or ./build.

  • notFoundStrategy: The fallback strategy when a requested path does not match any static asset.

    • singlePageApplication: Returns index.html from the static hosting directory with a 200 OK status code. Use this for single-page applications.

    • 404 Page: Returns 404.html from the static hosting directory with the 404 Not Found status code.

      Note

      If you configure both a function script and assets.notFoundStrategy, navigation requests do not trigger the function script. A navigation request is sent by the browser when a user visits a page directly (typing a URL or clicking a link) and includes the Sec-Fetch-Mode: navigate header.

Route static assets

After you configure esa.jsonc, requested URLs are routed to files in the static asset directory as follows:

Default mode

When notFoundStrategy is not set in esa.jsonc:

{
  "name": "vite-react-template",
  "entry": "./src/index.js",
  "assets": {
    "directory": "./dist"
  }
}

ESA routes requests as follows:

image
  1. When a request URL reaches an edge node, ESA checks for a matching static asset. If found, ESA serves the file directly. If not, proceed to step 2.

  2. If an Edge Routine (ER) function script exists, ESA executes it. Otherwise, a 404 Not Found response is returned.

Single-page application

For single-page applications, set notFoundStrategy to singlePageApplication in esa.jsonc. Example:

{
  "name": "vite-react-template",
  "entry": "./src/index.js",
  "assets": {
    "directory": "./dist",
    "notFoundStrategy": "singlePageApplication"
  }
}

ESA routes requests as follows:

image
  1. When a request URL reaches an edge node, ESA checks for a matching static asset. If found, ESA serves the file directly. If not, proceed to step 2.

  2. Check if the request is a navigation request (contains the Sec-Fetch-Mode: navigate header). If not, proceed to step 3. If so, route to /index.html and proceed to step a:

    1. If /index.html exists, respond with 200 OK and return its content. Otherwise, proceed to step 3.

  3. If an ER function script exists, ESA executes it. Otherwise, a 404 Not Found response is returned.

Static site generation

For static site generation applications, set notFoundStrategy to 404Page in esa.jsonc. Example:

{
  "name": "vite-react-template",
  "entry": "./src/index.js",
  "assets": {
    "directory": "./dist",
    "notFoundStrategy": "404Page"
  }
}

ESA routes requests as follows:

image
  1. When a request URL reaches an edge node, ESA checks for a matching static asset. If found, ESA serves the file directly. If not, proceed to step 2.

  2. Check if the request is a navigation request (contains the Sec-Fetch-Mode: navigate header). If not, proceed to step 3. If so, route to /404.html and proceed to step a:

    1. If /404.html exists, respond with 200 OK and return its content. Otherwise, proceed to step 3.

  3. If an ER function script exists, ESA executes it. Otherwise, a 404 Not Found response is returned.

Modify build settings in the console

  1. Log in to the ESA console. In the left-side navigation pane, choose Edge Computing and AI > Functions and Pages.

  2. On the Functions and Pages page, click the target function or page.

  3. Select the Basic Information tab. In the Build Information section, click Edit.

    image

  4. Update the Build Information settings as needed.

    image

    Parameter

    Description

    Root directory

    The directory where the build command runs. Defaults to /. For a monorepo, enter the subproject path, such as /frontend or /packages/web.

    Static asset directory

    The build output directory for static assets, such as ./public, ./dist, or ./build. You can also set this in the assets.directory field of the esa.jsonc file, which takes precedence over the Build Information console setting.

    Function file path

    Path to the function entry file, such as ./src/index.ts. You can also set this in the entry field of the esa.jsonc file, which takes precedence over the Build Information console setting.

    Node.js version

    The Node.js version for the build. Changes take effect on the next build. You can also set the major version in the engines.node field of package.json, which takes precedence over the Build Information console setting.

    Environment variables

    Environment variables for the build process, accessible through process.env.