Skip to content

Commit 16c6d5a

Browse files
committed
Clarify that target_hint is not necessary for document rules
See https://groups.google.com/a/chromium.org/g/blink-dev/c/am_noPAIH5k/m/RoRpeufNAAAJ.
1 parent 992d70d commit 16c6d5a

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

triggers.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,38 @@ We require that user agents discard any rules with requirements that are not rec
143143

144144
### Window name targeting hints
145145

146-
It is desired by developers to further maximize pre-rendering capability by supporting prerendering pages into different windows. However, for implementation reasons, it can be difficult for user agents to prerender content without knowing which window it will end up in. For example, the browser might prerender content assuming that it will be activated in the current window, replacing the current content, but then the user might end up clicking on an `<a href="page.html" target="_blank">` link, so the prerendered content is wasted.
146+
It is desired by developers to further maximize prerendering capability by supporting prerendering pages into different windows. However, for implementation reasons, it can be difficult for user agents to prerender content without knowing which window it will end up in. For example, the browser might prerender content assuming that it will be activated in the current window, replacing the current content, but then the user might end up clicking on an `<a href="page.html" target="_blank">` link, or the web developer might open the page using `window.open('page.html')`, causing the prerendered content is wasted.
147147

148-
To help with this, `"prerender"` rules can have a `"target_hint"` field, which contains a [valid browsing context name or keyword](https://html.spec.whatwg.org/#valid-browsing-context-name-or-keyword) indicating where the page expects the prerendered content to be activated. (The name "target" comes from the HTML `target=""` attribute on hyperlinks.)
148+
When using [document rules](#document-rules) that target `<a>` elements, the browser can infer the window name target automatically:
149149

150150
```html
151151
<script type=speculationrules>
152152
{
153153
"prerender": [{
154-
"target_hint": "_blank",
155-
"urls": ["page.html"]
154+
"where": { "selector_matches": ".prerender" }
155+
}]
156+
}
157+
</script>
158+
<a href="page.html" target="_blank" class="prerender">Click me</a>
159+
```
160+
161+
However, when using [list rules](#list-rules), where the developer prerenders specific URLs (e.g., in anticipation of them being opened from JavaScript), a bit more setup is needed. For such cases, `"prerender"` rules can have a `"target_hint"` field, which contains a [valid navigable target name or keyword](https://html.spec.whatwg.org/#valid-navigable-target-name-or-keyword) indicating where the page expects the prerendered content to be activated. (The name "target" comes from the HTML `target=""` attribute on hyperlinks.)
162+
163+
```html
164+
<script type=speculationrules>
165+
{
166+
"prerender": [{
167+
"urls": ["page.html"],
168+
"target_hint": "_blank"
156169
}]
157170
}
158171
</script>
159-
<a target="_blank" href="page.html">click me</a>
172+
<button onclick="window.open('page.html')">Click me</a>
160173
```
161174

162-
This is just a hint, and is not binding on the implementation. Indeed, we hope that it one day becomes unnecessary, and all implementations can activate prerendered content into any target window. At that point, the field can be safely ignored, and removed from the specification. But at least for Chromium, getting to that point might take a year or so of engineering effort, so in the meantime `"target_hint"` gives developers a way to use prerendering in combination with new windows.
175+
This is just a hint, and is not binding on the implementation. It's possible one day it will not be necessary for any implementations. At that point, the field can be safely ignored, and removed from the specification. But at least for Chromium, the engineering work for creating prerendering contexts which can be activated into any tab is currently infeasible, so in the meantime `"target_hint"` gives developers a way to use prerendering in combination with new windows.
163176

164-
To check Chromium's progress on removing the `target_hint` field, see [this issue](https://crbug.com/361129302).
177+
(To check Chromium's progress on eventually removing the `target_hint` field, see [this issue](https://crbug.com/361129302).)
165178

166179
Note that if a page is truly unsure whether a given URL will be prerendered into the current window or a new one, they could include prerendering rules for multiple target windows:
167180

0 commit comments

Comments
 (0)