-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: cloudfront-function for url-rewrite needed for SPA
- Loading branch information
1 parent
d138971
commit 5a798a4
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# URL rewrite for single page applications (SPAs) | ||
|
||
CloudFront Functions event type: viewer request | ||
|
||
## Creating the function | ||
|
||
``` | ||
aws cloudformation deploy \ | ||
--region us-east-1 \ | ||
--stack-name url-rewrite-stack \ | ||
--template-file ./url-rewrite-spa.yaml \ | ||
--parameter-overrides AutoPublishParam=true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
AWSTemplateFormatVersion: 2010-09-09 | ||
Description: URL rewrite for React Router SPA with client-side routing | ||
|
||
Parameters: | ||
AutoPublishParam: | ||
Description: Whether to automatically publish on creation | ||
Type: String | ||
Default: false | ||
AllowedValues: | ||
- true | ||
- false | ||
|
||
Resources: | ||
RewriteFunction: | ||
Type: AWS::CloudFront::Function | ||
Properties: | ||
AutoPublish: !Ref AutoPublishParam | ||
FunctionCode: !Sub | | ||
function handler(event) { | ||
var request = event.request; | ||
var uri = request.uri; | ||
var paths = ['assets/', 'img/', '.xml', '.webmanifest', '.js', 'robots.txt'] | ||
var isServerPath = (path) => uri.includes(path); | ||
if (!paths.some(isServerPath)) { | ||
request.uri = '/'; | ||
} | ||
return request; | ||
} | ||
FunctionConfig: | ||
Comment: "Rewrite the URL for SPAs with client-side routing" | ||
Runtime: cloudfront-js-1.0 | ||
Name: !Sub "${AWS::StackName}-rewriteFunction" | ||
|
||
Outputs: | ||
FunctionArn: | ||
Description: CloudFront Function | ||
Value: !GetAtt RewriteFunction.FunctionMetadata.FunctionARN |