Skip to content

Instantly share code, notes, and snippets.

@artemave
Created January 11, 2019 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemave/7c7415878c414cfdaf3978485d74e11d to your computer and use it in GitHub Desktop.
Save artemave/7c7415878c414cfdaf3978485d74e11d to your computer and use it in GitHub Desktop.
Stub window location for hyperdom router
var isBrowser = typeof window !== 'undefined';
var pushState, replaceState;
pushState = replaceState = function(state, title, url) {
window.location.pathname = url;
(window.__registeredEvents['onpopstate'] || []).forEach(cb => cb({}));
};
if (!isBrowser) {
global.window = {
location: {
pathname: '/',
search: ''
},
__registeredEvents: {},
history: {
pushState,
replaceState,
},
addEventListener(eventName, cb) {
if (!this.__registeredEvents[eventName]) {
this.__registeredEvents[eventName] = [];
}
this.__registeredEvents[eventName].push(cb);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment