Optional Cache Interception

By default OpenNext override the cache system of Next.js to store the cache in S3(Or any other incremental cache provider defined in config) using cacheHandler from next.config.js (opens in a new tab). It also uses a queue system to handle ISR revalidation.

This is great but it still has go through NextServer which by default will load the js associated with the page even if the page is cached. This is not ideal for cold start, and makes it impossible to serve ISR/SSG pages inside an external middleware.

Since OpenNext 3.1, we have added a new feature that we call Cache Interception that allows you to intercept the cache system directly inside OpenNext routing layer and serve the page directly from the cache without going through NextServer. If the cache interception fail, the request will be forwarded to the NextServer as usual.

Enabling this alongside external middleware means that the external middleware will need to have all the proper permissions and env variable to interact with S3, DynamoDB and SQS (Or whatever you override it with). You can see here for more details.

This has the following benefits:

  • Faster cold start (No need to load the js associated with the page if the page is cached)
  • ISR/SSG route can be served directly from an external middleware
  • If the external middleware is in front of the CDN, the middleware can be used for every route including ISR/SSG
  • This will allow PPR (Partial Prerendering) to work as intended and demonstrated by Vercel.¹ This is not implemented yet.

To enable cache interception, you need to add enableCacheInterception option in the open-next.config.ts file.

// open-next.config.ts
import type { OpenNextConfig } from 'open-next/types/open-next';
const config = {
  default: {
  },
  dangerous: {
    enableCacheInterception: true,
  },
} satisfies OpenNextConfig;
 
export default config;
 
  1. PPR outside of vercel does not work in the way they demonstrated. The reason is that the cache system is handled by NextServer and it has to reach the server every single time. PPR pages cannot be served from the CDN when using next start or the standalone ouput as of now. In some cases PPR without vercel can be slower than SSR (especially if your cache is not using the default filesystem).