Skip to content

Commit

Permalink
feat: add support for lambda streaming (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
jadenv authored Jul 31, 2024
1 parent 55ae485 commit d7d2556
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
61 changes: 61 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/Nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export interface NextjsProps {
* could be important for some users.
*/
readonly skipFullInvalidation?: boolean;
/**
* Streaming allows you to send data to the client as it's generated
* instead of waiting for the entire response to be generated.
*/
readonly streaming?: boolean;
}

/**
Expand Down Expand Up @@ -160,6 +165,7 @@ export class Nextjs extends Construct {
environment: props.environment,
quiet: props.quiet,
skipBuild: props.skipBuild,
streaming: props.streaming,
...props.overrides?.nextjs?.nextjsBuildProps,
});

Expand Down Expand Up @@ -205,6 +211,7 @@ export class Nextjs extends Construct {
nextjsPath: props.nextjsPath,
basePath: props.basePath,
distribution: props.distribution,
streaming: props.streaming,
staticAssetsBucket: this.staticAssets.bucket,
nextBuild: this.nextBuild,
nextDomain: this.domain,
Expand Down
7 changes: 6 additions & 1 deletion src/NextjsBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export interface NextjsBuildProps {
* @see {@link NextjsProps.skipBuild}
*/
readonly skipBuild?: NextjsProps['skipBuild'];
/**
* @see {@link NextjsProps.streaming}
*/
readonly streaming?: NextjsProps['streaming'];
}

/**
Expand Down Expand Up @@ -142,7 +146,8 @@ export class NextjsBuild extends Construct {

private build() {
const buildPath = this.props.buildPath ?? this.props.nextjsPath;
const buildCommand = this.props.buildCommand ?? 'npx open-next@^2 build';
const defaultBuildCommand = `npx open-next@^2 build ${this.props.streaming ? '--streaming' : ''}`;
const buildCommand = this.props.buildCommand ?? defaultBuildCommand;
// run build
if (!this.props.quiet) {
console.debug(`Running "${buildCommand}" in`, buildPath);
Expand Down
11 changes: 9 additions & 2 deletions src/NextjsDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as origins from 'aws-cdk-lib/aws-cloudfront-origins';
import { HttpOriginProps } from 'aws-cdk-lib/aws-cloudfront-origins';
import { PolicyStatement, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { Runtime, InvokeMode } from 'aws-cdk-lib/aws-lambda';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { NEXTJS_BUILD_DIR, NEXTJS_STATIC_DIR } from './constants';
Expand Down Expand Up @@ -89,6 +89,10 @@ export interface NextjsDistributionProps {
* Must be provided if you want to serve static files.
*/
readonly staticAssetsBucket: s3.IBucket;
/**
* @see {@link NextjsProps.streaming}
*/
readonly streaming?: boolean;
}

/**
Expand Down Expand Up @@ -256,7 +260,10 @@ export class NextjsDistribution extends Construct {
}

private createServerBehaviorOptions(): cloudfront.BehaviorOptions {
const fnUrl = this.props.serverFunction.addFunctionUrl({ authType: this.fnUrlAuthType });
const fnUrl = this.props.serverFunction.addFunctionUrl({
authType: this.fnUrlAuthType,
invokeMode: this.props.streaming ? InvokeMode.RESPONSE_STREAM : InvokeMode.BUFFERED,
});
const origin = new origins.HttpOrigin(Fn.parseDomainName(fnUrl.url), this.props.overrides?.serverHttpOriginProps);
const serverBehaviorOptions = this.props.overrides?.serverBehaviorOptions;

Expand Down
4 changes: 4 additions & 0 deletions src/generated-structs/OptionalNextjsBuildProps.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/generated-structs/OptionalNextjsDistributionProps.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d7d2556

Please sign in to comment.