From d648f9257438952f084deb094ea4d1d2dd0c51e0 Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Mon, 24 Jun 2024 17:48:23 -0500 Subject: [PATCH 1/3] feat: Add `location` stub from prerenderer as a utility --- src/prerender.d.ts | 2 ++ src/prerender.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/prerender.d.ts b/src/prerender.d.ts index 49eb8ab..b9958bc 100644 --- a/src/prerender.d.ts +++ b/src/prerender.d.ts @@ -13,3 +13,5 @@ export default function prerender( vnode: VNode, options?: PrerenderOptions ): Promise; + +export function locationStub(path: string): void; diff --git a/src/prerender.js b/src/prerender.js index 49b841e..bc3a759 100644 --- a/src/prerender.js +++ b/src/prerender.js @@ -40,3 +40,17 @@ export default async function prerender(vnode, options) { vnodeHook = null; } } + +/** + * Update `location` to current URL so routers can use things like `location.pathname` + * + * @param {string} path - current URL path + */ +export function locationStub(path) { + const u = new URL(path, 'http://localhost'); + for (const i in u) { + try { + globalThis.location[i] = String(u[i]); + } catch {} + } +} From 29c7d6f2b22591b57470214ea0e2f6fe826e8f8c Mon Sep 17 00:00:00 2001 From: Ryan Christian <33403762+rschristian@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:59:54 -0500 Subject: [PATCH 2/3] fix: Setup `globalThis.location` & support class methods --- src/prerender.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/prerender.js b/src/prerender.js index bc3a759..be435bf 100644 --- a/src/prerender.js +++ b/src/prerender.js @@ -47,10 +47,13 @@ export default async function prerender(vnode, options) { * @param {string} path - current URL path */ export function locationStub(path) { + globalThis.location = {}; const u = new URL(path, 'http://localhost'); for (const i in u) { try { - globalThis.location[i] = String(u[i]); + globalThis.location[i] = /to[A-Z]/.test(i) + ? u[i].bind(u) + : String(u[i]); } catch {} } } From a10a90669d163c197bf9bb0c722c815b1d917cbd Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Sun, 7 Jul 2024 19:15:23 -0500 Subject: [PATCH 3/3] test: Add tests for locationStub utility --- test/location-stub.test.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/location-stub.test.js diff --git a/test/location-stub.test.js b/test/location-stub.test.js new file mode 100644 index 0000000..e78b46d --- /dev/null +++ b/test/location-stub.test.js @@ -0,0 +1,36 @@ +import { describe, it, beforeEach, expect } from '@jest/globals'; +import { locationStub } from '../src/prerender.js'; + +describe('location-stub', () => { + beforeEach(() => { + if (globalThis.location) { + delete globalThis.location; + } + }); + + it('Contains all Location instance properties', () => { + locationStub('/foo/bar?baz=qux#quux'); + + [ + // 'ancestorOrigins', // Not supported by FireFox and sees little use, but we could add an empty val if it's needed + 'hash', + 'host', + 'hostname', + 'href', + 'origin', + 'pathname', + 'port', + 'protocol', + 'search', + ].forEach(key => { + expect(globalThis.location).toHaveProperty(key); + }); + }); + + // Do we need to support `assign`, `reload`, and/or `replace`? + it('Support bound methods', () => { + locationStub('/foo/bar?baz=qux#quux'); + + expect(globalThis.location.toString()).toBe('http://localhost/foo/bar?baz=qux#quux'); + }); +});