Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to set redirect options for the bucket #23

Closed
wants to merge 9 commits into from

Conversation

linusmarco
Copy link
Contributor

This PR implements the feature I described in #21.

Specifically, the user is now allowed to set all redirect/routing options supported by the CloudFormation API:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-websiteconfiguration.html

Note that I had to slightly change the implementation of the recent error/index document options because CloudFormation does not allow the optional RedirectAllRequestsTo property to be specified with any other WebsiteConfiguration options. Therefore, we cannot set IndexDocument and ErrorDocument by default; instead those now get set only if RedirectAllRequestsTo is not specified. Existing behavior is unchanged (for these options and everything else).

Thanks!

@fernando-mc
Copy link
Owner

@linusmarco It's taken me a while to get to this, sorry about that! I'll try to review this over the weekend.

@linusmarco
Copy link
Contributor Author

linusmarco commented Feb 27, 2018

No problem at all. That sounds good to me!

@fernando-mc fernando-mc self-assigned this Feb 28, 2018
@evanseeds
Copy link
Contributor

@linusmarco oh nice, I didn't realize this CloudFormation option was available. What's the use case for RedirectAllRequestsTo? I first thought it might be a natural solution for single-page applications, better than setting both indexDocument and errorDocument to the same page. Looking at the AWS documentation, however, it seems like RedirectAllRequestsTo is used to redirect to a different host, not to a specific document.

In the indexDoc/errorDoc PR I submitted, I originally had a spa option to enable a single-page application configuration, which we removed for clarity's sake. Would these options provide a better practice for those situations?

Sorry if this is a little off-topic from the PR itself!

@evanseeds evanseeds mentioned this pull request Mar 1, 2018
@linusmarco
Copy link
Contributor Author

linusmarco commented Mar 1, 2018

@evanseeds -- I'm honestly not sure what a real-world use case would be for the RedirectAllRequestsTo option -- maybe if you moved a site to a different domain or something? I just included that option in this PR for completeness' sake.

Using RoutingRules is a more flexible alternative to the trick of setting the errorDocument to the same file as the indexDocument and can be used in conjunction with that trick. For instance, to achieve the same base result as indexDoc=errorDoc, you could do:

routingRules:
   - redirect:
       hostName: www.my-website.com
       httpRedirectCode: 300
       protocol: https
       replaceKeyPrefixWith: '#/'
     condition:
       httpErrorCodeReturnedEquals: 404

The big use case for RoutingRules (for me at least) is to direct certain requests to Lambda functions. That way in my front-end code I can send a request to 'www.my-website.com/api/users' and have the S3 bucket autmatically route that to the correct Lambda function via an API Gateway endpoint:

routingRules:
   ...
   - redirect:
       hostName: gatewayhash.execute-api.us-east-1.amazonaws.com
       httpRedirectCode: 300
       protocol: https
       replaceKeyPrefixWith: 'production/'
     condition:
       keyPrefixEquals: 'api/'

But there's a bunch of other useful stuff you can do with these options too!

Copy link
Owner

@fernando-mc fernando-mc left a comment

Choose a reason for hiding this comment

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

I few general suggestions on how we can improve the structure. Address those and I'll try to test this for merge Wednesday.

README.md Outdated
indexDocument: index.html # (Optional) The name of your index document inside your distributionFolder. Defaults to index.html
errorDocument: error.html # (Optional) The name of your error document inside your distributionFolder. Defaults to error.html
redirectAllRequestsTo: # (Optional) If specified, all requests will redirect to specified endpoint
hostName: [hostName] # (Required) Name of the host where requests are redirected (e.g. www.google.com)
protocol: [http|http] # (Optional) Protocol for redirect
Copy link
Owner

Choose a reason for hiding this comment

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

Fix - http|http --> http|https?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch!

README.md Outdated
routingRules: # (Optional) Redirect routing rules
- redirect: # (Required) Redirect options for this rule
hostName: [hostName] # (Optional) Name of the host where requests are redirected (e.g. www.google.com)
httpRedirectCode: [CODE] # (Optional) HTTP status code for redirect
Copy link
Owner

Choose a reason for hiding this comment

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

add example codes? Or is this all covered in the AWS docs. I'm fine with linking to that in multiple places this if it is.

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 don't think they spell it out in the AWS docs, so I'll give some examples here

README.md Outdated
- redirect: # (Required) Redirect options for this rule
hostName: [hostName] # (Optional) Name of the host where requests are redirected (e.g. www.google.com)
httpRedirectCode: [CODE] # (Optional) HTTP status code for redirect
protocol: [http|http] # (Optional) Protocol for redirect
Copy link
Owner

Choose a reason for hiding this comment

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

Same fix as above. one should be https?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

index.js Outdated

}

function confirm(expr, error) {
Copy link
Owner

Choose a reason for hiding this comment

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

This is kinda awkward and uses the same name as the built-in browser function confirm().

What do you think about using something like this - https://github.com/arasatasaygin/is.js

Might still need a little custom work to implement it but seems like it might save us defining a bunch of custom checks. If we're going to define those they should be broken out of an already unwieldy index.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, I had completely forgotten that confirm was a built-in...I never really use it. The library you linked looks like a great option for the kind of stuff that I was rolling-my-own on. I'm happy to switch to that. Thanks!

@linusmarco linusmarco self-assigned this Mar 5, 2018
@linusmarco
Copy link
Contributor Author

@fernando-mc - I addressed your comments. Thanks for the review!

@fernando-mc
Copy link
Owner

@linusmarco awesome! Last thing, can you please update to conform with the style guidelines PR you put in? I figured it would be better to get the style guidelines in sooner since they would basically make everything have a merge conflict.

@linusmarco
Copy link
Contributor Author

@fernando-mc - updated coding and docs style to comply with recently merged PR. Should be ready to merge.

@linusmarco
Copy link
Contributor Author

@fernando-mc - Updated to fix merge conflicts resulting from #42

Tested and should be ready to merge

@linusmarco linusmarco changed the base branch from master to development March 15, 2018 01:31
@linusmarco
Copy link
Contributor Author

Just updated this PR to merge to the new development branch rather than master

@fernando-mc
Copy link
Owner

@linusmarco, awesome work! I’ll take a look at everything tomorrow. Hoping to run a set of tests on all PRs that are ready for it then we can merge into development!

@linusmarco linusmarco mentioned this pull request Apr 7, 2018
@fernando-mc
Copy link
Owner

@linusmarco I think I dropped the ball on this. Can we salvage the code changes from this and move forward on this again after I merge up v2?

@linusmarco
Copy link
Contributor Author

linusmarco commented Apr 22, 2018

@fernando-mc I actually worked these changes into the refactor (#43), so no need to do anythong with this PR. I'll close it.

@linusmarco linusmarco closed this Apr 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants