0

I have a web app (using Angular 2 with webpack) that should not require more than static file hosting...except that I am using html5 style routing (slashes rather than hashes). I would like to host it in an AWS S3 Bucket. If a user goes to the root of my app, the S3 Bucket serves index.html, and everything is fine. If the user clicks some button that uses the router to take them to myapp.com/routename, then it still works fine (because there's technically no page reload). But if the entry point for the user is myapp.com/routename, then the browser will do a get request to that url, and S3 will not know that index.html is the thing it should be responding with. I've tried using the S3 routing config...but the routing config seems to only be able to do redirects which actually change the url in the urlbar. For example, if I say redirect myapp.com/routename to myapp.com, it's not just serving index.html at that url too, it's actually changing the url in the browser, which sends the user back to the "/" route. Is there a way to configure and S3 bucket to do regular routing instead of redirects?

John Rood
  • 805
  • 3
  • 13
  • 25
  • 1
    Related? http://stackoverflow.com/questions/28063945/configure-amazon-s3-static-site-with-angular-js-ui-router-html5modetrue-on-pag – jarmod Dec 08 '16 at 18:58
  • Here's my corresponding post on the AWS forum https://forums.aws.amazon.com/thread.jspa?threadID=244746 – John Rood Dec 09 '16 at 17:46
  • @jarmod yes thanks, adding a hash (and then having angular remove it again) seems like it would work, but I think there ought to be a way to do this without using hashes at all. – John Rood Dec 09 '16 at 17:52
  • I think this CloudFront solution is probably the right one http://stackoverflow.com/a/35354677/3390298 – John Rood Dec 09 '16 at 18:19

1 Answers1

1

Theoretically you could copy your index.html to something like 404.html and enter this filename in "Error Document" section of S3 settings. When there is no page to display (error 404), S3 would show that document.

It should work, but it's a horrible idea. Browsers will still receive 404 HTTP code in HTTP headers.

Sergey Kovalev
  • 9,110
  • 2
  • 28
  • 32
  • Yes I think handling a "document not found" scenario is a decent kind of solution. Returning a 404 is definitely not ideal. I've found that if you do this on the CloudFront side of things you can have it return a 200. – John Rood Dec 09 '16 at 18:27