Next.js
Next.js is a full-stack framework based on React to build high-performance, scalable Web applications. It simplifies the development process, supports various rendering modes, and is suitable for various project requirements.
Note:
Pages currently supports Next.js versions 13.5+, 14, 15, and 16, with the earliest version being 13.5+.
Core Features
Multiple rendering modes: Supports SSG (static generation), SSR (server-side rendering), ISR (incremental static regeneration), and CSR (client-side rendering), flexibly adapting to static and dynamic scenarios.
File-based routing: Automatically generates routes (Pages Router or App Router) through files and folders structure, simplifying page management.
API routing: Built-in API functionality to easily create backend APIs.
Performance optimization: Automatic code splitting and quick refresh enhance loading speed and development experience.
TypeScript support: Native support for TypeScript to enhance code reliability.
Advantages
Quickly build SEO-friendly apps with superior performance.
Unify backend and frontend development experience and reduce learning cost.
Suitable for a range of scenarios from static blogs to complex dynamic applications.
Quick Start
Start deployment of the Next.js project on EdgeOne Pages:
Import the Next.js project from a Git repository.
Select the Next.js template from the Pages template library.
Use the sample Next.js project.
Support for Next.Js in Pages
Pages supports the earlier version of Next.js Pages Router, but we recommend using App Router.
The following table shows the key characteristics of Next.js currently supported by Pages. The platform will support additional features as soon as possible, but experimental features may not be completely stable yet.
Next.Js Features | Support Status |
App Router | ✓ |
Pages Router | ✓ |
Server-Side Rendering (SSR) | ✓ |
Incremental Static Regeneration (ISR) | ✓ |
Static Site Generation (SSG) | ✓ |
React Server Components | ✓ |
Response Streaming | ✓ |
Route Handlers | ✓ |
Experimental framework features | Partially supported |
Redirects and rewrites | Currently not supported for Next.js rewrite and redirection. The platform recommends using edgeone.json to configure. For more details, see the document. |
Server-Side Rendering (SSR)
Server-side rendering allows you to render webpages on servers dynamically. Every time users initiate requests, the server dynamically obtains data (such as user sessions, query parameters) by using getServerSideProps (Pages Router) or server components in App Router to generate HTML.
Default build settings are as follows:
Build command:
npm run buildOutput directory:
.nextIncremental Static Regeneration (ISR)
Incremental Static Regeneration is an extension of SSG, combining the advantages of SSG and SSR. There is no need to rebuild the entire site when data is updated. ISR brings three key advantages to developers: better performance, higher security, and shorter build time.
To enable ISR during static page generation, you can use the revalidate option in getStaticProps (Pages Router) or App Router. Set revalidate to periodically (such as every 60 seconds) or call revalidatePath as needed to regenerate.
Note:
The revalidatePath method is currently an experimental feature and may not yet be fully stable.
Default build settings are as follows:
Build command:
npm run buildOutput directory:
.nextStatic Site Export (SSG)
If you do not need any dynamic features provided by Next.js, you can use it to generate a fully static site. Configured as static export mode, modify next.config.js as in the following example:
/** @type {import('next').NextConfig} */const nextConfig = {output: 'export', // enable static exportimages: {unoptimized: true // disable image optimization for static export},trailingSlash: true, // add a trailing slash for high compatibility};
Default build settings are as follows:
Build command:
npm run buildOutput directory:
outStreaming Rendering
Pages support streaming rendering through React Server Components (RSC).
With the aid of the Suspense component, page content can be gradually "streaming" transmitted to the client rather than waiting for the entire webpage to be completely rendered before sending it all at once. This can distinctly improve user experience, particularly in complex pages or slow data access situations.
The example code (in page.tsx):
import { Suspense } from 'react';import { PostFeed, Weather } from './Components';export default function Posts() {return (<section><Suspense fallback={<p>Loading post...</p>}><PostFeed /> {/* This component will asynchronously fetch data and stream rendering */}</Suspense><Suspense fallback={<p>Loading weather...</p>}><Weather /></Suspense></section>);}
PostFeed and Weather can independently stream rendering. If one is slow, another won't block.
Middleware
Next.js middleware is code executed before request arrival at a webpage or API routing. It runs in Edge Runtime environment by default, allowing you to intercept requests before completion, perform operations like rewrite, redirection, modify request or response header, and achieve such global general features without intrusion into business logic.
You can use middleware in the following typical usage scenarios:
1. Identity verification and authorization: Check user login status and redirect unlogged-in users to the login page.
2. A/B testing: Direct users to different versions of the webpage based on conditions.
3. Internationalization (i18n): Rewrite to the corresponding language webpage according to the user's language preference.
4. Bot detection and prevention: Identify and block malicious crawlers or bots.
5. Request log and monitoring: Record request information for analysis and debug.
6. Feature Flags: Dynamically control feature visibility based on feature switch.
To create middleware, you can create a
proxy.ts (or .js) file in the project at the same directory level as pages or app. Example code:import { NextResponse } from 'next/server'import type { NextRequest } from 'next/server'// Export the proxy named function, receive request object as parameterexport function proxy(request: NextRequest) {// Example 1: Redirection - Navigate to /home when accessing paths related to /aboutreturn NextResponse.redirect(new URL('/home', request.url))// Example 2: Rewrite - Rewrite /about to /about-new when accessing /about// return NextResponse.rewrite(new URL('/about-new', request.url))// Example 3: Direct response - Block the request and return 403// return new NextResponse('Access denied', { status: 403 })}// Middleware matching configuration: define which requests trigger middlewareexport const config = {matcher: ['/about/:path*', // match /about subpath'/((?!api|_next/static|_next/image|favicon.ico).*)', // exclude API, static resource, match all webpages],}
Note:
Next.js version 16 renames the middleware file from middleware.ts to proxy.ts and changes the function export from middleware to proxy. When using Next.js 16 or higher, please use the proxy.ts writing format.
Pages provides complete support for Next.js middleware. You can easily create and use middleware, with usage methods and grammar consistent with Next.js. For more uses, see Next.js Proxy.
If you need to use middleware in a non-full-stack framework, you can use the common middleware service provided by the platform.
