Skip to content

Commit 89c6d95

Browse files
gbone-restoregbone24Ben Stickley
authored
fix: basePath not being applied to static assets in S3 and Cloudfront behaviors (#102)
* Fix for basePath not being applied to static assets in S3 and Cloudfront behaviors * fix: api docs --------- Co-authored-by: gbone24 <4950487+gbone24@users.noreply.github.com> Co-authored-by: Ben Stickley <stickb@amazon.com>
1 parent 8306ef1 commit 89c6d95

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

API.md

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/generated-structs/OptionalNextjsAssetsDeploymentProps.ts

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nextjs-assets-deployment.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export interface NextjsAssetDeploymentOverrides {
2727

2828
export interface NextjsAssetsDeploymentProps {
2929
readonly accessPoint: AccessPoint;
30+
/**
31+
* Prefix to the URI path the app will be served at.
32+
* @example "/my-base-path"
33+
*/
34+
readonly basePath?: string;
3035
/**
3136
* @see {@link NextjsBuild.buildImageDigest}
3237
*/
@@ -177,13 +182,18 @@ export class NextjsAssetsDeployment extends Construct {
177182
const root = "/app";
178183
const actions: CustomResourceProperties["actions"] = [];
179184
if (this.props.staticAssetsBucket?.bucketName) {
185+
// Prepare the destination key prefix with basePath when available
186+
const staticKeyPrefix = this.props.basePath
187+
? `${this.props.basePath.replace(/^\//, "")}/_next/static`
188+
: "_next/static";
189+
180190
actions.push(
181191
// static files
182192
{
183193
type: "fs-to-s3",
184194
sourcePath: join(root, ".next", "static"),
185195
destinationBucketName: this.props.staticAssetsBucket.bucketName,
186-
destinationKeyPrefix: "_next/static",
196+
destinationKeyPrefix: staticKeyPrefix,
187197
},
188198
// public directory to s3 for CloudFront -> S3
189199
{

src/nextjs-distribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export class NextjsDistribution extends Construct {
349349
}
350350
private addStaticBehaviors() {
351351
this.distribution.addBehavior(
352-
"_next/static*",
352+
this.getPathPattern("_next/static*"),
353353
this.staticOrigin,
354354
this.staticBehaviorOptions,
355355
);

src/root-constructs/nextjs-global-containers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class NextjsGlobalContainers extends Construct {
140140
private createNextjsAssetsDeployment() {
141141
return new NextjsAssetsDeployment(this, "NextjsAssetsDeployment", {
142142
accessPoint: this.nextjsFileSystem.accessPoint,
143+
basePath: this.props.basePath,
143144
buildImageDigest: this.nextjsBuild.buildImageDigest,
144145
containerMountPathForEfs: this.nextjsBuild.containerMountPathForEfs,
145146
dockerImageCode: this.nextjsBuild.imageForNextjsAssetsDeployment,

src/root-constructs/nextjs-global-functions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export class NextjsGlobalFunctions extends Construct {
143143
private createNextjsAssetsDeployment() {
144144
return new NextjsAssetsDeployment(this, "NextjsAssetsDeployment", {
145145
accessPoint: this.nextjsFileSystem.accessPoint,
146+
basePath: this.props.basePath,
146147
buildImageDigest: this.nextjsBuild.buildImageDigest,
147148
dockerImageCode: this.nextjsBuild.imageForNextjsAssetsDeployment,
148149
containerMountPathForEfs: this.nextjsBuild.containerMountPathForEfs,

0 commit comments

Comments
 (0)