prefetch
import "drab/prefetch/define";
<drab-prefetch>
<a data-trigger href="/">Hover to prefetch</a>
</drab-prefetch>
<!-- Wrap anchors and use a `trigger` selector. -->
<div>
<drab-prefetch trigger="a[href^='/']" strategy="visible" prerender>
<a href="/">Prerender when visible</a>
</drab-prefetch>
</div>
<!-- Prefetch a `url` without providing a trigger. -->
<drab-prefetch url="/" class="hidden"></drab-prefetch>
Overview
Defined in: prefetch/index.ts:73
The Prefetch
element can prefetch a url, or enhance the HTMLAnchorElement
by loading the HTML for a page before it is navigated to. This element speeds up the navigation for multi-page applications (MPAs).
If you are using a framework that already has a prefetch feature or uses a client side router, it is best to use the framework’s feature instead of this element to ensure prefetching is working in accordance with the router.
Attributes
strategy
Set the strategy
attribute to specify the when the prefetch will take place.
"hover"
- (default) Aftermouseover
,focus
, ortouchstart
for > 200ms"visible"
- Uses an intersection observer to prefetch when the anchor is within the viewport."load"
- Immediately prefetches when the element is loaded, use carefully.
prerender
Use the prerender
attribute to use the Speculation Rules API when supported to prerender on the client. This allows you to run client side JavaScript in advance instead of only fetching the HTML.
Browsers that do not support will still use <link rel="prefetch">
instead.
url
Add a url
attribute to immediately prefetch a url without having to provide
(or in addition to) trigger
anchor elements.
This element can be deprecated once the Speculation Rules API is supported across browsers. The API will be able to prefetch assets in a similar way with the source: "document"
and eagerness
features, and will work without JavaScript.
Constructors
Constructor
new Prefetch():
Prefetch
Defined in: prefetch/index.ts:76
Returns
Prefetch
Overrides
Lifecycle(Trigger()).constructor
Accessors
event
Get Signature
get event(): keyof
HTMLElementEventMap
Defined in: base/index.ts:120
Event for the trigger
to execute.
For example, set to "mouseover"
to execute the event when the user hovers the mouse over the trigger
, instead of when they click it.
Default
"click";
Returns
keyof HTMLElementEventMap
Set Signature
set event(
value
):void
Defined in: base/index.ts:126
Parameters
value
keyof HTMLElementEventMap
Returns
void
Inherited from
Lifecycle(Trigger()).event
Methods
connectedCallback()
connectedCallback():
void
Defined in: base/index.ts:76
Called when custom element is added to the page.
Returns
void
Inherited from
Lifecycle(Trigger()).connectedCallback
destroy()
destroy():
void
Defined in: base/index.ts:83
Passed into disconnectedCallback
, since Base
needs to run disconnectedCallback
as well. It is overridden in each element that needs to run disconnectedCallback
.
Returns
void
Inherited from
Lifecycle(Trigger()).destroy
disconnectedCallback()
disconnectedCallback():
void
Defined in: base/index.ts:86
Called when custom element is removed from the page.
Returns
void
Inherited from
Lifecycle(Trigger()).disconnectedCallback
listener()
Call Signature
listener<
T
>(listener
,options?
):void
Defined in: base/index.ts:152
Type Parameters
T
T
extends keyof HTMLElementEventMap
Parameters
listener
Listener
<T
>
Listener to attach to all of the trigger
elements.
options?
AddEventListenerOptions
Returns
void
Inherited from
Lifecycle(Trigger()).listener
Call Signature
listener<
T
>(type
,listener
,options?
):void
Defined in: base/index.ts:161
Type Parameters
T
T
extends keyof HTMLElementEventMap
Parameters
type
T
Event type.
listener
Listener
<T
>
Listener to attach to all of the trigger
elements.
options?
AddEventListenerOptions
Returns
void
Inherited from
Lifecycle(Trigger()).listener
mount()
mount():
void
Defined in: prefetch/index.ts:143
Passed into queueMicrotask
in connectedCallback
.
It is overridden in each component that needs to run connectedCallback
.
The reason for this is to make these elements work better with frameworks like Svelte.
For SSR this isn’t necessary, but when client side rendering, the HTML within the
custom element isn’t available before connectedCallback
is called. By waiting until
the next microtask, the HTML content is available—then for example, listeners can
be attached to elements inside.
Returns
void
Overrides
Lifecycle(Trigger()).mount
prefetch()
prefetch(
options
):void
Defined in: prefetch/index.ts:101
Appends <link rel="prefetch">
or <script type="speculationrules">
to the head of the document.
Parameters
options
Configuration options.
prerender?
boolean
Uses the Speculation Rules API when supported to prerender on the client.
url
string
url
to prefetch.
Returns
void
safeListener()
Call Signature
safeListener<
T
>(type
,listener
,element?
,options?
):void
Defined in: base/index.ts:35
Wrapper around addEventListener
that ensures when the element is
removed from the DOM, these event listeners are cleaned up.
Type Parameters
T
T
extends keyof HTMLElementEventMap
Parameters
type
T
Event listener type - ex: "keydown"
listener
(this
, event
) => any
Listener to add to the target.
element?
HTMLElement
options?
AddEventListenerOptions
Other options sans signal
.
Returns
void
Inherited from
Lifecycle(Trigger()).safeListener
Call Signature
safeListener<
T
>(type
,listener
,document
,options?
):void
Defined in: base/index.ts:41
Wrapper around addEventListener
that ensures when the element is
removed from the DOM, these event listeners are cleaned up.
Type Parameters
T
T
extends keyof DocumentEventMap
Parameters
type
T
Event listener type - ex: "keydown"
listener
(this
, event
) => any
Listener to add to the target.
document
Document
options?
AddEventListenerOptions
Other options sans signal
.
Returns
void
Inherited from
Lifecycle(Trigger()).safeListener
Call Signature
safeListener<
T
>(type
,listener
,window
,options?
):void
Defined in: base/index.ts:47
Wrapper around addEventListener
that ensures when the element is
removed from the DOM, these event listeners are cleaned up.
Type Parameters
T
T
extends keyof WindowEventMap
Parameters
type
T
Event listener type - ex: "keydown"
listener
(this
, event
) => any
Listener to add to the target.
window
Window
options?
AddEventListenerOptions
Other options sans signal
.
Returns
void
Inherited from
Lifecycle(Trigger()).safeListener
triggers()
Call Signature
triggers<
T
>(instance
):NodeListOf
<T
>
Defined in: base/index.ts:136
Type Parameters
T
T
extends HTMLElement
Parameters
instance
Constructor
<T
>
The instance of the desired element to validate against,
ex: HTMLButtonElement
. Defaults to HTMLElement
.
Returns
NodeListOf
<T
>
All of the elements that match the trigger
selector.
Default
this.querySelectorAll("[data-trigger]");
Inherited from
Lifecycle(Trigger()).triggers
Call Signature
triggers():
NodeListOf
<HTMLElement
>
Defined in: base/index.ts:137
Returns
NodeListOf
<HTMLElement
>
All of the elements that match the trigger
selector.
Default
this.querySelectorAll("[data-trigger]");
Inherited from
Lifecycle(Trigger()).triggers