Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: manual strategy #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Fresh you'd use globalThis instead of window.

manually

Not setting `options.strategy` will make this plugin fallback to the usual
quicklink's behavior, i.e. viewport based prefetching
13 changes: 10 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]";
import * as quicklink from "https://esm.sh/[email protected]";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to be more specific?


window.quicklink = quicklink;

function getOptions (options) {
if (options.strategy) {
Expand All @@ -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 {
Expand Down