MCPcopy
hub / github.com/barbajs/barba / Prefetch

Class Prefetch

packages/prefetch/src/prefetch.ts:22–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20import { requestIdleCallback } from './polyfills';
21
22class Prefetch implements IBarbaPlugin<IPrefetchOptions> {
23 public name = '@barba/prefetch';
24 public version = version;
25 public barba: Core;
26 public logger: Logger;
27
28 public observer: IntersectionObserver;
29 public root: HTMLElement | HTMLDocument;
30 public timeout: number;
31 public limit: number;
32 public toPrefetch: Set<string> = new Set();
33
34 /**
35 * Plugin installation.
36 */
37 public install(
38 barba: Core,
39 { root = document.body, timeout = 2e3, limit = 0 }: IPrefetchOptions = {}
40 ) {
41 this.logger = new barba.Logger(this.name);
42 this.logger.info(this.version);
43 this.barba = barba;
44 this.root = root;
45 this.timeout = timeout;
46 this.limit = limit;
47 }
48
49 /**
50 * Plugin initialisation.
51 */
52 public init() {
53 if (this.barba.prefetchIgnore) {
54 this.logger.warn('barba.prefetchIgnore is enabled');
55
56 return;
57 }
58 if (this.barba.cacheIgnore) {
59 this.logger.warn('barba.cacheIgnore is enabled');
60
61 return;
62 }
63
64 /**
65 * Init intersection observer
66 * when intersecting, it will check if URL should be prefetched
67 * then unobserve the element
68 * and, if no cache data, fetch the page
69 */
70 /* istanbul ignore next */
71 this.observer = new IntersectionObserver(entries => {
72 entries.forEach(entry => {
73 if (!entry.isIntersecting) {
74 return;
75 }
76
77 const link = entry.target as Link;
78 const href = this.barba.url.getAbsoluteHref(this.barba.dom.getHref(link));
79

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected