@indexea/scaffold-svelte
Version:
This is the scaffolding of Indexea search, on which you can adjust the typography and style of the search interface
37 lines (27 loc) • 987 B
JavaScript
import { derived, writable } from 'svelte/store'
export function createUrlStore (ssrUrl) {
// Ideally a bundler constant so that it's tree-shakable
if (typeof window === 'undefined') {
const { subscribe } = writable(ssrUrl)
return { subscribe }
}
const href = writable(window.location.href)
const originalPushState = history.pushState
const originalReplaceState = history.replaceState
const updateHref = () => href.set(window.location.href)
history.pushState = function () {
originalPushState.apply(this, arguments)
updateHref()
}
history.replaceState = function () {
originalReplaceState.apply(this, arguments)
updateHref()
}
window.addEventListener('popstate', updateHref)
window.addEventListener('hashchange', updateHref)
return {
subscribe: derived(href, $href => new URL($href)).subscribe
}
}
// If you're using in a pure SPA, you can return a store directly and share it everywhere
export default createUrlStore()