-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnextjs-global-functions.ts
More file actions
119 lines (109 loc) · 4.49 KB
/
Copy pathnextjs-global-functions.ts
File metadata and controls
119 lines (109 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Distribution } from "aws-cdk-lib/aws-cloudfront";
import { Construct } from "constructs";
import { NextjsType } from "../constants";
import { OptionalNextjsDistributionProps } from "../generated-structs/OptionalNextjsDistributionProps";
import { OptionalNextjsPostDeployProps } from "../generated-structs/OptionalNextjsPostDeployProps";
import {
NextjsFunctions,
NextjsFunctionsOverrides,
NextjsFunctionsProps,
} from "../nextjs-compute/nextjs-functions";
import {
NextjsDistribution,
NextjsDistributionOverrides,
} from "../nextjs-distribution";
import {
NextjsPostDeploy,
NextjsPostDeployOverrides,
} from "../nextjs-post-deploy";
import {
NextjsBaseConstructOverrides,
NextjsBaseOverrides,
NextjsBaseConstruct,
NextjsBaseProps,
} from "./nextjs-base-construct";
export interface NextjsGlobalFunctionsConstructOverrides extends NextjsBaseConstructOverrides {
readonly nextjsFunctionsProps?: NextjsFunctionsProps;
readonly nextjsDistributionProps?: OptionalNextjsDistributionProps;
readonly nextjsPostDeployProps?: OptionalNextjsPostDeployProps;
}
/**
* Overrides for `NextjsGlobalFunctions`. Overrides are lower level than
* props and are passed directly to CDK Constructs giving you more control. It's
* recommended to use caution and review source code so you know how they're used.
*/
export interface NextjsGlobalFunctionsOverrides extends NextjsBaseOverrides {
readonly nextjsGlobalFunctions?: NextjsGlobalFunctionsConstructOverrides;
readonly nextjsFunctions?: NextjsFunctionsOverrides;
readonly nextjsDistribution?: NextjsDistributionOverrides;
readonly nextjsPostDeploy?: NextjsPostDeployOverrides;
}
export interface NextjsGlobalFunctionsProps extends NextjsBaseProps {
/**
* Bring your own distribution. Can be used with `basePath` to host multiple
* apps on the same CloudFront distribution.
*/
readonly distribution?: Distribution;
/**
* Override props of any construct.
*/
readonly overrides?: NextjsGlobalFunctionsOverrides;
}
/**
* Deploy Next.js globally distributed with functions. Uses [CloudFront
* Distribution](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-working-with.html)
* as Content Delivery Network (CDN) for global distribution and [AWS Lambda Functions](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)
* for functions.
*/
export class NextjsGlobalFunctions extends NextjsBaseConstruct {
nextjsFunctions: NextjsFunctions;
nextjsDistribution: NextjsDistribution;
nextjsPostDeploy: NextjsPostDeploy;
get url(): string {
return `https://${this.nextjsDistribution.distribution.domainName}`;
}
private props: NextjsGlobalFunctionsProps;
constructor(scope: Construct, id: string, props: NextjsGlobalFunctionsProps) {
super(scope, id, props, NextjsType.GLOBAL_FUNCTIONS);
this.props = props;
this.nextjsFunctions = this.createNextjsFunctions();
this.nextjsDistribution = this.createNextjsDistribution();
this.nextjsPostDeploy = this.createNextjsPostDeploy();
}
private createNextjsFunctions(): NextjsFunctions {
// Create functions with local build output
return new NextjsFunctions(this, "NextjsFunctions", {
...this.computeBaseProps(),
overrides: {
...this.props.overrides?.nextjsFunctions,
dockerImageFunctionProps: {
...this.props.overrides?.nextjsFunctions?.dockerImageFunctionProps,
vpc: this.baseProps.vpc,
},
},
...this.props.overrides?.nextjsGlobalFunctions?.nextjsFunctionsProps,
});
}
private createNextjsDistribution() {
return new NextjsDistribution(this, "NextjsDistribution", {
assetsBucket: this.nextjsStaticAssets.bucket,
basePath: this.baseProps.basePath,
functionUrl: this.nextjsFunctions.functionUrl,
nextjsType: this.nextjsType,
overrides: this.props.overrides?.nextjsDistribution,
publicDirEntries: this.nextjsBuild.publicDirEntries,
...this.props.overrides?.nextjsGlobalFunctions?.nextjsDistributionProps,
});
}
private createNextjsPostDeploy(): NextjsPostDeploy {
return new NextjsPostDeploy(this, "NextjsPostDeploy", {
buildId: this.nextjsBuild.buildId,
distribution: this.nextjsDistribution.distribution,
cacheBucket: this.nextjsCache.cacheBucket,
revalidationTable: this.nextjsCache.revalidationTable,
staticAssetsBucket: this.nextjsStaticAssets.bucket,
overrides: this.props.overrides?.nextjsPostDeploy,
...this.props.overrides?.nextjsGlobalFunctions?.nextjsPostDeployProps,
});
}
}