Skip to content

Commit

Permalink
Merge pull request #124 from swup/fix/fetch-page-visit-done
Browse files Browse the repository at this point in the history
  • Loading branch information
hirasso authored Feb 2, 2024
2 parents bf2fe5b + a0422a6 commit ebdb2e3
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import type {
PageData,
HookDefaultHandler
} from 'swup';
import { deviceSupportsHover, networkSupportsPreloading, whenIdle, isAnchorElement } from './util.js';
import {
deviceSupportsHover,
networkSupportsPreloading,
whenIdle,
isAnchorElement
} from './util.js';
import createQueue, { Queue } from './queue.js';
import createObserver, { Observer } from './observer.js';

Expand All @@ -26,7 +31,7 @@ declare module 'swup' {
}
export interface HookDefinitions {
'link:hover': { el: HTMLAnchorElement | SVGAElement; event: DelegateEvent };
'page:preload': { url: string, page?: PageData };
'page:preload': { url: string; page?: PageData };
}
export interface HookReturnValues {
'page:preload': Promise<PageData>;
Expand Down Expand Up @@ -133,7 +138,6 @@ export default class SwupPreloadPlugin extends Plugin {
swup.hooks.create('page:preload');
swup.hooks.create('link:hover');

// @ts-ignore: non-matching signatures (TODO: fix properly)
swup.preload = this.preload;
swup.preloadLinks = this.preloadLinks;

Expand Down Expand Up @@ -414,10 +418,16 @@ export default class SwupPreloadPlugin extends Plugin {
// @ts-expect-error: createVisit is currently private, need to make this semi-public somehow
const visit = this.swup.createVisit({ to: url });

const page = await this.swup.hooks.call('page:preload', visit, { url }, async (visit, args) => {
args.page = await this.swup.fetchPage(href);
return args.page;
});
const page = await this.swup.hooks.call(
'page:preload',
visit,
{ url },
async (visit, args) => {
// @ts-expect-error FetchOptions.visit is currently marked as internal
args.page = await this.swup.fetchPage(href, { visit });
return args.page;
}
);
return page;
}
}

0 comments on commit ebdb2e3

Please sign in to comment.