Skip to content

Commit

Permalink
external middleware support splitted fn rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
conico974 committed Dec 13, 2023
1 parent 7e71793 commit 811c929
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
34 changes: 32 additions & 2 deletions packages/sst/src/constructs/NextjsSite.ts
Expand Up @@ -8,6 +8,7 @@ import {
Duration as CdkDuration,
RemovalPolicy,
CustomResource,
Fn,
} from "aws-cdk-lib/core";
import {
Code,
Expand Down Expand Up @@ -237,7 +238,9 @@ type NextjsSiteNormalizedProps<ONConfig extends OpenNextConfig> = NextjsSiteProp
* ```
*/
export class NextjsSite<ONConfig extends OpenNextConfig = OpenNextConfig> extends SsrSite {
declare props: NextjsSiteNormalizedProps<ONConfig>;
declare props: NextjsSiteNormalizedProps<ONConfig> & {
openNextOutput: OpenNextOutput
}
private _routes?: {
route: string;
regex: string;
Expand Down Expand Up @@ -275,6 +278,7 @@ export class NextjsSite<ONConfig extends OpenNextConfig = OpenNextConfig> extend
].join(" "),
...props,
});
this.openNextOutput = this.props.openNextOutput;

const disableIncrementalCache = this.openNextOutput?.additionalProps?.disableIncrementalCache ?? false;
const disableTagCache = this.openNextOutput?.additionalProps?.disableTagCache ?? false;
Expand All @@ -287,6 +291,11 @@ export class NextjsSite<ONConfig extends OpenNextConfig = OpenNextConfig> extend
this.disableDefaultLogging();
this.uploadSourcemaps();
}


if(this.openNextOutput?.edgeFunctions?.middleware) {
this.setMiddlewareEnv();
}

if (!disableIncrementalCache) {
this.createRevalidationQueue();
Expand Down Expand Up @@ -427,7 +436,6 @@ export class NextjsSite<ONConfig extends OpenNextConfig = OpenNextConfig> extend
const openNextOutput = JSON.parse(
fs.readFileSync(openNextOutputPath).toString()
) as OpenNextOutput;

const imageOpt = openNextOutput.origins.imageOptimizer as OpenNextFunctionOrigin;
const defaultFn = openNextOutput.origins.default;
const remainingFns = Object.entries(openNextOutput.origins).filter(([key, value]) => {
Expand Down Expand Up @@ -497,9 +505,31 @@ export class NextjsSite<ONConfig extends OpenNextConfig = OpenNextConfig> extend
warmerConfig: openNextOutput.additionalProps?.warmer ? {
function: openNextOutput.additionalProps.warmer.bundle,
} : undefined,
additionalProps: {
openNextOutput: openNextOutput,
}
});
}

private setMiddlewareEnv() {
const origins = this.serverFunctions.reduce((acc, server) => {
return {
...acc,
[server.function ?
server.id.replace("ServerFunction", "") :
server.id.replace("ServerContainer", "")
]:
{
host: server.function ? Fn.parseDomainName(server.fnUrl?.url ?? "") : server.cdk?.applicationLoadBalancer?.loadBalancerDnsName ?? "",
port: 443,
protocol: "https",
}
}
}, {} as Record<string, {host: string, port: number, protocol: string}>)
console.log(origins)
this.edgeFunctions?.middleware?.addEnvironment('OPEN_NEXT_ORIGIN', Fn.toJsonString(origins))
}

private createRevalidationQueue() {
if (!this.serverFunction) return;

Expand Down
5 changes: 4 additions & 1 deletion packages/sst/src/constructs/SsrFunction.ts
Expand Up @@ -22,6 +22,7 @@ import {
FunctionOptions,
Function as CdkFunction,
FunctionUrlOptions,
FunctionUrl,
} from "aws-cdk-lib/aws-lambda";
import { Bucket } from "aws-cdk-lib/aws-s3";
import {
Expand Down Expand Up @@ -79,6 +80,7 @@ export class SsrFunction extends Construct implements SSTConstruct {
/** @internal */
public readonly _doNotAllowOthersToBind = true;
public function: CdkFunction;
public fnUrl?: FunctionUrl;
private assetReplacer: CustomResource;
private assetReplacerPolicy: Policy;
private missingSourcemap?: boolean;
Expand Down Expand Up @@ -153,7 +155,8 @@ export class SsrFunction extends Construct implements SSTConstruct {
}

public addFunctionUrl(props?: FunctionUrlOptions) {
return this.function.addFunctionUrl(props);
this.fnUrl = this.function.addFunctionUrl(props);
return this.fnUrl;
}

public grantInvoke(grantee: IGrantable) {
Expand Down
4 changes: 3 additions & 1 deletion packages/sst/src/constructs/SsrSite.ts
Expand Up @@ -248,7 +248,7 @@ export interface SsrSiteProps {
/**
* The number of server functions to keep warm. This option is only supported for the regional mode.
* @deprecated should be set by function
* @default Server function is not kept warm
* @default server function is not kept warm
*/
warm?: number;
regional?: {
Expand Down Expand Up @@ -591,6 +591,7 @@ export abstract class SsrSite extends Construct implements SSTConstruct {
// Build app
buildApp();
const plan = this.plan(bucket);
this.props = { ...props, ...plan.additionalProps };
validateTimeout();

// Create CloudFront
Expand Down Expand Up @@ -1621,6 +1622,7 @@ if (event.type === "warmer") {
function: string;
schedule?: Schedule;
};
additionalProps?: Record<string, any>;
}) {
return input;
}
Expand Down

0 comments on commit 811c929

Please sign in to comment.