@very-simple/ajax 1.0.0-beta.8 → 1.0.0-beta.9

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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  interface AjaxState extends Record<string, unknown> {
2
2
  regions?: string[];
3
+ index?: number;
3
4
  }
4
5
  /** Also accepts all `Config` options, e.g. `merge`, `scrollBehavior`, etc. */
5
6
  interface VisitOptions extends Config {
@@ -8,7 +9,7 @@ interface VisitOptions extends Config {
8
9
  autoFocus?: boolean;
9
10
  request?: RequestInit;
10
11
  regions?: string[];
11
- state?: Omit<AjaxState, 'regions'>;
12
+ state?: Omit<AjaxState, 'regions' | 'index'>;
12
13
  }
13
14
  type StartOptions = Config;
14
15
  interface LoadingOptions {
package/dist/index.js CHANGED
@@ -272,7 +272,11 @@ var visit = async (url, {
272
272
  url = normalizeUrl(url);
273
273
  const fromUrl = currentUrl;
274
274
  const fromState = currentState;
275
- state = { ...state, regions };
275
+ if (!(isBackForward && state)) {
276
+ const currentIndex = currentState?.index ?? 0;
277
+ const index = historyMode === "push" ? currentIndex + 1 : currentIndex;
278
+ state = { ...state, regions, index };
279
+ }
276
280
  currentVisitController?.abort();
277
281
  currentVisitController = new AbortController();
278
282
  const { signal } = currentVisitController;
@@ -287,7 +291,13 @@ var visit = async (url, {
287
291
  let newDocument;
288
292
  if (isBackForward) {
289
293
  const html = cache.get(url);
290
- if (html) newDocument = parseHtml(html);
294
+ if (html) {
295
+ const cached = parseHtml(html);
296
+ const isComplete = regions.every(
297
+ (selector) => !document.querySelector(selector) || cached.querySelector(selector)
298
+ );
299
+ if (isComplete) newDocument = cached;
300
+ }
291
301
  }
292
302
  if (!newDocument) {
293
303
  const options = { loadingDelay, progressHideDelay, request };
@@ -488,10 +498,12 @@ var interceptForms = () => {
488
498
  var interceptHistory = () => {
489
499
  window.addEventListener("popstate", (event) => {
490
500
  if (normalizeUrl(window.location.href) === normalizeUrl(currentUrl)) return;
501
+ const isBack = (event.state?.index ?? 0) < (currentState?.index ?? 0);
502
+ const regions = isBack ? currentState?.regions : event.state?.regions;
491
503
  visit(window.location.href, {
492
504
  history: "none",
493
505
  isBackForward: true,
494
- regions: currentState?.regions ?? config.regions,
506
+ regions: regions ?? config.regions,
495
507
  state: event.state
496
508
  });
497
509
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@very-simple/ajax",
3
- "version": "1.0.0-beta.8",
3
+ "version": "1.0.0-beta.9",
4
4
  "description": "A very simple turbolinks inspired ajax library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",