swup 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,15 +7,30 @@ export type TransitionOptions = {
7
7
  customTransition?: string;
8
8
  };
9
9
 
10
- export const loadPage = function (
11
- this: Swup,
12
- data: TransitionOptions,
13
- popstate: PopStateEvent | null
14
- ) {
15
- const { url, customTransition } = data;
16
- const skipTransition = !!(popstate && !this.options.animateHistoryBrowsing);
10
+ export type PageLoadOptions = {
11
+ url: string;
12
+ event?: Event;
13
+ customTransition?: string;
14
+ };
17
15
 
18
- this.triggerEvent('transitionStart', popstate || undefined);
16
+ export function loadPage(this: Swup, data: TransitionOptions) {
17
+ const { url } = data;
18
+
19
+ // Check if the visit should be ignored
20
+ if (this.shouldIgnoreVisit(url)) {
21
+ window.location.href = url;
22
+ } else {
23
+ this.performPageLoad(data);
24
+ }
25
+ }
26
+
27
+ export function performPageLoad(this: Swup, data: PageLoadOptions) {
28
+ const { url, event, customTransition } = data ?? {};
29
+
30
+ const isHistoryVisit = event instanceof PopStateEvent;
31
+ const skipTransition = this.shouldSkipTransition({ url, event });
32
+
33
+ this.triggerEvent('transitionStart', event);
19
34
 
20
35
  // set transition object
21
36
  this.updateTransition(getCurrentUrl(), url, customTransition);
@@ -24,10 +39,10 @@ export const loadPage = function (
24
39
  }
25
40
 
26
41
  // start/skip animation
27
- const animationPromises = this.leavePage(data, { popstate, skipTransition });
42
+ const animationPromises = this.leavePage({ event, skipTransition });
28
43
 
29
44
  // create history record if this is not a popstate call (with or without anchor)
30
- if (!popstate) {
45
+ if (!isHistoryVisit) {
31
46
  createHistoryRecord(url + (this.scrollToElement || ''));
32
47
  }
33
48
 
@@ -39,7 +54,7 @@ export const loadPage = function (
39
54
  // when everything is ready, render the page
40
55
  Promise.all<PageRecord | void>([fetchPromise, ...animationPromises])
41
56
  .then(([pageData]) => {
42
- this.renderPage(pageData as PageRecord, { popstate, skipTransition });
57
+ this.renderPage(pageData as PageRecord, { event, skipTransition });
43
58
  })
44
59
  .catch((errorUrl) => {
45
60
  // Return early if errorUrl is not defined (probably aborted preload request)
@@ -54,4 +69,4 @@ export const loadPage = function (
54
69
  // Go back to the actual page we're still at
55
70
  history.go(-1);
56
71
  });
57
- };
72
+ }
@@ -2,12 +2,15 @@ import { Location, updateHistoryRecord, getCurrentUrl } from '../helpers';
2
2
  import Swup from '../Swup';
3
3
  import { PageRecord } from './Cache';
4
4
 
5
+ export type PageRenderOptions = {
6
+ event?: Event;
7
+ skipTransition?: boolean;
8
+ };
9
+
5
10
  export const renderPage = function (
6
11
  this: Swup,
7
12
  page: PageRecord,
8
- { popstate, skipTransition }: { popstate: PopStateEvent | null; skipTransition?: boolean } = {
9
- popstate: null
10
- }
13
+ { event, skipTransition }: PageRenderOptions = {}
11
14
  ) {
12
15
  document.documentElement.classList.remove('is-leaving');
13
16
 
@@ -30,11 +33,11 @@ export const renderPage = function (
30
33
  document.documentElement.classList.add('is-rendering');
31
34
  }
32
35
 
33
- this.triggerEvent('willReplaceContent', popstate || undefined);
36
+ this.triggerEvent('willReplaceContent', event);
34
37
 
35
38
  this.replaceContent(page).then(() => {
36
- this.triggerEvent('contentReplaced', popstate || undefined);
37
- this.triggerEvent('pageView', popstate || undefined);
39
+ this.triggerEvent('contentReplaced', event);
40
+ this.triggerEvent('pageView', event);
38
41
 
39
42
  // empty cache if it's disabled (in case preload plugin filled it)
40
43
  if (!this.options.cache) {
@@ -42,7 +45,7 @@ export const renderPage = function (
42
45
  }
43
46
 
44
47
  // Perform in transition
45
- this.enterPage({ popstate: popstate || undefined, skipTransition });
48
+ this.enterPage({ event, skipTransition });
46
49
 
47
50
  // reset scroll-to element
48
51
  this.scrollToElement = null;
@@ -0,0 +1,10 @@
1
+ import Swup from '../Swup';
2
+
3
+ export function updateTransition(this: Swup, from: string, to: string, custom?: string): void {
4
+ this.transition = { from, to, custom };
5
+ }
6
+
7
+ export function shouldSkipTransition(this: Swup, { event }: { url?: string; event?: Event }) {
8
+ const isHistoryVisit = event instanceof PopStateEvent;
9
+ return !!(isHistoryVisit && !this.options.animateHistoryBrowsing);
10
+ }
@@ -1,2 +0,0 @@
1
- import Swup from '../Swup';
2
- export declare const updateTransition: (this: Swup, from: string, to: string, custom?: string) => void;
@@ -1,10 +0,0 @@
1
- import Swup from '../Swup';
2
-
3
- export const updateTransition = function (
4
- this: Swup,
5
- from: string,
6
- to: string,
7
- custom?: string
8
- ): void {
9
- this.transition = { from, to, custom };
10
- };