middleware

Handle the communication between the client and the server.

This function handles the SDK logic between the client and the server. It is equivalent to calling withCroct with no arguments.

Signature

This function has the following signature:

function middleware(request: NextRequest, event: NextFetchEvent) => NextMiddlewareResult | Promise<NextMiddlewareResult>

Example

Here is an example of how to use this middleware:

middleware.js
1
export {config, middleware} from '@croct/plug-next/middleware';

The config constant is a convenience export defining a matcher that targets only page routes, excluding API routes, static assets, and other non-page resources such as images and the favicon.

The constant is defined as follows:

export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'^(?!/(api|_next/static|_next/image|favicon.ico)).*',
],
};