feat: support passing URL to islands (#3181)

Adds support for passing `URL` values to Islands.

```tsx
// routes/index.html
export default function Page() {
  return <MyIsland url={new URL("https://fresh.deno.dev")} />;
}
```

Also updates documentation with some missing serialization features
(`undefined`, `Map`, `Set`, and `-0`).

---------

Co-authored-by: Marvin Hagemeister <hello@marvinh.dev>
This commit is contained in:
Christian Svensson
2025-08-03 20:42:50 +02:00
committed by GitHub
co-authored by Marvin Hagemeister
parent 03f6981860
commit 57596dc406
5 changed files with 14 additions and 3 deletions
+5 -2
View File
@@ -40,12 +40,15 @@ const app = new App()
Passing props to islands is supported, but only if the props are serializable. Passing props to islands is supported, but only if the props are serializable.
Fresh can serialize the following types of values: Fresh can serialize the following types of values:
- Primitive types `string`, `number`, `boolean`, `bigint`, and `null` - Primitive types `string`, `number`, `boolean`, `bigint`, `undefined`, and
- `Infinity`, `-Infinity`, and `NaN` `null`
- `Infinity`, `-Infinity`, `-0`, and `NaN`
- `Uint8Array` - `Uint8Array`
- `URL`
- `Date` - `Date`
- `RegExp` - `RegExp`
- `JSX` Elements - `JSX` Elements
- Collections `Map` and `Set`
- Plain objects with string keys and serializable values - Plain objects with string keys and serializable values
- Arrays containing serializable values - Arrays containing serializable values
- Preact Signals (if the inner value is serializable) - Preact Signals (if the inner value is serializable)
@@ -40,6 +40,8 @@ snapshot[`round trip - { a: 1, b: null, c: -2 } 1`] = `'[{"a":1,"b":-2,"c":2},1,
snapshot[`round trip - Uint8Array(3) [ 1, 2, 3 ] 1`] = `'[["Uint8Array","AQID"]]'`; snapshot[`round trip - Uint8Array(3) [ 1, 2, 3 ] 1`] = `'[["Uint8Array","AQID"]]'`;
snapshot[`round trip - URL {\\n href: 'https://fresh.deno.dev/',\\n origin: 'https://fresh.deno.dev',\\n protocol: 'https:',\\n username: '',\\n password: '',\\n host: 'fresh.deno.dev',\\n hostname: 'fresh.deno.dev',\\n port: '',\\n pathname: '/',\\n hash: '',\\n search: ''\\n} 1`] = `'[["URL","https://fresh.deno.dev/"]]'`;
snapshot[`round trip - 1990-05-31T00:00:00.000Z 1`] = `'[["Date","1990-05-31T00:00:00.000Z"]]'`; snapshot[`round trip - 1990-05-31T00:00:00.000Z 1`] = `'[["Date","1990-05-31T00:00:00.000Z"]]'`;
snapshot[`round trip - Map(2) { 1 => null, undefined => -2 } 1`] = `'[["Map",[1,-2,-1,2]],1,-2]'`; snapshot[`round trip - Map(2) { 1 => null, undefined => -2 } 1`] = `'[["Map",[1,-2,-1,2]],1,-2]'`;
+2
View File
@@ -65,6 +65,8 @@ function unpack(
switch (name) { switch (name) {
case "BigInt": case "BigInt":
return hydrated[idx] = BigInt(current[1]); return hydrated[idx] = BigInt(current[1]);
case "URL":
return hydrated[idx] = new URL(current[1]);
case "Date": case "Date":
return hydrated[idx] = new Date(current[1]); return hydrated[idx] = new Date(current[1]);
case "RegExp": case "RegExp":
+1
View File
@@ -32,6 +32,7 @@ const TESTS = [
[null, undefined, -2], [null, undefined, -2],
{ a: 1, b: null, c: -2 }, { a: 1, b: null, c: -2 },
new Uint8Array([1, 2, 3]), new Uint8Array([1, 2, 3]),
new URL("https://fresh.deno.dev"),
new Date("1990-05-31"), new Date("1990-05-31"),
new Map([[1, null], [undefined, -2]]), new Map([[1, null], [undefined, -2]]),
new Set([1, 2, null, -2, NaN]), new Set([1, 2, null, -2, NaN]),
+4 -1
View File
@@ -26,6 +26,7 @@ export type Stringifiers = Record<
* - `array` * - `array`
* - `object` (no prototypes) * - `object` (no prototypes)
* - `Uint8Array` * - `Uint8Array`
* - `URL`
* - `Date` * - `Date`
* - `RegExp` * - `RegExp`
* - `Set` * - `Set`
@@ -104,7 +105,9 @@ function serializeInner(
} }
} }
if (value instanceof Date) { if (value instanceof URL) {
str += `["URL","${value.href}"]`;
} else if (value instanceof Date) {
str += `["Date","${value.toISOString()}"]`; str += `["Date","${value.toISOString()}"]`;
} else if (value instanceof RegExp) { } else if (value instanceof RegExp) {
str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`; str += `["RegExp",${JSON.stringify(value.source)}, "${value.flags}"]`;