decode urls on history api, add url object support to the url rewriter

This commit is contained in:
Percs 2024-07-14 14:08:05 -05:00
parent 4fedcea1d4
commit 78813da771
2 changed files with 13 additions and 6 deletions

View file

@ -1,9 +1,8 @@
import { decodeUrl } from "./shared";
import { encodeUrl } from "./shared";
window.history.pushState = new Proxy(window.history.pushState, { window.history.pushState = new Proxy(window.history.pushState, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[3] = encodeUrl(argArray[3]); argArray[3] = decodeUrl(argArray[3]);
return Reflect.apply(target, thisArg, argArray); 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, { window.history.replaceState = new Proxy(window.history.replaceState, {
apply(target, thisArg, argArray) { apply(target, thisArg, argArray) {
argArray[3] = encodeUrl(argArray[3]); argArray[3] = decodeUrl(argArray[3]);
return Reflect.apply(target, thisArg, argArray); return Reflect.apply(target, thisArg, argArray);
}, },

View file

@ -11,7 +11,11 @@ function canParseUrl(url: string, origin?: URL) {
} }
// something is broken with this but i didn't debug it // 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) { if (!origin) {
origin = new URL(self.$scramjet.config.codec.decode(location.href.slice((location.origin + self.$scramjet.config.prefix).length))); 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 // 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)) { if (/^(#|about|data|mailto|javascript)/.test(url)) {
return url; return url;
} else if (canParseUrl(url)) { } else if (canParseUrl(url)) {