URL serialization (source code)

= URL serialization
{c}

<URL serialization> produces the textual spelling of a structured <URL record>. It is the output side of processing that begins with a <URL parser>. The WHATWG serializer returns an <ASCII> string and can optionally omit the <fragment identifier>.<URL Standard by WHATWG>[Section4.5, URL serializing, including exclude-fragment parameter]{ref}

A worked example separates the stages. Constructing `new URL('https://EXAMPLE.org:443/a/../b?q=hello%20world~#top')` gives `href` equal to `https://example.org/b?q=hello%20world~#top`. Lowercasing the host, removing the default port and resolving the parent-directory segment happen during parsing; the serializer exposes the resulting record.

Now call `u.searchParams.sort()` on that object. Even though there is only one query pair to sort, `href` becomes `https://example.org/b?q=hello+world%7E#top`. The decoded query value is unchanged, but <URLSearchParams> has rewritten its spelling using <form URL encoding>. Thus changing the query through an API can alter its <percent-encoding> without changing the value returned by that API.<URL API in the Node.js documentation>[url.searchParams: query-update and encoding caveat; URLSearchParams.sort]{ref}

A serialized address is also different from a browser's chosen display text or a server's returned content. Comparing those different outputs answers different questions.