@hybridly/core 0.1.0-alpha.9 → 0.2.0

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.cjs CHANGED
@@ -214,18 +214,6 @@ function sameUrls(...hrefs) {
214
214
  }
215
215
  return false;
216
216
  }
217
- function sameHashes(...hrefs) {
218
- if (hrefs.length < 2) {
219
- return true;
220
- }
221
- try {
222
- return hrefs.every((href) => {
223
- return makeUrl(href).toJSON() === makeUrl(hrefs.at(0)).toJSON();
224
- });
225
- } catch {
226
- }
227
- return false;
228
- }
229
217
  function fillHash(currentUrl, targetUrl) {
230
218
  currentUrl = makeUrl(currentUrl);
231
219
  targetUrl = makeUrl(targetUrl);
@@ -253,10 +241,12 @@ function setHistoryState(options = {}) {
253
241
  throw error;
254
242
  }
255
243
  }
256
- function getHistoryState(key) {
257
- const context = getRouterContext();
258
- const data = context.serializer.unserialize(window.history.state);
259
- return key ? data?.state?.[key] : data?.state;
244
+ function getHistoryState() {
245
+ return getRouterContext().serializer.unserialize(window.history.state);
246
+ }
247
+ function getHistoryMemo(key) {
248
+ const state = getHistoryState();
249
+ return key ? state?.memo?.[key] : state?.memo;
260
250
  }
261
251
  async function registerEventListeners() {
262
252
  const context = getRouterContext();
@@ -302,10 +292,17 @@ function isBackForwardNavigation() {
302
292
  return window.performance?.getEntriesByType("navigation").at(0)?.type === "back_forward";
303
293
  }
304
294
  async function handleBackForwardNavigation() {
305
- utils.debug.router("Handling a back/forward navigation.");
306
- window.history.state.version = getRouterContext().version;
295
+ utils.debug.router("Handling a back/forward navigation from an external URL.");
296
+ const context = getRouterContext();
297
+ const state = getHistoryState();
298
+ if (!state) {
299
+ throw new Error("Tried to handling a back/forward navigation, but there was no state in the history. This should not happen.");
300
+ }
307
301
  await navigate({
308
- payload: window.history.state,
302
+ payload: {
303
+ ...state,
304
+ version: context.version
305
+ },
309
306
  preserveScroll: true,
310
307
  preserveState: false,
311
308
  updateHistoryState: false,
@@ -315,16 +312,13 @@ async function handleBackForwardNavigation() {
315
312
  function remember(key, value) {
316
313
  utils.debug.history(`Remembering key "${key}" with value`, value);
317
314
  setContext({
318
- state: {
319
- ...getHistoryState(),
315
+ memo: {
316
+ ...getHistoryMemo(),
320
317
  [key]: value
321
318
  }
322
319
  }, { propagate: false });
323
320
  setHistoryState({ replace: true });
324
321
  }
325
- function getKeyFromHistory(key) {
326
- return getHistoryState(key);
327
- }
328
322
  function serializeContext(context) {
329
323
  return context.serializer.serialize({
330
324
  url: context.url,
@@ -332,7 +326,7 @@ function serializeContext(context) {
332
326
  view: context.view,
333
327
  dialog: context.dialog,
334
328
  scrollRegions: context.scrollRegions,
335
- state: context.state
329
+ memo: context.memo
336
330
  });
337
331
  }
338
332
  function createSerializer(options) {
@@ -341,10 +335,12 @@ function createSerializer(options) {
341
335
  }
342
336
  return {
343
337
  serialize: (data) => {
338
+ utils.debug.history("Serializing data.", data);
344
339
  return superjson.stringify(data);
345
340
  },
346
341
  unserialize: (data) => {
347
342
  if (!data) {
343
+ utils.debug.history("No data to unserialize.");
348
344
  return;
349
345
  }
350
346
  return superjson.parse(data);
@@ -493,7 +489,7 @@ async function initializeContext(options) {
493
489
  axios: options.axios ?? axios__default.create(),
494
490
  routing: options.routing,
495
491
  hooks: {},
496
- state: {}
492
+ memo: {}
497
493
  };
498
494
  await runHooks("initialized", {}, state.context);
499
495
  return getInternalRouterContext();
@@ -603,7 +599,7 @@ const router = {
603
599
  close: (options) => closeDialog(options)
604
600
  },
605
601
  history: {
606
- get: (key) => getKeyFromHistory(key),
602
+ get: (key) => getHistoryMemo(key),
607
603
  remember: (key, value) => remember(key, value)
608
604
  }
609
605
  };
@@ -718,7 +714,7 @@ async function performHybridNavigation(options) {
718
714
  preserveScroll: options.preserveScroll,
719
715
  preserveState: options.preserveState,
720
716
  preserveUrl: options.preserveUrl,
721
- replace: options.replace === true || sameHashes(payload.url, window.location.href) || options.preserveUrl
717
+ replace: options.replace === true || options.preserveUrl
722
718
  });
723
719
  if (Object.keys(context.view.properties.errors ?? {}).length > 0) {
724
720
  const errors = (() => {
@@ -804,9 +800,9 @@ async function navigate(options) {
804
800
  const shouldPreserveScroll = evaluateConditionalOption(options.preserveScroll);
805
801
  const shouldReplaceHistory = evaluateConditionalOption(options.replace);
806
802
  const shouldReplaceUrl = evaluateConditionalOption(options.preserveUrl);
807
- if (shouldPreserveState && getHistoryState() && options.payload.view.component === context.view.component) {
808
- utils.debug.history("Setting the history from this entry into the context.");
809
- setContext({ state: getHistoryState() });
803
+ if (shouldPreserveState && getHistoryMemo() && options.payload.view.component === context.view.component) {
804
+ utils.debug.history("Setting the memo from this history entry into the current context.");
805
+ setContext({ memo: getHistoryMemo() });
810
806
  }
811
807
  if (shouldReplaceUrl) {
812
808
  utils.debug.router(`Preserving the current URL (${context.url}) instead of navigating to ${options.payload.url}`);
@@ -814,7 +810,7 @@ async function navigate(options) {
814
810
  }
815
811
  setContext({
816
812
  ...options.payload,
817
- state: {}
813
+ memo: {}
818
814
  });
819
815
  if (options.updateHistoryState !== false) {
820
816
  utils.debug.router(`Target URL is ${context.url}, current window URL is ${window.location.href}.`, { shouldReplaceHistory });
@@ -825,7 +821,7 @@ async function navigate(options) {
825
821
  await context.adapter.onViewSwap({
826
822
  component: viewComponent,
827
823
  dialog: context.dialog,
828
- properties: options.payload?.view.properties,
824
+ properties: options.payload?.view?.properties,
829
825
  preserveState: shouldPreserveState
830
826
  });
831
827
  if (options.isBackForward) {
package/dist/index.d.ts CHANGED
@@ -355,7 +355,7 @@ interface InternalRouterContext {
355
355
  /** Scroll positions of the current page's DOM elements. */
356
356
  scrollRegions: ScrollRegion[];
357
357
  /** Arbitrary state. */
358
- state: Record<string, any>;
358
+ memo: Record<string, any>;
359
359
  /** Currently pending navigation. */
360
360
  pendingNavigation?: PendingNavigation;
361
361
  /** History state serializer. */
package/dist/index.mjs CHANGED
@@ -205,18 +205,6 @@ function sameUrls(...hrefs) {
205
205
  }
206
206
  return false;
207
207
  }
208
- function sameHashes(...hrefs) {
209
- if (hrefs.length < 2) {
210
- return true;
211
- }
212
- try {
213
- return hrefs.every((href) => {
214
- return makeUrl(href).toJSON() === makeUrl(hrefs.at(0)).toJSON();
215
- });
216
- } catch {
217
- }
218
- return false;
219
- }
220
208
  function fillHash(currentUrl, targetUrl) {
221
209
  currentUrl = makeUrl(currentUrl);
222
210
  targetUrl = makeUrl(targetUrl);
@@ -244,10 +232,12 @@ function setHistoryState(options = {}) {
244
232
  throw error;
245
233
  }
246
234
  }
247
- function getHistoryState(key) {
248
- const context = getRouterContext();
249
- const data = context.serializer.unserialize(window.history.state);
250
- return key ? data?.state?.[key] : data?.state;
235
+ function getHistoryState() {
236
+ return getRouterContext().serializer.unserialize(window.history.state);
237
+ }
238
+ function getHistoryMemo(key) {
239
+ const state = getHistoryState();
240
+ return key ? state?.memo?.[key] : state?.memo;
251
241
  }
252
242
  async function registerEventListeners() {
253
243
  const context = getRouterContext();
@@ -293,10 +283,17 @@ function isBackForwardNavigation() {
293
283
  return window.performance?.getEntriesByType("navigation").at(0)?.type === "back_forward";
294
284
  }
295
285
  async function handleBackForwardNavigation() {
296
- debug.router("Handling a back/forward navigation.");
297
- window.history.state.version = getRouterContext().version;
286
+ debug.router("Handling a back/forward navigation from an external URL.");
287
+ const context = getRouterContext();
288
+ const state = getHistoryState();
289
+ if (!state) {
290
+ throw new Error("Tried to handling a back/forward navigation, but there was no state in the history. This should not happen.");
291
+ }
298
292
  await navigate({
299
- payload: window.history.state,
293
+ payload: {
294
+ ...state,
295
+ version: context.version
296
+ },
300
297
  preserveScroll: true,
301
298
  preserveState: false,
302
299
  updateHistoryState: false,
@@ -306,16 +303,13 @@ async function handleBackForwardNavigation() {
306
303
  function remember(key, value) {
307
304
  debug.history(`Remembering key "${key}" with value`, value);
308
305
  setContext({
309
- state: {
310
- ...getHistoryState(),
306
+ memo: {
307
+ ...getHistoryMemo(),
311
308
  [key]: value
312
309
  }
313
310
  }, { propagate: false });
314
311
  setHistoryState({ replace: true });
315
312
  }
316
- function getKeyFromHistory(key) {
317
- return getHistoryState(key);
318
- }
319
313
  function serializeContext(context) {
320
314
  return context.serializer.serialize({
321
315
  url: context.url,
@@ -323,7 +317,7 @@ function serializeContext(context) {
323
317
  view: context.view,
324
318
  dialog: context.dialog,
325
319
  scrollRegions: context.scrollRegions,
326
- state: context.state
320
+ memo: context.memo
327
321
  });
328
322
  }
329
323
  function createSerializer(options) {
@@ -332,10 +326,12 @@ function createSerializer(options) {
332
326
  }
333
327
  return {
334
328
  serialize: (data) => {
329
+ debug.history("Serializing data.", data);
335
330
  return stringify(data);
336
331
  },
337
332
  unserialize: (data) => {
338
333
  if (!data) {
334
+ debug.history("No data to unserialize.");
339
335
  return;
340
336
  }
341
337
  return parse(data);
@@ -484,7 +480,7 @@ async function initializeContext(options) {
484
480
  axios: options.axios ?? axios.create(),
485
481
  routing: options.routing,
486
482
  hooks: {},
487
- state: {}
483
+ memo: {}
488
484
  };
489
485
  await runHooks("initialized", {}, state.context);
490
486
  return getInternalRouterContext();
@@ -594,7 +590,7 @@ const router = {
594
590
  close: (options) => closeDialog(options)
595
591
  },
596
592
  history: {
597
- get: (key) => getKeyFromHistory(key),
593
+ get: (key) => getHistoryMemo(key),
598
594
  remember: (key, value) => remember(key, value)
599
595
  }
600
596
  };
@@ -709,7 +705,7 @@ async function performHybridNavigation(options) {
709
705
  preserveScroll: options.preserveScroll,
710
706
  preserveState: options.preserveState,
711
707
  preserveUrl: options.preserveUrl,
712
- replace: options.replace === true || sameHashes(payload.url, window.location.href) || options.preserveUrl
708
+ replace: options.replace === true || options.preserveUrl
713
709
  });
714
710
  if (Object.keys(context.view.properties.errors ?? {}).length > 0) {
715
711
  const errors = (() => {
@@ -795,9 +791,9 @@ async function navigate(options) {
795
791
  const shouldPreserveScroll = evaluateConditionalOption(options.preserveScroll);
796
792
  const shouldReplaceHistory = evaluateConditionalOption(options.replace);
797
793
  const shouldReplaceUrl = evaluateConditionalOption(options.preserveUrl);
798
- if (shouldPreserveState && getHistoryState() && options.payload.view.component === context.view.component) {
799
- debug.history("Setting the history from this entry into the context.");
800
- setContext({ state: getHistoryState() });
794
+ if (shouldPreserveState && getHistoryMemo() && options.payload.view.component === context.view.component) {
795
+ debug.history("Setting the memo from this history entry into the current context.");
796
+ setContext({ memo: getHistoryMemo() });
801
797
  }
802
798
  if (shouldReplaceUrl) {
803
799
  debug.router(`Preserving the current URL (${context.url}) instead of navigating to ${options.payload.url}`);
@@ -805,7 +801,7 @@ async function navigate(options) {
805
801
  }
806
802
  setContext({
807
803
  ...options.payload,
808
- state: {}
804
+ memo: {}
809
805
  });
810
806
  if (options.updateHistoryState !== false) {
811
807
  debug.router(`Target URL is ${context.url}, current window URL is ${window.location.href}.`, { shouldReplaceHistory });
@@ -816,7 +812,7 @@ async function navigate(options) {
816
812
  await context.adapter.onViewSwap({
817
813
  component: viewComponent,
818
814
  dialog: context.dialog,
819
- properties: options.payload?.view.properties,
815
+ properties: options.payload?.view?.properties,
820
816
  preserveState: shouldPreserveState
821
817
  });
822
818
  if (options.isBackForward) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hybridly/core",
3
- "version": "0.1.0-alpha.9",
3
+ "version": "0.2.0",
4
4
  "description": "Core functionality of Hybridly",
5
5
  "keywords": [
6
6
  "hybridly",
@@ -38,7 +38,7 @@
38
38
  "dependencies": {
39
39
  "qs": "^6.11.1",
40
40
  "superjson": "^1.12.2",
41
- "@hybridly/utils": "0.1.0-alpha.9"
41
+ "@hybridly/utils": "0.2.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "defu": "^6.1.2"