Created
April 24, 2020 13:18
-
-
Save raido/37897fb625b0c64deab9ec83143cec4a to your computer and use it in GitHub Desktop.
Redirect Ember.js # URLs to new ones for backward compatible user bookmarks
This file contains hidden or 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
const REDIRECT_MAP = { | |
"v1/login": "v2/login" | |
}; | |
export function initialize() { | |
const location = window.location; | |
const hash = location.hash; | |
if (hash.length > 0) { | |
const oldRoute = hash.substr(1, hash.length); | |
const [path, query] = oldRoute.split("?"); | |
const [,normalizedPath] = path.split("/"); | |
const mappedNewRoute = REDIRECT_MAP[normalizedPath]; | |
if (mappedNewRoute) { | |
const queryString = query ? `?${query}` : ""; | |
window.history.replaceState({}, "", `${mappedNewRoute}${queryString}`); | |
} else { | |
window.history.replaceState({}, "", oldRoute); | |
} | |
} | |
} | |
export default { | |
name: "rewrite-hash-url-to-history", | |
initialize | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment