liveBook · Manning

Url fragment

As explained in Vanilla Web

A hash fragment is the fragment identifier following a URL's hash mark that identifies a target within a document.

A URL fragment is the part after `#`. It points within the current document: `/products/coffee-beans#reviews` identifies the reviews fragment of that page. The path identifies the resource; the query string can hold a shareable variation of its view; the fragment identifies a target inside it. section 10.1.8 section 2.1.2

Use descriptive paths for application routes and the query string for shareable state such as sorting or pagination. Reserve the hash for in-page navigation rather than making `/#/products/42` the primary route. Fragment-based routing complicates server integration and weakens the URL's meaning; descriptive paths are also preferred for discoverability. section 10.1.8 section 10.1.9

Text fragment

`#:~:text=espresso` is a fragment that targets text in a page. The portion after `:~:` is a hash directive. When inspecting such a URL, note that `location.hash` may omit directive content, while `new URL(url).hash` returns the full hash segment. section 10.1.8 section 2.1.2

How Vanilla Web covers it

How deep it goes
Defines fragments, contrasts them with paths and queries, and notes a text-fragment API caveat without building fragment navigation.
Examples
Uses `#reviews`, `#:~:text=espresso`, and `/#/products/42` to distinguish document targets from fragment-based routes.
What it assumes
Defines URL components with concrete examples rather than assuming familiarity with their names.
What you can do after
Evaluate/understand when to use a fragment instead of a path or query string, including the implications of hash routing.
How it connects
Connects URL fragments to in-page navigation, text fragments, query strings, routes, and the History API.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.