diff --git a/src/client/history.ts b/src/client/history.ts index c36e0c0..0bc2e2f 100644 --- a/src/client/history.ts +++ b/src/client/history.ts @@ -1,9 +1,8 @@ - -import { encodeUrl } from "./shared"; +import { decodeUrl } from "./shared"; window.history.pushState = new Proxy(window.history.pushState, { apply(target, thisArg, argArray) { - argArray[3] = encodeUrl(argArray[3]); + argArray[3] = decodeUrl(argArray[3]); return Reflect.apply(target, thisArg, argArray); }, @@ -12,7 +11,7 @@ window.history.pushState = new Proxy(window.history.pushState, { window.history.replaceState = new Proxy(window.history.replaceState, { apply(target, thisArg, argArray) { - argArray[3] = encodeUrl(argArray[3]); + argArray[3] = decodeUrl(argArray[3]); return Reflect.apply(target, thisArg, argArray); }, diff --git a/src/shared/rewriters/url.ts b/src/shared/rewriters/url.ts index c596ffd..00153c8 100644 --- a/src/shared/rewriters/url.ts +++ b/src/shared/rewriters/url.ts @@ -11,7 +11,11 @@ function canParseUrl(url: string, origin?: URL) { } // something is broken with this but i didn't debug it -export function encodeUrl(url: string, origin?: URL) { +export function encodeUrl(url: string | URL, origin?: URL) { + if (url instanceof URL) { + url = url.toString() + } + if (!origin) { origin = new URL(self.$scramjet.config.codec.decode(location.href.slice((location.origin + self.$scramjet.config.prefix).length))); } @@ -26,7 +30,11 @@ export function encodeUrl(url: string, origin?: URL) { } // something is also broken with this but i didn't debug it -export function decodeUrl(url: string) { +export function decodeUrl(url: string | URL) { + if (url instanceof URL) { + url = url.toString() + } + if (/^(#|about|data|mailto|javascript)/.test(url)) { return url; } else if (canParseUrl(url)) {