This guide will help you smoothly migrate your Netlify project to EdgeOne Pages.
Preparations: Search Build Command and Publish Directory
Start by finding your Netlify project's build command and output directory:
1. Log in to the dashboard and find the project to migrate.
2. Enter Site configuration, select the Build and deploy option.
3. In the Build settings section, note the values of Build command and Publish directory.
Example:
npm run build
Publish directory: .next
This information will be used in project configuration.
2.Configuration Migration: Handle Redirects and Headers
If your project used _redirects and _headers files or configured redirects and custom headers in the netlify.toml file, require migration to the edgeone.json file in EdgeOne Pages.
Before migration, we need to understand your existing Netlify configuration and convert it to a format Pages can understand.
The following is a comparison example of both:
Netlify configuration in netlify.toml
[build]
command = "npm run build"
publish = "build"
[[redirects]]
from = "/articles"
to = "/blogs"
status = 301
[[headers]]
source = "/*"
[[headers.headers]]
key = "X-Frame-Options"
value = "DENY"
[[headers.headers]]
key = "Cache-Control"
value = "max-age=7200"
[[headers]]
source = "/assets/*"
[[headers.headers]]
key = "Cache-Control"
value = "max-age=31536000"
In this example, we can see the build command, publish directory, redirect rules and custom headers configuration.
In EdgeOne Pages, we need to create an edgeone.json file to include these configurations:
1.1 Netlify Serverless Functions export async handler by default; Edge Functions export handler by default (can be async) and require explicitly specifying the route path.
1.2 Pages uses named onRequest functions (or onRequestGet, onRequestPost, etc.).
2. Parameters:
2.1 Netlify functions receive request and context as separate parameters.
2.2 Pages function receives a context object.
3. Response method:
3.1 Both use the Response object to return responses, which is similar.
4. Runtime environment:
4.1 Netlify uses Node.js runtime and Deno runtime.
4.2 Pages use Node.js runtime and Edge runtime.
Migration suggestions:
Change the default exported function to a named onRequest function
// Netlify
exportdefaultasync(request, context)=>{
function body
};
// EdgeOne Pages
exportdefaultfunctiononRequest(context){
// function body
}
Adjust parameter usage
// Netlify
const{ geo }= context;
// EdgeOne Pages
const{ request }= context;
const geo = request.eo.geo;
Keep the usage of the Response object. This part basically does not need to change.
Check and adjust any Netlify function-specific API usage. Pages Functions may not support specific features. Need to find alternatives. Contact us through the community.
File naming may need adjusting. Netlify uses .js, .ts, or .edge.js suffixes, while Pages currently supports .js, .ts, .cjs, .jsx, .tsx.
Note:
Pages and Functions support Node Functions and Edge Functions, with some differences in features such as the context object. You can select based on your specific needs.
By completing the above steps, you have successfully migrated your Netlify project to EdgeOne Pages. The two platforms have similarities in some aspects, but on a strong infrastructure, we have optimized Pages product features, such as intelligent refresh preheating, to provide an out-of-the-box experience. In addition, Pages has fewer restrictions compared to competitors during the public beta phase, providing more flexibility for developers. In terms of customer support, we provide more timely responses, committed to providing developers with a high-quality product experience.