This documentation is still a work in progress. Some parts might be missing or incomplete.
This is also mostly for advanced use cases (Overriding some default, provide support for another Cloud provider, creating your own IAC component ...), most people won't need to use this.
General
- For the node runtime, you need at least Node 18.
- For the edge runtime, you can use both Node 18+ or cloudflare workers with
node_compat
flag enabled (Cloudflare workers support is experimental) - Open-next doesn't work well on Windows. We recommend using WSL2 or a Linux VM.
Backends
Every backend is a separate module. The following backends are available:
- Servers (Node or Edge runtime)
- Middleware (If manually set to external)
- Image Optimization
- Revalidation
- Warmer
- Initializer
All these backends can be overrided to fit your needs. They share some common ground for the configuration and the way they are used.
Common Overrides
Every custom overrides (not string
) share a BaseOverride
types and needs to be wrapped this way:
type BaseOverride = {
name: string;
};
// This is the type of the override
type LazyLoadedOverride<T extends BaseOverride> = () => Promise<T>;
Wrapper
The wrapper is the main entrypoint for the backend. This is the type of the wrapper:
type WrapperHandler<
E extends BaseEventOrResult = InternalEvent,
R extends BaseEventOrResult = InternalResult,
> = (
handler: OpenNextHandler<E, R>,
converter: Converter<E, R>,
) => Promise<(...args: any[]) => any>;
export type Wrapper<
E extends BaseEventOrResult = InternalEvent,
R extends BaseEventOrResult = InternalResult,
> = BaseOverride & {
wrapper: WrapperHandler<E, R>;
supportStreaming: boolean;
};
The handler
is the original handler that is being wrapped. The converter
is the converter that is being used to convert the event and the result.
Here is a list of the provided wrappers:
aws-lambda
- The default AWS Lambda wrapper - It is the default wrapperaws-lambda-streaming
- The AWS Lambda wrapper with streaming supportnode
- The Node wrapper - Create a node server, not suitable for serverlesscloudflare
- The Cloudflare wrapper - For Cloudflare Worker
Aws Lambda streaming
If you want to enable streaming in lambda, you need to use this wrapper. It is not enabled by default.
Be aware that you might encounter some unexpected behaviors when using streaming. Aws Lambda seems to apply some buffering on the response. In some rare cases it might mean that streaming won't properly start. This is an issue with the lambda runtime itself, but this should only impact TTFB (Time To First Byte) in those cases. See this github issue (opens in a new tab)
Converter
The converter is used to convert the event and the result. This is the type of the converter:
export type Converter<
E extends BaseEventOrResult = InternalEvent,
R extends BaseEventOrResult = InternalResult,
> = BaseOverride & {
convertFrom: (event: any) => Promise<E>;
convertTo: (result: R, originalRequest?: any) => any;
};
The convertFrom
method is used to convert the event to the internal event used by the backend internal handler. The convertTo
method is used to convert the result to the expected result.
convertTo
can usually be ignored when using streaming as the result is being streamed directly.
Here is a list of the provided converters:
aws-apigw-v2
- The default AWS API Gateway v2 converter - It is the default converteraws-apigw-v1
- The AWS API Gateway v1 converteraws-cloudfront
- The AWS CloudFront converter - Used for lambda@edgeedge
- The Edge converter - Converts fromRequest
and toResponse
node
- The Node converter - Converts fromIncomingMessage
and toServerResponse
sqs-revalidate
- The SQS Revalidate converter - Used by default for the revalidation backenddummy
- The Dummy converter - Does nothing