-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
55 lines (48 loc) · 1.52 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Plugin } from "$fresh/server.ts";
export interface Options {
/** Attempt higher priority fetch (low or high) */
priority?: boolean
/** Allowed origins to prefetch (empty allows all) */
origins?: string[]
/** Timeout after which prefetching will occur */
timeout?: number
/**
* The concurrency limit for prefetching
* @default 2
* */
throttle?: number
/** The area percentage of each link that must have entered the viewport to be fetched */
threshold?: number
/** The total number of prefetches to allow */
limit?: number
/** Time each link needs to stay inside viewport before prefetching (milliseconds) */
delay?: number
/** Option to switch from prefetching and use prerendering only */
prerender?: boolean
/** Option to use both prerendering and prefetching */
prerenderAndPrefetch?: boolean
};
const prefetch = (options: Options = {
throttle: 2
}): Plugin => {
const main = `data:application/javascript,
import { listen } from "https://esm.sh/quicklink@2.3.0";
export default function(options) {
if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") {
listen(options);
} else {
document.addEventListener("DOMContentLoaded", () => listen(options));
}
};`
return {
name: 'prefetch',
entrypoints: { main },
render: (ctx) => {
ctx.render()
return {
scripts: [{ entrypoint: 'main', state: options }]
}
}
}
}
export default prefetch