Last active
July 16, 2016 13:20
-
-
Save morgan/c83d0440f40d22956d55f46cc27db30a to your computer and use it in GitHub Desktop.
Make S3 website hosting and multiple EmberJS applications play well together by using a hashbang without sacrificing pretty URLs! This initializer removes the hashbang from the Ember URL while preserving the nested route.
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
/** | |
`location-hashbang.js` checks for the presence of a `#!` hashbang, | |
removes it and updates the location without a page reload | |
or history modification. | |
Create initializer as follows: `app/initializers/location-hashbang.js` | |
See `s3-redirection-rules.xml` for sample rules. | |
Tested with Ember 2.6.0 | |
@class LocationHashbang | |
@namespace initializers | |
@author Micheal Morgan <micheal@morgan.ly> | |
*/ | |
export default { | |
name: 'location-hashbang', | |
initialize() { | |
const location = window.location; | |
const hash = location.hash; | |
let pathname; | |
// Check for hashbang and update location removing it | |
if (hash.indexOf('#!/') > -1) { | |
pathname = location.pathname; | |
// Various versions of IE and Opera do not always return a leading slash | |
if (pathname.charAt(0) !== '/') { | |
pathname = `/${pathname}`; | |
} | |
// Reconstruct path | |
const path = pathname + hash.replace('#!/', '') + location.search; | |
// Update location without page reload | |
window.history.replaceState({ path: path }, null, path); | |
} | |
} | |
}; |
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
<RoutingRules> | |
<RoutingRule> | |
<Condition> | |
<KeyPrefixEquals>nested/</KeyPrefixEquals> | |
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals> | |
</Condition> | |
<Redirect> | |
<HostName>example.com</HostName> | |
<ReplaceKeyPrefixWith>nested/#!/</ReplaceKeyPrefixWith> | |
</Redirect> | |
</RoutingRule> | |
</RoutingRules> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment