Search and Hash in URLs

Sometimes we parameterize a #URL to specify in detail what to display.
- The "search" (after a "?") can be used by the server but also by the client, e.g., for routing in a single-page application.
- The "hash" (after a "#") is only available to the client, where it is normally used to scroll to a particular element of a page. But it can also be used for client-side routing.

@w3c @tag #WebStandards #WebDevelopers

1/3

2/3

Now I have a use case with client-side routing parameters where
A. I would prefer to keep the parameters in the client for data-protection reasons, and
B. I would still like to use the hash for scrolling within the page.

So where to put the parameters? Due to (A) I don't want them in the search and due to (B) I don't want them in the hash.

Any ideas, #WebDevelopers ?
Or at @w3c @tag #WebStandards ?

3/3

Well, I can put both the routing parameters and the scroll target in the hash and implement the scrolling in JS. But that feels more complex than necessary.

Is there a standard solution for this? Or should we have yet another URL component that
- is only available to the client and
- does not interfere with the scrolling functionality of the hash component?

@w3c @tag #WebStandards #WebDevelopers

@hcschuetz @w3c @tag It's an interesting question. For now, I think your choices are hash+JS scrolling, or path/query+Service Worker to hide it from the server.

Thanks for your reply, @jyasskin. I'm afraid you're right.

Currently I'm essentially following the first of these choices. A while ago I actually experimented with the service-worker approach but gave it up because the service worker could only be started after the first request and so the query of the first request was not hidden.

Oh, and I forgot to mention: Omitting the parameters from the request to the server is not only privacy-friendly but also cache-friendly.

@w3c @tag

@hcschuetz @tag You might use the :~: fragment directive as in https://en.wikipedia.org/wiki/Calque#Types:~:route=foobar.
This will probably take some finessing for a navigation to a client-side rendered page, but I think it'll work for an intra-page navigation. (This comes from https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Fragment/Text_fragments, which was designed to be extensible.)

I'm not sure if this is going to be a good idea in the long run. @nomster @dbaron @jaffathecake do you have any sense of whether this is a good idea for scrolling inside a client-side rendered page?

Calque - Wikipedia

@jyasskin

Yes, that is an interesting approach.

Currently I am parsing and serializing the hash with URLSearchParams.
For example, in https://hcschuetz.github.io/follow-toots/dist/tree.html#instance=mastodon.social&id=116075812807473585&focus=116076988328849782 the parameters `instance` and `id` are routing parameters (telling what to display) whereas `focus` is for scrolling (and focusing) within the page.

(It took me a little while to get it in demonstratable shape.)

@tag @nomster @dbaron @jaffathecake

Follow Toot

If we could write the hash part as

...#116076988328849782:~:instance=mastodon.social&id=116075812807473585

and had an API for reading/writing the hash components before and after the :~: *independently*, this could simplify my code a bit.

Notes:
- Changes to the first part would have to trigger scrolling.
- Links inside the page (say from a child toot to its parent in the example)
would leave the part after the :~: unchanged.

@jyasskin @tag @nomster @dbaron @jaffathecake

A question about data protection - are you OK with those hashes being sharable? Because despite not being sent to the server they would appear when sharing the URL peer to peer...

Aside from that, ignoring the :~:... stuff when finding a scroll target sounds like a good idea. Worth filing an issue on https://github.com/whatwg/html

GitHub - whatwg/html: HTML Standard

HTML Standard. Contribute to whatwg/html development by creating an account on GitHub.

GitHub