Skip to content

Commit

Permalink
feat: Add location stub from prerenderer as a utility
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Jun 27, 2024
1 parent bd83e97 commit 00b1cd9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/prerender.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export default function prerender(
vnode: VNode,
options?: PrerenderOptions
): Promise<PrerenderResult>;

export function locationStub(path: string): void;
14 changes: 14 additions & 0 deletions src/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
}

0 comments on commit 00b1cd9

Please sign in to comment.