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:
Currently, Makers supports Next.js versions 13.5+, 14, 15, and 16, with 13.5+ being the minimum supported version.
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 (Makers Router or App Router) based on file and folder structures, 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 deploying your Next.js project on EdgeOne Makers:
Import the Next.js project from a Git repository.
Select a Next.js template from the Makers template library.
Use the sample Next.js project.
Makers Support for Next.js
Makers supports the legacy Makers Router for Next.js, but using the App Router is still recommended.
The following table lists the key Next.js features currently supported by Makers. The platform will support more features as soon as possible, but experimental features may not be fully stable yet.
Next.Js Features | Support Status |
App Router | ✓ |
Makers Router | ✓ |
Server-Side Rendering (SSR) | ✓ |
Incremental Static Regeneration (ISR) | ✓ |
Static Site Generation (SSG) | ✓ |
React Server Components | ✓ |
Response Streaming | ✓ |
Middleware | ✓ |
Route Handlers | ✓ |
Image Optimization | ✓ |
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 dynamically render pages on the server. Each time a user makes a request, the server dynamically generates HTML by using getServerSideProps (Makers Router) or server components in the App Router to retrieve data such as user sessions and query parameters.
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.
ISR has two trigger methods:
Time-based regeneration: Pages are automatically regenerated in the background at set intervals.
On-demand regeneration: Page regeneration is explicitly triggered via an API call.
Scheduled Regeneration
Time-based regeneration automatically expires page caches at specified intervals. When a visitor requests a page after its cache has expired, EdgeOne returns the old version while triggering a regeneration in the background.
Example code:
In the App Router, you can enable ISR by exporting the revalidate route segment configuration.
// Revalidate every 60 seconds.export const revalidate = 60;export default async function BlogPage() {const res = await fetch('https://api.example.com/posts');const posts = await res.json();return (<ul>{posts.map((post) => (<li key={post.id}>{post.title}</li>))}</ul>);}
On-Demand Regeneration
On-demand regeneration allows you to clear the cache of an ISR page at any time, without waiting for the scheduled interval to expire. It is suitable for scenarios where content changes due to external events, such as a CMS publish or a Webhook notification.
Example code:
In the App Router, you can trigger on-demand regeneration using revalidatePath or revalidateTag.
import { revalidatePath } from 'next/cache';export async function POST(request: Request) {const { secret, path } = await request.json();if (secret !== process.env.REVALIDATION_SECRET) {return Response.json({ message: 'Invalid secret' }, { status: 401 });}revalidatePath(path);return Response.json({ revalidated: true, now: Date.now() });}
Static 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
Makers supports using 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:
In Next.js version 16, the middleware file is renamed from middleware.ts to proxy.ts, and the function export is changed from middleware to proxy. When using Next.js version 16 or later, use the proxy.ts approach.
Makers provides comprehensive support for Next.js middleware. You can easily create and use middleware, with its usage and syntax consistent with Next.js. For more usage, refer to 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.
Image Optimization
Makers supports image optimization for the Next.js
next/image component by default, with zero configuration required.When deployed to Makers, images are automatically optimized, including automatic scaling, quality compression, and format conversion (currently only WebP is supported), thereby reducing image size and accelerating page loading speed.
Example code:
Use the
next/image component directly. import Image from 'next/image'export default function Page() {return (<Imagesrc="/photo.jpg"alt="example image"width={500}height={500}quality={75}// Default is 75/>)}
If you do not need the image optimization feature, you can disable it in the following two ways:
1. In
next.config.js, set unoptimized: true, and ALL <Image> components will use the original image for direct use.// next.config.jsmodule.exports = {images: {unoptimized: true,},}
2. Add the
unoptimized prop to specific images:<Imagesrc="/photo.jpg"alt="example image"width={500}height={500}unoptimized/>
