Skip to content

Instantly share code, notes, and snippets.

@raido
Created April 24, 2020 13:18
Show Gist options
  • Save raido/37897fb625b0c64deab9ec83143cec4a to your computer and use it in GitHub Desktop.
Save raido/37897fb625b0c64deab9ec83143cec4a to your computer and use it in GitHub Desktop.
Redirect Ember.js # URLs to new ones for backward compatible user bookmarks
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