UNPKG

siegel

Version:

Web application development ecosystem

1 lines 1.13 kB
import t from"../../../common/is/exists/index.js";import{BASENAME_UPDATE_EVENT_TYPE as e,NAVIGATION_UPDATE_EVENT_TYPE as s}from"../constants.js";import a from"../get_final_url.js";import r from"./parse_basename.js";export default((o,i)=>{let{pathname:h}=location;if(history.push=(t,e,r)=>{let o=a("string"==typeof r?r:h,t);dispatchEvent(new CustomEvent(s,{detail:{state:e,pathname:h,newPathname:o}}))&&i(o,e,t=>{let{newPathname:e,newHistoryState:s}=t;r?history.replaceState(s,"",e):history.pushState(s,"",e)})},history.setURLQuery=t=>{history.replaceState(history.state,"",`${h}?${t}`)},window.onpopstate=()=>{let{pathname:t,search:e}=location,{state:s}=history,a=t;e&&(a+=e),i(a,s,t=>{let{newHistoryState:e,newPathname:s}=t;history.replaceState(e,"",s)})},t(o)){let{finalBasename:t}=r(o,h);history.basename=t,h.startsWith(t)||history.push(h,null,!0),history.updateBasename=t=>{t&&(t.startsWith("/")?t.startsWith("//")&&(t=t.replace(/(\/){2,}/g,"/")):t="/"+t);let{basename:s}=history;return t!=s&&(history.basename=t,history.push(h.replace(s,"")||"/",{__basenameUpdate:!0,prevPath:h}),dispatchEvent(new CustomEvent(e))),t}}});