@nordcraft/runtime 1.0.8 → 1.0.10

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/package.json CHANGED
@@ -4,13 +4,13 @@
4
4
  "type": "module",
5
5
  "homepage": "https://github.com/nordcraftengine/nordcraft",
6
6
  "dependencies": {
7
- "@nordcraft/core": "1.0.8",
8
- "@nordcraft/std-lib": "1.0.8",
7
+ "@nordcraft/core": "1.0.10",
8
+ "@nordcraft/std-lib": "1.0.10",
9
9
  "fast-deep-equal": "3.1.3",
10
10
  "path-to-regexp": "6.3.0"
11
11
  },
12
12
  "devDependencies": {
13
- "@happy-dom/global-registrator": "17.4.6"
13
+ "@happy-dom/global-registrator": "17.4.7"
14
14
  },
15
15
  "scripts": {
16
16
  "build": "tsc && bun scripts/build.js",
@@ -21,5 +21,5 @@
21
21
  "files": ["dist", "src"],
22
22
  "main": "dist/page.main.js",
23
23
  "types": "dist/page.main.d.ts",
24
- "version": "1.0.8"
24
+ "version": "1.0.10"
25
25
  }
@@ -31,6 +31,7 @@ import {
31
31
  sortObjectEntries,
32
32
  } from '@nordcraft/core/dist/utils/collections'
33
33
  import { PROXY_URL_HEADER, validateUrl } from '@nordcraft/core/dist/utils/url'
34
+ import { isDefined } from '@nordcraft/core/dist/utils/util'
34
35
  import { handleAction } from '../events/handleAction'
35
36
  import type { Signal } from '../signal/signal'
36
37
  import type { ComponentContext, ContextApi } from '../types'
@@ -117,7 +118,10 @@ export function createAPI(
117
118
  },
118
119
  })
119
120
  if (typeof location === 'string') {
120
- const url = validateUrl(location, window.location.href)
121
+ const url = validateUrl({
122
+ path: location,
123
+ origin: window.location.origin,
124
+ })
121
125
  if (url) {
122
126
  if (ctx.env.runtime === 'preview') {
123
127
  // Attempt to notify the parent about the failed navigation attempt
@@ -748,71 +752,72 @@ export function createAPI(
748
752
  }
749
753
  })
750
754
  payloadSignal.subscribe(async (_) => {
751
- if (api.autoFetch && applyFormula(api.autoFetch, getFormulaContext(api))) {
752
- // Ensure we only use caching if the page is currently loading
753
- if ((window?.__toddle?.isPageLoaded ?? false) === false) {
754
- const { url, requestSettings } = constructRequest(api)
755
- const cacheKey = requestHash(url, requestSettings)
756
- const cacheMatch = ctx.toddle.pageState.Apis?.[cacheKey] as ApiStatus
757
- if (cacheMatch) {
758
- if (cacheMatch.error) {
759
- apiError(
760
- api,
761
- {
762
- body: cacheMatch.error,
763
- status: cacheMatch.response?.status,
764
- headers: cacheMatch.response?.headers ?? undefined,
765
- },
766
- {
767
- requestStart:
768
- cacheMatch.response?.performance?.requestStart ?? null,
769
- responseStart:
770
- cacheMatch.response?.performance?.responseStart ?? null,
771
- responseEnd:
772
- cacheMatch.response?.performance?.responseEnd ?? null,
773
- },
774
- )
775
- } else {
776
- apiSuccess(
777
- api,
778
- {
779
- body: cacheMatch.data,
780
- status: cacheMatch.response?.status,
781
- headers: cacheMatch.response?.headers ?? undefined,
782
- },
783
- {
784
- requestStart:
785
- cacheMatch.response?.performance?.requestStart ?? null,
786
- responseStart:
787
- cacheMatch.response?.performance?.responseStart ?? null,
788
- responseEnd:
789
- cacheMatch.response?.performance?.responseEnd ?? null,
790
- },
791
- )
792
- }
793
- } else {
794
- // Execute will set the initial status of the api in the dataSignal
795
- await execute(api, url, requestSettings)
796
- }
755
+ const { url, requestSettings } = constructRequest(api)
756
+ // Ensure we only use caching if the page is currently loading
757
+ const cacheMatch =
758
+ // We lookup the API from cache as long as autofetch is defined (and not statically falsy)
759
+ // since the autofetch formula could've evaluated to true during SSR
760
+ isDefined(api.autoFetch) &&
761
+ (api.autoFetch.type !== 'value' || api.autoFetch.value === true) &&
762
+ (window?.__toddle?.isPageLoaded ?? false) === false
763
+ ? (ctx.toddle.pageState.Apis?.[
764
+ requestHash(url, requestSettings)
765
+ ] as ApiStatus)
766
+ : undefined
767
+
768
+ if (cacheMatch) {
769
+ if (cacheMatch.error) {
770
+ apiError(
771
+ api,
772
+ {
773
+ body: cacheMatch.error,
774
+ status: cacheMatch.response?.status,
775
+ headers: cacheMatch.response?.headers ?? undefined,
776
+ },
777
+ {
778
+ requestStart:
779
+ cacheMatch.response?.performance?.requestStart ?? null,
780
+ responseStart:
781
+ cacheMatch.response?.performance?.responseStart ?? null,
782
+ responseEnd: cacheMatch.response?.performance?.responseEnd ?? null,
783
+ },
784
+ )
797
785
  } else {
798
- // Execute will set the initial status of the api in the dataSignal
799
- const { url, requestSettings } = constructRequest(api)
800
- await execute(api, url, requestSettings)
786
+ apiSuccess(
787
+ api,
788
+ {
789
+ body: cacheMatch.data,
790
+ status: cacheMatch.response?.status,
791
+ headers: cacheMatch.response?.headers ?? undefined,
792
+ },
793
+ {
794
+ requestStart:
795
+ cacheMatch.response?.performance?.requestStart ?? null,
796
+ responseStart:
797
+ cacheMatch.response?.performance?.responseStart ?? null,
798
+ responseEnd: cacheMatch.response?.performance?.responseEnd ?? null,
799
+ },
800
+ )
801
801
  }
802
802
  } else {
803
- ctx.dataSignal.update((data) => {
804
- return {
805
- ...data,
806
- Apis: {
807
- ...(data.Apis ?? {}),
808
- [api.name]: {
809
- isLoading: false,
810
- data: null,
811
- error: null,
803
+ if (applyFormula(api.autoFetch, getFormulaContext(api))) {
804
+ // Execute will set the initial status of the api in the dataSignal
805
+ await execute(api, url, requestSettings)
806
+ } else {
807
+ ctx.dataSignal.update((data) => {
808
+ return {
809
+ ...data,
810
+ Apis: {
811
+ ...(data.Apis ?? {}),
812
+ [api.name]: {
813
+ isLoading: false,
814
+ data: null,
815
+ error: null,
816
+ },
812
817
  },
813
- },
814
- }
815
- })
818
+ }
819
+ })
820
+ }
816
821
  }
817
822
  })
818
823