English edition updated

Accelerating a Next.js app globally with functions and a CDN

  • #Next.js
  • #Serverless
  • #DCDN
  • #Deployment

I'd like to share a practical topic: how to deploy Next.js applications built with Vibe Coding, and how to accelerate them globally.

These types of applications typically come in three forms:

  1. Purely static applications: These only have front-end pages, no database required, and no server-side logic.
  2. Frontend → Supabase (SDK / REST): The browser reads and writes data through Supabase's client SDK or REST interface, with RLS responsible for data permissions.
  3. Frontend → Next.js Server-side → Subbase / External Services: The Next.js server can handle more complex permissions and business logic through APIs, SSRs, Server Components, or Server Actions.

The deployment methods for the three are different. Purely static applications can be placed directly on CDN or edge nodes; if it is just simple data reading and writing, the browser can connect directly to Supabase; if the application needs to hide the server key, execute complex business logic, or generate pages on the server, the requests need to go through the Next.js server.

Next.js Three Deployment Architectures

The phrase "via Next.js server" here does not necessarily imply the existence of a traditional API. SSR or Server Components can directly read the database on the server and generate the page; Route Handlers or Server Actions are more commonly used for read and write operations triggered by browser actions. They are often used in combination, but they solve different problems.

There is another point that needs special attention in the third type: the server should be as close to the database as possible.

Since the Next.js server handles API, SSR, and Server Action tasks, it frequently accesses the database and other external services. It's not feasible to deploy the server overseas for global access while keeping the database in mainland China. Especially when a single request involves multiple round trips between the server and the database, cross-regional latency will accumulate, ultimately resulting in a slower experience for the user.

Therefore, when choosing a server deployment region, you should first consider where the database is located, and then where the users are primarily located. DCDN can accelerate the process from user to edge node, but it cannot eliminate long-distance calls between the server and the database.

1. What should be noted when directly connecting the front end to Supabase?

The code in the browser is on the same side as the user, and the user can see the requests sent by the front end. Therefore, when the browser connects directly to Supabase, security cannot be based on the assumption that "the user cannot see the interface".

However, direct browser connection to Supabase does not equate to inherent insecurity. Supabase's anon key can be stored on the front end; the actual data isolation is handled by the database's RLS policy and user permissions. Adding, modifying, and deleting operations can also be performed securely via RLS, but the rules must be configured correctly.

If you still need to hide the service-role key, call private third-party APIs, execute complex business rules, or do not want to expose the data processing process in the browser, then the server-side logic of Next.js should handle the request and complete the authentication, permission judgment and database operation on the server side.

2. Running Next.js using a function service.

I recently tried Volcano Engine's function service. Its idea is to offload the application code to the cloud, where it runs in a container and provides a publicly accessible domain name.

In this deployment method, the front-end pages and server-side logic of Next.js can be hosted by the same application. You can also set instance strategies for functions:

  1. Dynamic instances: Instances are started only when a request arrives. This is less costly, but may result in a cold start.
  2. Reserved instances: Start a fixed number of instances in advance, which results in faster response times, but will continuously consume resources.

I initially thought that reserving instances would definitely be more expensive, but I later discovered that this isn't entirely true. Because scheduling costs are lower when resources are locked in advance, reserving instances may actually be more cost-effective in scenarios with high traffic.

If it's just a development environment with low traffic, you can use dynamic instances; if the traffic is relatively stable, or you are concerned about the response speed of the first request, you can consider reserving instances.

Furthermore, regardless of whether it's a dynamic instance or a reserved instance, each instance has its own concurrency configuration. The platform might provide a default concurrency value of 100, but don't directly copy this default value. This is because the memory and CPU usage of a default instance are usually not high. Under high concurrency, Next.js's SSR or API can cause multiple requests to compete for the same instance's resources, easily leading to queuing, timeouts, or errors.

In this scenario, it's advisable to initially set the single-instance concurrency lower, and then gradually increase it by conducting load tests on CPU, memory, response time, and error rate. The number of instances and single-instance concurrency are two different parameters, both of which need to be adjusted based on the actual load.

Third, confirm the Node.js version supported by the platform.

Different service providers have different requirements for the runtime environment of Next.js. You cannot deploy it to any platform just because it can run on a local project.

Taking Volcano Engine as an example, the current official deployment documentation requires a local Node.js 20.x installation and selection of the Native Node.js 20.x runtime. Higher versions should not be used lightly; at least confirm the current runtime support range of the platform and the compatibility of Next.js and its dependencies.

If using the veFaaS CLI for deployment, the official process is as follows: the CLI detects the framework, build commands, artifact paths, and startup commands in the project directory, then proceeds with dependency installation and the build process, and finally deploys the build results to the cloud. In other words, for this CLI deployment path, the build primarily occurs locally; the cloud is responsible for receiving and running the deployment artifacts, rather than handing over the complete source code to the cloud for temporary building.

Therefore, it's best to ensure your local Node.js version is consistent with the platform runtime before deployment. I highly recommend using it in conjunction with the veFaaS CLI and its corresponding Skill: the CLI handles the actual execution, while the Skill provides runtime suggestions, build configurations, and common troubleshooting paths.

IV. Why is it necessary to add a DCDN layer in front of the function service?

Function services address "where the code runs," but they are not synonymous with global acceleration. User requests may still require accessing function instances across long distances.

One approach is to connect DCDN in front of the function service, using it as a unified access point. This allows static resources to be accessed as much as possible on edge nodes, while forwarding dynamic requests back to the function service.

But simply attaching DCDN is not enough; the key is to configure caching and conditional routing.

Static resources: Cache them as much as possible.

Static resources generated by Next.js can typically be cached for a long time, such as _next resources under a specific path. These files usually contain versioning or hash information, and their URLs change as the content changes, making them suitable for CDN caching.

Dynamic interfaces: Do not cache by default.

Login status, user information, and database query results can all be dynamic content, especially for /api/ interfaces under [specific API name], and static caching rules should generally not be applied directly.

Conditional routing can differentiate between static resources and dynamic requests: static resources are routed through the cache, while APIs and other dynamic pages are routed back to the function service. Specific rules must be validated based on your own routing structure; do not blindly configure them based solely on path names.

V. Several configurations in DCDN worth checking

HTTP/2

HTTP/2 can help handle multiple requests concurrently over the same connection, which is helpful for dynamic requests and pages containing multiple resources.

Gzip

Gzip can compress text resources such as HTML, CSS, and JavaScript, usually with significant benefits and minimal performance loss, so it is generally recommended to enable it.

HTTPS

HTTPS is a basic requirement for production environments. In addition to protecting transmitted content, many capabilities of modern browsers also rely on secure connections.

VI. Acceleration Range of DCDN

DCDN typically offers three acceleration options: Mainland China, Overseas, and Global. The choice depends on user distribution, not simply the region where the function service is located. If you lack a domain name that can be registered in China, choose Overseas acceleration.

Conclusion: What scenarios are suitable for Function Services + DCDN?

If your Next.js application requires server-side execution (such as SSR, Route Handler, or Server Action), or needs the server to securely connect to a database, then "Function Service + DCDN" is a relatively intuitive solution:

  • The function service is responsible for running Next.js and server-side logic;
  • DCDN is responsible for static resource caching and network acceleration;
  • Conditional routing is responsible for distinguishing between static and dynamic requests;
  • Instance strategy is responsible for making trade-offs between response speed and cost.

After the site is deployed, I also recommend running a performance analysis test using Chrome DevTools to see if there are any areas for optimization in terms of initial screen load, static resources, network requests, and script execution. Deployment is not the end of performance optimization; actual access data and browser analysis results are the basis for the next round of adjustments.