From ad4b32a5f4d04576245c57cc6029b9b41a8007bb Mon Sep 17 00:00:00 2001 From: gimenes Date: Mon, 24 Apr 2023 12:40:19 -0300 Subject: [PATCH 1/2] try manual --- README.md | 3 +++ mod.ts | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 21ee93e..77d92b3 100644 --- a/README.md +++ b/README.md @@ -61,5 +61,8 @@ all links but those containing `data-noprefetch`. For instance will prefetch `/bar`. +`manual` disables listening and requires you to call `window.QuickLink.prefetch` +manually + Not setting `options.strategy` will make this plugin fallback to the usual quicklink's behavior, i.e. viewport based prefetching diff --git a/mod.ts b/mod.ts index 4251f9c..e1ddbe0 100644 --- a/mod.ts +++ b/mod.ts @@ -27,18 +27,21 @@ export interface Options { * * set opt-in to prefetch only anchor tags containing the data-prefetch attribute * set opt-out to ignore only those links with the data-noprefetch attribute + * set manual to disable listening and fallback to calling prefetch manually * set aggresive to prefetch all links * * @default undefined */ - strategy?: "opt-in" | "opt-out"; + strategy?: "opt-in" | "opt-out" | "manual"; } const prefetch = (options: Options = { throttle: 10, }): Plugin => { const main = `data:application/javascript, - import { listen as qlListen } from "https://esm.sh/quicklink@2.3.0"; + import * as QuickLink from "https://esm.sh/quicklink@2.3.0"; + + window.QuickLink = QuickLink; function getOptions (options) { if (options.strategy) { @@ -54,10 +57,14 @@ const prefetch = (options: Options = { }; function listen (options) { - return qlListen(getOptions(options)) + return QuickLink.listen(getOptions(options)) }; export default function(options) { + if (options.strategy === "manual") { + return; + } + if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") { listen(options); } else { From 9ce01aff6438b7fa52131896e3e950a3056c6ce2 Mon Sep 17 00:00:00 2001 From: gimenes Date: Mon, 24 Apr 2023 14:20:23 -0300 Subject: [PATCH 2/2] lowercase --- README.md | 2 +- mod.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 77d92b3..ef97763 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ all links but those containing `data-noprefetch`. For instance will prefetch `/bar`. -`manual` disables listening and requires you to call `window.QuickLink.prefetch` +`manual` disables listening and requires you to call `window.quicklink.prefetch` manually Not setting `options.strategy` will make this plugin fallback to the usual diff --git a/mod.ts b/mod.ts index e1ddbe0..68678c4 100644 --- a/mod.ts +++ b/mod.ts @@ -39,9 +39,9 @@ const prefetch = (options: Options = { throttle: 10, }): Plugin => { const main = `data:application/javascript, - import * as QuickLink from "https://esm.sh/quicklink@2.3.0"; + import * as quicklink from "https://esm.sh/quicklink@2.3.0"; - window.QuickLink = QuickLink; + window.quicklink = quicklink; function getOptions (options) { if (options.strategy) { @@ -57,7 +57,7 @@ const prefetch = (options: Options = { }; function listen (options) { - return QuickLink.listen(getOptions(options)) + return quicklink.listen(getOptions(options)) }; export default function(options) {