Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-amplify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ amplifyApp.addCustomRule({
});
```

When working with a single page application (SPA), use the
`CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT` to set 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.

```ts
mySinglePageApp.addCustomRule(amplify.CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT);
```

Add a domain and map sub domains to branches:
```ts
const domain = amplifyApp.addDomain('example.com');
Expand Down
68 changes: 66 additions & 2 deletions packages/@aws-cdk/aws-amplify/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,30 +375,94 @@ export enum RedirectStatus {
}

/**
* Custom rewrite/redirect rule for an Amplify App.
* Options for a custom rewrite/redirect rule for an Amplify App.
*/
export interface CustomRule {
export interface CustomRuleOptions {
/**
* The source pattern for a URL rewrite or redirect rule.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*/
readonly source: string;

/**
* The target pattern for a URL rewrite or redirect rule.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*/
readonly target: string

/**
* The status code for a URL rewrite or redirect rule.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*
* @default PERMANENT_REDIRECT
*/
readonly status?: RedirectStatus;

/**
* The condition for a URL rewrite or redirect rule, e.g. country code.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*
* @default - no condition
*/
readonly condition?: string;
}

/**
* Custom rewrite/redirect rule for an Amplify App.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*/
export class CustomRule {
/**
* Sets up a 200 rewrite for all paths to `index.html` except for path
* containing a file extension.
*/
public static readonly SINGLE_PAGE_APPLICATION_REDIRECT = new CustomRule({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've always found these lists of extensions to be problematic. In my experience a custom redirect rule for all paths that don't contain an explicit file extension is much easier to deal with and wouldn't require us to maintain this ongoing list.

@jogold what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right... the regex comes from the docs at https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html and I already had to add the webmanifest extension.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully its relatively straightforward. I see this being relied on a lot. I'm actually surprised amplify doesn't have some solution built in for this behavior.

source: '</^[^.]+$/>',
target: '/index.html',
status: RedirectStatus.REWRITE,
});

/**
* The source pattern for a URL rewrite or redirect rule.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*/
public readonly source: string;

/**
* The target pattern for a URL rewrite or redirect rule.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*/
public readonly target: string;

/**
* The status code for a URL rewrite or redirect rule.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*
* @default PERMANENT_REDIRECT
*/
public readonly status?: RedirectStatus;

/**
* The condition for a URL rewrite or redirect rule, e.g. country code.
*
* @see https://docs.aws.amazon.com/amplify/latest/userguide/redirects.html
*
* @default - no condition
*/
public readonly condition?: string;

constructor(options: CustomRuleOptions) {
this.source = options.source;
this.target = options.target;
this.status = options.status;
this.condition = options.condition;
}
}
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-amplify/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,29 @@ test('with custom rules', () => {
});
});

test('with SPA redirect', () => {
// WHEN
new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: 'aws',
repository: 'aws-cdk',
oauthToken: SecretValue.plainText('secret'),
}),
customRules: [amplify.CustomRule.SINGLE_PAGE_APPLICATION_REDIRECT],
});

// THEN
expect(stack).toHaveResource('AWS::Amplify::App', {
CustomRules: [
{
Source: '</^[^.]+$/>',
Status: '200',
Target: '/index.html',
},
],
});
});

test('with auto branch creation', () => {
// WHEN
const app = new amplify.App(stack, 'App', {
Expand Down