Skip to content

Commit 0ef9883

Browse files
authored
feat(amplify): add SPA redirect custom rule (#7320)
Refactor `CustomRule` to an enum-like class and add a static property `SINGLE_PAGE_APPLICATION_REDIRECT` that sets up a 200 rewrite for all files to `index.html` except for the following file extensions: css, gif, ico, jpg, js, png, txt, svg, woff, ttf, map, json, webmanifest.
1 parent f3219c3 commit 0ef9883

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

packages/@aws-cdk/aws-amplify/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ amplifyApp.addCustomRule({
8383
});
8484
```
8585

86+
When working with a single page application (SPA), use the
87+
`CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT` to set up a 200
88+
rewrite for all files to `index.html` except for the following
89+
file extensions: css, gif, ico, jpg, js, png, txt, svg, woff,
90+
ttf, map, json, webmanifest.
91+
92+
```ts
93+
mySinglePageApp.addCustomRule(amplify.CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT);
94+
```
95+
8696
Add a domain and map sub domains to branches:
8797
```ts
8898
const domain = amplifyApp.addDomain('example.com');

packages/@aws-cdk/aws-amplify/lib/app.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,30 +375,94 @@ export enum RedirectStatus {
375375
}
376376

377377
/**
378-
* Custom rewrite/redirect rule for an Amplify App.
378+
* Options for a custom rewrite/redirect rule for an Amplify App.
379379
*/
380-
export interface CustomRule {
380+
export interface CustomRuleOptions {
381381
/**
382382
* The source pattern for a URL rewrite or redirect rule.
383+
*
384+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
383385
*/
384386
readonly source: string;
385387

386388
/**
387389
* The target pattern for a URL rewrite or redirect rule.
390+
*
391+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
388392
*/
389393
readonly target: string
390394

391395
/**
392396
* The status code for a URL rewrite or redirect rule.
393397
*
398+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
399+
*
394400
* @default PERMANENT_REDIRECT
395401
*/
396402
readonly status?: RedirectStatus;
397403

398404
/**
399405
* The condition for a URL rewrite or redirect rule, e.g. country code.
400406
*
407+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
408+
*
401409
* @default - no condition
402410
*/
403411
readonly condition?: string;
404412
}
413+
414+
/**
415+
* Custom rewrite/redirect rule for an Amplify App.
416+
*
417+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
418+
*/
419+
export class CustomRule {
420+
/**
421+
* Sets up a 200 rewrite for all paths to `index.html` except for path
422+
* containing a file extension.
423+
*/
424+
public static readonly SINGLE_PAGE_APPLICATION_REDIRECT = new CustomRule({
425+
source: '</^[^.]+$/>',
426+
target: '/index.html',
427+
status: RedirectStatus.REWRITE,
428+
});
429+
430+
/**
431+
* The source pattern for a URL rewrite or redirect rule.
432+
*
433+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
434+
*/
435+
public readonly source: string;
436+
437+
/**
438+
* The target pattern for a URL rewrite or redirect rule.
439+
*
440+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
441+
*/
442+
public readonly target: string;
443+
444+
/**
445+
* The status code for a URL rewrite or redirect rule.
446+
*
447+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
448+
*
449+
* @default PERMANENT_REDIRECT
450+
*/
451+
public readonly status?: RedirectStatus;
452+
453+
/**
454+
* The condition for a URL rewrite or redirect rule, e.g. country code.
455+
*
456+
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
457+
*
458+
* @default - no condition
459+
*/
460+
public readonly condition?: string;
461+
462+
constructor(options: CustomRuleOptions) {
463+
this.source = options.source;
464+
this.target = options.target;
465+
this.status = options.status;
466+
this.condition = options.condition;
467+
}
468+
}

packages/@aws-cdk/aws-amplify/test/app.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,29 @@ test('with custom rules', () => {
261261
});
262262
});
263263

264+
test('with SPA redirect', () => {
265+
// WHEN
266+
new amplify.App(stack, 'App', {
267+
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
268+
owner: 'aws',
269+
repository: 'aws-cdk',
270+
oauthToken: SecretValue.plainText('secret'),
271+
}),
272+
customRules: [amplify.CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT],
273+
});
274+
275+
// THEN
276+
expect(stack).toHaveResource('AWS::Amplify::App', {
277+
CustomRules: [
278+
{
279+
Source: '</^[^.]+$/>',
280+
Status: '200',
281+
Target: '/index.html',
282+
},
283+
],
284+
});
285+
});
286+
264287
test('with auto branch creation', () => {
265288
// WHEN
266289
const app = new amplify.App(stack, 'App', {

0 commit comments

Comments
 (0)