init
This commit is contained in:
96
src/index.ts
Normal file
96
src/index.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import App from "./components/App.svelte"
|
||||
import { location } from "./store"
|
||||
import { apiBaseURL } from "./config"
|
||||
|
||||
console.log("API Base: ", apiBaseURL)
|
||||
|
||||
// update location store
|
||||
const publishLocation = (_p?: string) => {
|
||||
let _s: string
|
||||
if (_p) {
|
||||
const parts = _p.split("?")
|
||||
_p = parts.shift()
|
||||
_s = parts.join()
|
||||
if (_s) _s = "?" + _s
|
||||
}
|
||||
|
||||
const newLocation = {
|
||||
path:
|
||||
_p || (typeof window !== "undefined" && window.location?.pathname),
|
||||
search:
|
||||
_s || (typeof window !== "undefined" && window.location?.search),
|
||||
push: !!_p,
|
||||
pop: !_p,
|
||||
categoryPath: "",
|
||||
}
|
||||
newLocation.categoryPath = newLocation.path.replace(/\/\d{4,99}[^\/]+$/, "")
|
||||
location.set(newLocation)
|
||||
}
|
||||
|
||||
// history proxy for location store update
|
||||
if (typeof history !== "undefined") {
|
||||
if (typeof Proxy !== "undefined") {
|
||||
// modern browser
|
||||
const historyApply = (target, thisArg, argumentsList) => {
|
||||
publishLocation(
|
||||
argumentsList && argumentsList.length >= 2 && argumentsList[2]
|
||||
)
|
||||
Reflect.apply(target, thisArg, argumentsList)
|
||||
}
|
||||
|
||||
history.pushState = new Proxy(history.pushState, {
|
||||
apply: historyApply,
|
||||
})
|
||||
|
||||
history.replaceState = new Proxy(history.replaceState, {
|
||||
apply: historyApply,
|
||||
})
|
||||
} else {
|
||||
// ie11
|
||||
const pushStateFn = history.pushState
|
||||
const replaceStateFn = history.replaceState
|
||||
|
||||
history.pushState = function (data: any, title: string, url?: string) {
|
||||
publishLocation(url)
|
||||
return pushStateFn.apply(history, arguments)
|
||||
}
|
||||
history.replaceState = function (
|
||||
data: any,
|
||||
title: string,
|
||||
url?: string
|
||||
) {
|
||||
publishLocation(url)
|
||||
return replaceStateFn.apply(history, arguments)
|
||||
}
|
||||
}
|
||||
} // else ssr -> no history handling
|
||||
|
||||
typeof window !== "undefined" &&
|
||||
window.addEventListener("popstate", (event) => {
|
||||
publishLocation()
|
||||
})
|
||||
|
||||
let appContainer = document?.getElementById("appContainer")
|
||||
|
||||
const hydrate = true //import.meta?.env?.MODE !== "development"
|
||||
console.log("Features: ", { hydrate })
|
||||
const app = new App({
|
||||
target: appContainer,
|
||||
props: {},
|
||||
hydrate,
|
||||
})
|
||||
|
||||
export default app
|
||||
|
||||
// PWA
|
||||
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
|
||||
console.log("Registering service worker")
|
||||
navigator.serviceWorker
|
||||
.register("/serviceworker.js", { scope: "/" })
|
||||
.then((reg) => {
|
||||
console.log("Registration succeeded. Scope is " + reg.scope)
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log("Registration failed:", e)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user