[whatwg/url] [Proposal] Specify a fragment-directive meant for UA instructions (#445)

### Background
(This use case comes from the [ScrollToTextFragment proposal](https://github.com/WICG/ScrollToTextFragment/)). This is also being discussed as part of the TAG review for this feature in w3ctag/design-reviews#392

The URL fragment is a convenient place to add "UA targeted instructions". For example, a URL with an element-id in the fragment tells the UA load the main resource and scroll the sub-resource indicated by the element-id into view. The [media fragments specification](https://www.w3.org/TR/media-frags/) adds more ways to address sub-resources. For example, a specific temporal point in a video.

The problem with fragments is that they can be read and processed by script on a page. In some cases, page script will use the fragment to perform its own routing and state tracking. This usage may not interact well with unexpected fragments meant as instructions to the UA. To take a real-world example:

https://www.webmd.com/pain-management/knee-pain/picture-of-the-knee

Navigating to the above URL will load a multi-page article routed using the fragment. If the user tries to load the same URL but with an element-id fragment:

https://www.webmd.com/pain-management/knee-pain/picture-of-the-knee#ContentPane50

The page loads blank because script in the page gets confused.

### Proposal

_We propose amending the URL specification to allow a `##` to indicate a **fragment directive**_. A fragment directive would be a part of the URL reserved for UA instructions that's stripped off during loading before being passed to the page. Taking the example above mixed with our `targetText` feature:

https://www.webmd.com/pain-management/knee-pain/picture-of-the-knee##targetText=Knee%20Conditions

In this case, the UA processes the targetText and strips it from the URL as seen by the page. From the page's point of view:

```
window.location.href === 'https://www.webmd.com/pain-management/knee-pain/picture-of-the-knee'
window.location.hash === ''
document.URL === 'https://www.webmd.com/pain-management/knee-pain/picture-of-the-knee'
```

It can also be combined with plain plain fragments:

https://example.com#routingState-page1##targetText=Header

In which case the page sees:

```
window.location.href === 'https://example.com#routingState-page1'
window.location.hash === '#routingState-page1'
```

It also has the benefit of likely being parsed as part of the fragment in URL parsers and won't affect the server request so it should be _mostly_ backwards compatible. The major sticking point is that '#' is currently [not a valid code point](https://url.spec.whatwg.org/#url-writing) in URL fragments, something we'd have to change. We're currently trying to determine how compatible this would be; one way is measuring how often we already see URLs with a '#' in the fragment. There's existing discussion in WICG/ScrollToTextFragment#15.

This would be very helpful in ScrollToTextFragment but we can also imagine it being useful in other new features. For example, as an alternative solution to the [html-translate proposal](https://github.com/dtapuska/html-translate).

Are there any issues we may be overlooking that would make this difficult or undesirable to pursue?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/url/issues/445

Received on Friday, 16 August 2019 16:35:05 UTC