Closed hyunbinseo closed 1 month ago
You're trying to access url.searchParams directly, but you should use the 'get' method to access the value of a specific parameter.
Example:
export async function load({ url }) {
console.log(url.searchParams.get('parameterName'))
}
Can't the URL's searchParams property be accessed directly?
The following code works in Node.js v20.10.0
const url = new URL('https://example.com?foo=1&bar=2');
const params1 = new URLSearchParams(url.search);
console.log(params1);
// URLSearchParams { 'foo' => '1', 'bar' => '2' }
console.log(url.searchParams);
// URLSearchParams { 'foo' => '1', 'bar' => '2' }
This solution logs in the console the result you're trying to see:
export async function load({ url }) {
const params = new URLSearchParams(url.search);
console.log(params)
//URLSearchParams { 'foo' => '1', 'bar' => 2'' }
}
but if you want to retrieve the value you should treat it similarly to a map
console.log(url.searchParams.entries())
//URLSearchParams Iterator { [ 'foo', '1' ], [ 'bar', '2' ] }
console.log(url.searchParams.keys())
//URLSearchParams Iterator { 'foo', 'bar' }
console.log(url.searchParams.values())
//URLSearchParams Iterator { '1', '2' }
Check this links out to clear any doubt you might still have: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams https://kit.svelte.dev/docs/load#using-url-data
You really don't get anything useful from logging url.searchParams. The output will be URLSearchParams {}
on the server. However, this is still a bug since kit shouldn't crash from this.
EDIT: I'm sorry. It does contain useful info
Having this bug as well, would disagree it's not useful to log all the search params. Sometimes you just wanna see what you're working with then everything crashes. Maybe it could not do that?
Yeah url.searchParams should be loggable, even if it shows the iterable size URLSearchParams {size: 1}
Crashing when trying to log is not ideal, neither node nor the browser error on this.
Thanks everyone for the persistence on this. Found a fix and it indeed does contain useful information when logged.
Describe the bug
The following server load function fails in a Node.js environment.
Works in a universal load function and in StackBlitz environment.
Reproduction
Logs
System Info
Severity
annoyance
Additional Information
No response