cdk-nextjs 0.7.12 → 0.7.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.jsii +346 -100
  2. package/.jsii.tabl.json +1 -1
  3. package/API.md +208 -0
  4. package/README.md +115 -100
  5. package/docs/breaking-changes.md +4 -2
  6. package/docs/development-guide.md +10 -0
  7. package/docs/harness-coverage.md +16 -6
  8. package/docs/next-build-output-guide.md +1 -1
  9. package/lib/adapter/adapter.mjs +4 -1
  10. package/lib/generated-structs/OptionalNextjsApiProps.d.ts +6 -0
  11. package/lib/generated-structs/OptionalNextjsApiProps.js +1 -1
  12. package/lib/generated-structs/OptionalNextjsDistributionProps.d.ts +27 -0
  13. package/lib/generated-structs/OptionalNextjsDistributionProps.js +1 -1
  14. package/lib/nextjs-api.d.ts +11 -0
  15. package/lib/nextjs-api.js +63 -9
  16. package/lib/nextjs-api.test.js +127 -1
  17. package/lib/nextjs-build/nextjs-build.js +1 -1
  18. package/lib/nextjs-cache.js +1 -1
  19. package/lib/nextjs-compute/nextjs-containers.js +1 -1
  20. package/lib/nextjs-compute/nextjs-functions.js +1 -1
  21. package/lib/nextjs-distribution.d.ts +92 -2
  22. package/lib/nextjs-distribution.js +223 -109
  23. package/lib/nextjs-distribution.test.js +492 -12
  24. package/lib/nextjs-post-deploy.js +1 -1
  25. package/lib/nextjs-static-assets.js +1 -1
  26. package/lib/nextjs-static-assets.test.js +19 -7
  27. package/lib/root-constructs/nextjs-base-construct.d.ts +7 -0
  28. package/lib/root-constructs/nextjs-base-construct.js +14 -2
  29. package/lib/root-constructs/nextjs-global-containers.d.ts +16 -1
  30. package/lib/root-constructs/nextjs-global-containers.js +5 -2
  31. package/lib/root-constructs/nextjs-global-functions.d.ts +16 -1
  32. package/lib/root-constructs/nextjs-global-functions.js +5 -2
  33. package/lib/root-constructs/nextjs-regional-containers.js +1 -1
  34. package/lib/root-constructs/nextjs-regional-functions.js +3 -2
  35. package/lib/runtime/lambda.mjs +4 -1
  36. package/lib/runtime/server.mjs +4 -1
  37. package/lib/utils/viewer-request-functions.d.ts +53 -0
  38. package/lib/utils/viewer-request-functions.js +172 -0
  39. package/lib/utils/viewer-request-functions.test.d.ts +1 -0
  40. package/lib/utils/viewer-request-functions.test.js +43 -0
  41. package/package.json +4 -1
  42. package/scripts/e2e-harness/README.md +10 -8
  43. package/scripts/e2e-harness/app.js +7 -2
  44. package/scripts/render-diagrams.mjs +121 -0
@@ -1,5 +1,5 @@
1
1
  import { ICertificate } from "aws-cdk-lib/aws-certificatemanager";
2
- import { AddBehaviorOptions, CachePolicyProps, Distribution, ResponseHeadersPolicyProps } from "aws-cdk-lib/aws-cloudfront";
2
+ import { AddBehaviorOptions, CachePolicyProps, Distribution, FunctionCode, IKeyValueStore, ResponseHeadersPolicyProps } from "aws-cdk-lib/aws-cloudfront";
3
3
  import { FunctionUrlOriginWithOACProps, VpcOriginWithEndpointProps } from "aws-cdk-lib/aws-cloudfront-origins";
4
4
  import { IApplicationLoadBalancer } from "aws-cdk-lib/aws-elasticloadbalancingv2";
5
5
  import { IFunctionUrl } from "aws-cdk-lib/aws-lambda";
@@ -76,6 +76,15 @@ export interface NextjsDistributionProps {
76
76
  * add static behaviors to distribution.
77
77
  */
78
78
  readonly publicDirEntries: PublicDirEntry[];
79
+ /**
80
+ * The `Service-Worker-Allowed` header for `_next/static/service-worker/*`,
81
+ * which Next.js sends so a compiled service worker can register at a scope
82
+ * wider than its own directory, such as `/`. S3 can't send an arbitrary
83
+ * header, so setting this adds a behavior for those files whose response
84
+ * headers policy does. Next.js uses the app's `basePath`, or `/` for none.
85
+ * @default - no behavior; the build has no service worker
86
+ */
87
+ readonly serviceWorkerAllowed?: string;
79
88
  /**
80
89
  * The non-`default` function groups, the origins
81
90
  * {@link functionGroupBehaviors} route to.
@@ -101,6 +110,34 @@ export interface NextjsDistributionProps {
101
110
  * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html#limits-web-distributions
102
111
  */
103
112
  readonly maxCacheBehaviors?: number;
113
+ /**
114
+ * CloudFront Function code run first on every viewer request, on every
115
+ * behavior cdk-nextjs adds, static assets included — HTTP Basic auth on a
116
+ * pre-launch site, geo-gating.
117
+ *
118
+ * It must define `viewerRequest(event)` (a function, sync or async, or a
119
+ * `var`/`let`/`const`): return a response to answer the viewer, or the
120
+ * request (or nothing) to carry on. It is spliced into cdk-nextjs's own
121
+ * viewer-request functions, ahead of their logic, so it may not use the
122
+ * name `handler`, and the combined function must fit CloudFront's 10 KB.
123
+ * It runs on `cloudfront-js-2.0`, so it may be async and read
124
+ * {@link viewerRequestKeyValueStore} with `cf.kvs()`. A bring-your-own
125
+ * {@link distribution}'s default behavior isn't covered.
126
+ *
127
+ * @default - none
128
+ * @see https://github.com/cdklabs/cdk-nextjs#running-code-on-every-viewer-request
129
+ */
130
+ readonly viewerRequestFunctionCode?: FunctionCode;
131
+ /**
132
+ * A CloudFront KeyValueStore associated with every viewer-request function
133
+ * {@link viewerRequestFunctionCode} is copied into (up to three, one per kind
134
+ * of behavior), so values it reads — Basic auth credentials, an allowlist —
135
+ * live outside the CloudFormation template and change without a deploy.
136
+ * Read it with `cf.kvs()`, no ID needed.
137
+ * @default - none
138
+ * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-custom-methods.html
139
+ */
140
+ readonly viewerRequestKeyValueStore?: IKeyValueStore;
104
141
  }
105
142
  /** A non-default function group and the origin its routes must reach. */
106
143
  export interface NextjsDistributionFunctionGroup {
@@ -134,6 +171,18 @@ export declare class NextjsDistribution extends Construct {
134
171
  private dynamicOrigin;
135
172
  private dynamicOriginResponsePolicy;
136
173
  private dynamicCloudFrontFunctionAssociations;
174
+ /**
175
+ * `viewerRequestFunctionCode`, rendered and checked once, since
176
+ * `FunctionCode.fromFile` reads the file on every render.
177
+ */
178
+ private viewerRequestCode?;
179
+ /**
180
+ * The viewer-request function `viewerRequestFunctionCode` adds to behaviors
181
+ * with no viewer-request logic of their own, shared by all of them, so the
182
+ * `assetPrefix` behavior, which has a viewer-request function of its own,
183
+ * can tell it from an override's.
184
+ */
185
+ private passThroughFunction?;
137
186
  private isFunctionCompute;
138
187
  private staticBehaviorOptions;
139
188
  private dynamicBehaviorOptions;
@@ -168,11 +217,40 @@ export declare class NextjsDistribution extends Construct {
168
217
  * @see https://open-next.js.org/advanced/workaround#workaround-set-x-forwarded-host-header-aws-specific
169
218
  */
170
219
  private createDynamicCloudFrontFunctionAssociations;
220
+ /**
221
+ * The function a behavior with no viewer-request logic of its own gets when
222
+ * `viewerRequestFunctionCode` is set, created on first use.
223
+ */
224
+ private getPassThroughFunction;
225
+ /**
226
+ * The code, runtime and KeyValueStore of a function whose `handler` is
227
+ * cdk-nextjs's, with `viewerRequestFunctionCode` spliced in
228
+ * ({@link spliceViewerRequest}) and checked against CloudFront's size limit.
229
+ *
230
+ * Every one runs on `cloudfront-js-2.0`, the runtime AWS recommends and the
231
+ * one with `async` and KeyValueStores, so the handlers are all `async`
232
+ * whether or not there is user code to await.
233
+ */
234
+ private viewerRequestFunctionProps;
235
+ /**
236
+ * `viewerRequestFunctionCode`, rendered once, since `FunctionCode.fromFile`
237
+ * reads the file on every render, and checked
238
+ * ({@link checkViewerRequestCode}).
239
+ */
240
+ private renderViewerRequestCode;
241
+ /**
242
+ * CloudFront allows one function per event type per behavior, so an
243
+ * override's viewer-request association and one cdk-nextjs needs can't both
244
+ * be kept. Thrown at synth, where both halves are known, rather than left to
245
+ * a deploy that names neither.
246
+ */
247
+ private assertViewerRequestUnclaimed;
171
248
  private createStaticBehaviorOptions;
172
249
  private createDynamicBehaviorOptions;
173
250
  /**
174
251
  * The behavior's function associations: an override's, plus the viewer-request
175
- * function Global Functions depends on.
252
+ * function Global Functions depends on, or the one `viewerRequestFunctionCode`
253
+ * adds on Global Containers.
176
254
  *
177
255
  * Spreading the override's `functionAssociations` over ours would silently
178
256
  * drop that function, and with it the only thing standing between a viewer's
@@ -254,6 +332,18 @@ export declare class NextjsDistribution extends Construct {
254
332
  * parse.
255
333
  */
256
334
  private addAssetPrefixBehavior;
335
+ /**
336
+ * Serves `_next/static/service-worker/*` with `Service-Worker-Allowed`, which
337
+ * the browser requires before it registers a service worker at a scope above
338
+ * the script's own directory. Added ahead of `_next/static*`, which would
339
+ * otherwise match first.
340
+ *
341
+ * The policy is the static one plus the header, so security headers and any
342
+ * `staticResponseHeadersPolicyProps` still apply. A whole
343
+ * `staticBehaviorOptions.responseHeadersPolicy` can't be added to, so it is
344
+ * used as it is, with a warning.
345
+ */
346
+ private addServiceWorkerBehavior;
257
347
  private addStaticBehaviors;
258
348
  /**
259
349
  * The behavior path pattern for each top-level `public/` entry, final form: