Skip to content

Commit c67da83

Browse files
authored
feat(amplify): enables apps hosted with server side rendering (#26861)
### Problem: The Amplify App L2 construct does not support the ability for customers to configure a SSR app without reaching down into the lower level L1. ### Solution: Expose the `platform` field used to set SSG vs SSR hosted applications. Do not expose `WEB_DYNAMIC` as SSRv1 is on the deprecation path. ### Testing Done: * `yarn build+test` * added new tests for `platform` field * updated snapshots for new default value Closes #24076 and #23325 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8a3db0a commit c67da83

File tree

7 files changed

+65
-6
lines changed

7 files changed

+65
-6
lines changed

Diff for: packages/@aws-cdk/aws-amplify-alpha/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ const amplifyApp = new amplify.App(this, 'App', {
225225
});
226226
```
227227

228+
## Configure server side rendering when hosting app
229+
230+
Setting the `platform` field on the Amplify `App` construct can be used to control whether the app will host only static assets or server side rendered assets in addition to static. By default, the value is set to `WEB` (static only), however, server side rendering can be turned on by setting to `WEB_COMPUTE` as follows:
231+
232+
```ts
233+
const amplifyApp = new amplify.App(this, 'MyApp', {
234+
platform: amplify.Platform.WEB_COMPUTE,
235+
});
236+
```
237+
228238
## Deploying Assets
229239

230240
`sourceCodeProvider` is optional; when this is not specified the Amplify app can be deployed to using `.zip` packages. The `asset` property can be used to deploy S3 assets to Amplify as part of the CDK:

Diff for: packages/@aws-cdk/aws-amplify-alpha/lib/app.ts

+23
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ export interface AppProps {
158158
* @default - a new role is created
159159
*/
160160
readonly role?: iam.IRole;
161+
162+
/**
163+
* Indicates the hosting platform to use. Set to WEB for static site
164+
* generated (SSG) apps (i.e. a Create React App or Gatsby) and WEB_COMPUTE
165+
* for server side rendered (SSR) apps (i.e. NextJS).
166+
*
167+
* @default - WEB
168+
*/
169+
readonly platform?: Platform;
161170
}
162171

163172
/**
@@ -248,6 +257,7 @@ export class App extends Resource implements IApp, iam.IGrantable {
248257
oauthToken: sourceCodeProviderOptions?.oauthToken?.unsafeUnwrap(), // Safe usage
249258
repository: sourceCodeProviderOptions?.repository,
250259
customHeaders: props.customResponseHeaders ? renderCustomResponseHeaders(props.customResponseHeaders) : undefined,
260+
platform: props.platform || Platform.WEB,
251261
});
252262

253263
this.appId = app.attrAppId;
@@ -528,3 +538,16 @@ function renderCustomResponseHeaders(customHeaders: CustomResponseHeader[]): str
528538

529539
return `${yaml.join('\n')}\n`;
530540
}
541+
542+
export enum Platform {
543+
/**
544+
* WEB - Used to indicate that the app is hosted using only static assets.
545+
*/
546+
WEB = 'WEB',
547+
548+
/**
549+
* WEB_COMPUTE - Used to indicate the app is hosted using a combination of
550+
* server side rendered and static assets.
551+
*/
552+
WEB_COMPUTE = 'WEB_COMPUTE',
553+
}

Diff for: packages/@aws-cdk/aws-amplify-alpha/test/app.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,25 @@ test('with custom headers', () => {
442442
},
443443
});
444444
});
445+
446+
test('create a statically hosted app by default', () => {
447+
// WHEN
448+
new amplify.App(stack, 'App', {});
449+
450+
// THEN
451+
Template.fromStack(stack).hasResourceProperties('AWS::Amplify::App', {
452+
Platform: amplify.Platform.WEB,
453+
});
454+
});
455+
456+
test('create a dynamically rendered app when the platform is set to WEB_COMPUTE', () => {
457+
// WHEN
458+
new amplify.App(stack, 'App', {
459+
platform: amplify.Platform.WEB_COMPUTE,
460+
});
461+
462+
// THEN
463+
Template.fromStack(stack).hasResourceProperties('AWS::Amplify::App', {
464+
Platform: amplify.Platform.WEB_COMPUTE,
465+
});
466+
});

Diff for: packages/@aws-cdk/aws-amplify-alpha/test/integ.app-asset-deployment.js.snapshot/cdk-amplify-app-asset-deployment.template.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"AppRole1AF9B530",
3030
"Arn"
3131
]
32-
}
32+
},
33+
"Platform": "WEB"
3334
}
3435
}
3536
},
@@ -67,4 +68,4 @@
6768
]
6869
}
6970
}
70-
}
71+
}

Diff for: packages/@aws-cdk/aws-amplify-alpha/test/integ.app-codecommit.js.snapshot/cdk-amplify-codecommit-app.template.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"Repo02AC86CF",
6868
"CloneUrlHttp"
6969
]
70-
}
70+
},
71+
"Platform": "WEB"
7172
}
7273
},
7374
"AppmainF505BAED": {
@@ -119,4 +120,4 @@
119120
]
120121
}
121122
}
122-
}
123+
}

Diff for: packages/@aws-cdk/aws-amplify-alpha/test/integ.app.js.snapshot/cdk-amplify-app.template.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
"AppRole1AF9B530",
8080
"Arn"
8181
]
82-
}
82+
},
83+
"Platform": "WEB_COMPUTE"
8384
}
8485
},
8586
"AppmainF505BAED": {
@@ -137,4 +138,4 @@
137138
]
138139
}
139140
}
140-
}
141+
}

Diff for: packages/@aws-cdk/aws-amplify-alpha/test/integ.app.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestStack extends Stack {
2525
},
2626
},
2727
],
28+
platform: amplify.Platform.WEB_COMPUTE,
2829
});
2930

3031
amplifyApp.addCustomRule({

0 commit comments

Comments
 (0)