Skip to content

Commit

Permalink
fix: allow logging $page.url when url.search is disabled (#10567)
Browse files Browse the repository at this point in the history
fixes #10526

We were allow logging when url.hash is disabled but not when url.search is disabled. Just copied over the same thing for when url.search is disabled.
  • Loading branch information
eltigerchino committed Aug 16, 2023
1 parent 7f214df commit 32afba6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-mayflies-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow logging `$page.url` during prerendering
17 changes: 17 additions & 0 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export function make_trackable(url, callback) {
* @param {URL} url
*/
export function disable_hash(url) {
allow_nodejs_console_log(url);

Object.defineProperty(url, 'hash', {
get() {
throw new Error(
Expand All @@ -153,6 +155,8 @@ export function disable_hash(url) {
* @param {URL} url
*/
export function disable_search(url) {
allow_nodejs_console_log(url);

for (const property of ['search', 'searchParams']) {
Object.defineProperty(url, property, {
get() {
Expand All @@ -162,6 +166,19 @@ export function disable_search(url) {
}
}

/**
* Allow URL to be console logged, bypassing disabled properties.
* @param {URL} url
*/
function allow_nodejs_console_log(url) {
if (!BROWSER) {
// @ts-ignore
url[Symbol.for('nodejs.util.inspect.custom')] = (depth, opts, inspect) => {
return inspect(new URL(url), opts);
};
}
}

const DATA_SUFFIX = '/__data.json';

/** @param {string} pathname */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const prerender = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { page } from '$app/stores';
let error = false;
try {
console.log($page);
} catch (e) {
error = true;
}
</script>

<p>error: {error}</p>
6 changes: 6 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ test.describe('Load', () => {
'{"message":"Im prerendered and called from a non-prerendered +page.server.js"}'
);
});

test('Logging $page.url during prerendering works', async ({ page }) => {
await page.goto('/prerendering/log-url');

expect(await page.textContent('p')).toBe('error: false');
});
});

test.describe('Nested layouts', () => {
Expand Down

0 comments on commit 32afba6

Please sign in to comment.