@nosto/nosto-react 2.2.1 → 2.3.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.
Files changed (38) hide show
  1. package/LICENSE +1 -1
  2. package/dist/index.d.ts +23 -192
  3. package/dist/index.es.js +249 -221
  4. package/dist/index.umd.js +1 -1
  5. package/package.json +20 -17
  6. package/src/components/Nosto404.tsx +2 -25
  7. package/src/components/NostoCategory.tsx +2 -30
  8. package/src/components/NostoCheckout.tsx +2 -25
  9. package/src/components/NostoHome.tsx +2 -26
  10. package/src/components/NostoOrder.tsx +2 -33
  11. package/src/components/NostoOther.tsx +2 -25
  12. package/src/components/NostoPlacement.tsx +1 -1
  13. package/src/components/NostoProduct.tsx +2 -32
  14. package/src/components/NostoProvider.tsx +5 -9
  15. package/src/components/NostoSearch.tsx +2 -30
  16. package/src/components/NostoSession.tsx +2 -42
  17. package/src/context.ts +2 -2
  18. package/src/hooks/scriptLoader.ts +4 -4
  19. package/src/hooks/useDeepCompareEffect.ts +1 -4
  20. package/src/hooks/useLoadClientScript.ts +26 -17
  21. package/src/hooks/useNosto404.tsx +25 -0
  22. package/src/hooks/useNostoApi.ts +4 -7
  23. package/src/hooks/useNostoCategory.tsx +31 -0
  24. package/src/hooks/useNostoCheckout.tsx +25 -0
  25. package/src/hooks/useNostoContext.ts +2 -3
  26. package/src/hooks/useNostoHome.tsx +25 -0
  27. package/src/hooks/useNostoOrder.tsx +35 -0
  28. package/src/hooks/useNostoOther.tsx +25 -0
  29. package/src/hooks/useNostoProduct.tsx +43 -0
  30. package/src/hooks/useNostoSearch.tsx +31 -0
  31. package/src/hooks/useNostoSession.tsx +34 -0
  32. package/src/hooks/useRenderCampaigns.tsx +12 -9
  33. package/src/index.ts +23 -3
  34. package/src/types.ts +1 -923
  35. package/src/utils/types.ts +5 -3
  36. package/src/components/helpers.ts +0 -3
  37. package/src/components/index.ts +0 -11
  38. package/src/hooks/index.ts +0 -5
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nosto/nosto-react",
3
3
  "description": "Component library to simply implementing Nosto on React.",
4
- "version": "2.2.1",
4
+ "version": "2.3.0",
5
5
  "author": "Mridang Agarwalla, Dominik Gilg",
6
6
  "license": "ISC",
7
7
  "repository": {
@@ -23,12 +23,18 @@
23
23
  "prettier": "prettier '{src,spec}/**' --list-different",
24
24
  "prettier:fix": "prettier '{src,spec}/**' --write",
25
25
  "test": "vitest run --silent",
26
+ "test-loud": "vitest run",
26
27
  "typedoc": "typedoc src/index.ts"
27
28
  },
28
29
  "files": [
29
30
  "dist",
30
31
  "src"
31
32
  ],
33
+ "dependencies": {
34
+ "react": "^18.3.1",
35
+ "react-dom": "^18.3.1",
36
+ "@nosto/nosto-js": "*"
37
+ },
32
38
  "devDependencies": {
33
39
  "@testing-library/jest-dom": "^6.4.8",
34
40
  "@testing-library/react": "^16.0.0",
@@ -36,22 +42,19 @@
36
42
  "@types/react": "^18.0.0",
37
43
  "@types/react-dom": "^18.0.0",
38
44
  "@types/user-event": "^4.1.1",
39
- "@typescript-eslint/eslint-plugin": "^6.11.0",
40
- "@typescript-eslint/parser": "^6.11.0",
41
45
  "@vitejs/plugin-react": "^4.3.1",
42
- "eslint": "^8.53.0",
43
- "eslint-plugin-promise": "^6.1.1",
46
+ "eslint": "^9.13.0",
47
+ "eslint-plugin-promise": "^7.1.0",
44
48
  "eslint-plugin-react": "^7.33.2",
45
- "prettier": "^2.0.5",
46
- "react": "^18.3.1",
47
- "react-dom": "^18.3.1",
48
- "react-router": "^6.25.1",
49
- "react-router-dom": "^6.25.1",
50
- "rimraf": "^3.0.2",
51
- "typedoc": "^0.24.1",
52
- "typescript": "^4.6.3",
49
+ "prettier": "^3.3.3",
50
+ "react-router": "^7.0.1",
51
+ "react-router-dom": "^7.0.1",
52
+ "rimraf": "^6.0.1",
53
+ "typedoc": "^0.27.2",
54
+ "typescript": "^5.6.3",
55
+ "typescript-eslint": "^8.13.0",
53
56
  "vite": "^5.2.10",
54
- "vite-plugin-dts": "^3.9.1",
57
+ "vite-plugin-dts": "^4.2.2",
55
58
  "vitest": "^2.0.5"
56
59
  },
57
60
  "main": "./dist/index.umd.js",
@@ -60,9 +63,9 @@
60
63
  "exports": {
61
64
  ".": {
62
65
  "import": "./dist/index.es.js",
63
- "require": "./dist/index.umd.js"
64
- },
65
- "./shopify-hydrogen": "./dist/index.es.js"
66
+ "require": "./dist/index.umd.js",
67
+ "types": "./dist/index.d.ts"
68
+ }
66
69
  },
67
70
  "bugs": {
68
71
  "url": "https://github.com/Nosto/nosto-react/issues"
@@ -1,9 +1,4 @@
1
- import { useNostoApi, useRenderCampaigns } from "../hooks"
2
-
3
- /**
4
- * @group Components
5
- */
6
- export type Nosto404Props = { placements?: string[] }
1
+ import { Nosto404Props, useNosto404 } from "../hooks/useNosto404"
7
2
 
8
3
  /**
9
4
  * You can personalise your cart and checkout pages by using the `Nosto404` component.
@@ -25,25 +20,7 @@ export type Nosto404Props = { placements?: string[] }
25
20
  *
26
21
  * @group Components
27
22
  */
28
- export default function Nosto404(props: Nosto404Props) {
23
+ export function Nosto404(props: Nosto404Props) {
29
24
  useNosto404(props)
30
25
  return null
31
26
  }
32
-
33
- /**
34
- * You can personalise your cart and checkout pages by using the `useNosto404` hook.
35
- *
36
- * @group Hooks
37
- */
38
- export function useNosto404(props?: Nosto404Props) {
39
- const { renderCampaigns } = useRenderCampaigns()
40
-
41
- useNostoApi(
42
- async (api) => {
43
- const data = await api.defaultSession()
44
- .viewNotFound()
45
- .setPlacements(props?.placements || api.placements.getPlacements())
46
- .load()
47
- renderCampaigns(data)
48
- })
49
- }
@@ -1,12 +1,4 @@
1
- import { useNostoApi, useRenderCampaigns } from "../hooks"
2
-
3
- /**
4
- * @group Components
5
- */
6
- export type NostoCategoryProps = {
7
- category: string
8
- placements?: string[]
9
- }
1
+ import { NostoCategoryProps, useNostoCategory } from "../hooks/useNostoCategory"
10
2
 
11
3
  /**
12
4
  * You can personalise your category and collection pages by using the NostoCategory component.
@@ -30,27 +22,7 @@ export type NostoCategoryProps = {
30
22
  *
31
23
  * @group Components
32
24
  */
33
- export default function NostoCategory(props: NostoCategoryProps) {
25
+ export function NostoCategory(props: NostoCategoryProps) {
34
26
  useNostoCategory(props)
35
27
  return null
36
28
  }
37
-
38
- /**
39
- * You can personalise your category and collection pages by using the useNostoCategory hook.
40
- *
41
- * @group Hooks
42
- */
43
- export function useNostoCategory({ category, placements }: NostoCategoryProps) {
44
- const { renderCampaigns } = useRenderCampaigns()
45
-
46
- useNostoApi(
47
- async (api) => {
48
- const data = await api.defaultSession()
49
- .viewCategory(category)
50
- .setPlacements(placements || api.placements.getPlacements())
51
- .load()
52
- renderCampaigns(data)
53
- },
54
- [category]
55
- )
56
- }
@@ -1,9 +1,4 @@
1
- import { useNostoApi, useRenderCampaigns } from "../hooks"
2
-
3
- /**
4
- * @group Components
5
- */
6
- export type NostoCheckoutProps = { placements?: string[] }
1
+ import { NostoCheckoutProps, useNostoCheckout } from "../hooks/useNostoCheckout"
7
2
 
8
3
  /**
9
4
  * You can personalise your cart and checkout pages by using the NostoCheckout component.
@@ -24,25 +19,7 @@ export type NostoCheckoutProps = { placements?: string[] }
24
19
  *
25
20
  * @group Components
26
21
  */
27
- export default function NostoCheckout(props: NostoCheckoutProps) {
22
+ export function NostoCheckout(props: NostoCheckoutProps) {
28
23
  useNostoCheckout(props)
29
24
  return null
30
25
  }
31
-
32
- /**
33
- * You can personalise your cart and checkout pages by using the useNostoCheckout hook.
34
- *
35
- * @group Hooks
36
- */
37
- export function useNostoCheckout(props?: NostoCheckoutProps) {
38
- const { renderCampaigns } = useRenderCampaigns()
39
-
40
- useNostoApi(
41
- async (api) => {
42
- const data = await api.defaultSession()
43
- .viewCart()
44
- .setPlacements(props?.placements || api.placements.getPlacements())
45
- .load()
46
- renderCampaigns(data)
47
- })
48
- }
@@ -1,9 +1,4 @@
1
- import { useRenderCampaigns, useNostoApi } from "../hooks"
2
-
3
- /**
4
- * @group Components
5
- */
6
- export type NostoHomeProps = { placements?: string[] }
1
+ import { NostoHomeProps, useNostoHome } from "../hooks/useNostoHome"
7
2
 
8
3
  /**
9
4
  * The `NostoHome` component must be used to personalise the home page. The component does not require any props.
@@ -28,26 +23,7 @@ export type NostoHomeProps = { placements?: string[] }
28
23
  *
29
24
  * @group Components
30
25
  */
31
- export default function NostoHome(props: NostoHomeProps) {
26
+ export function NostoHome(props: NostoHomeProps) {
32
27
  useNostoHome(props)
33
28
  return null
34
29
  }
35
-
36
- /**
37
- * You can personalise your home page by using the useNostoHome hook.
38
- *
39
- * @group Hooks
40
- */
41
- export function useNostoHome(props?: NostoHomeProps) {
42
- const { renderCampaigns } = useRenderCampaigns()
43
-
44
- useNostoApi(
45
- async (api) => {
46
- const data = await api.defaultSession()
47
- .viewFrontPage()
48
- .setPlacements(props?.placements || api.placements.getPlacements())
49
- .load()
50
- renderCampaigns(data)
51
- }
52
- )
53
- }
@@ -1,14 +1,4 @@
1
- import { Order } from "../types"
2
- import { useRenderCampaigns, useNostoApi } from "../hooks"
3
- import { snakeize } from "../utils/snakeize"
4
-
5
- /**
6
- * @group Components
7
- */
8
- export type NostoOrderProps = {
9
- order: Order
10
- placements?: string[]
11
- }
1
+ import { NostoOrderProps, useNostoOrder } from "../hooks/useNostoOrder"
12
2
 
13
3
  /**
14
4
  * You can personalise your order-confirmation/thank-you page by using the `NostoOrder` component.
@@ -29,28 +19,7 @@ export type NostoOrderProps = {
29
19
  *
30
20
  * @group Components
31
21
  */
32
- export default function NostoOrder(props: NostoOrderProps) {
22
+ export function NostoOrder(props: NostoOrderProps) {
33
23
  useNostoOrder(props)
34
24
  return null
35
25
  }
36
-
37
- /**
38
- * You can personalise your order-confirmation/thank-you page by using the `useNostoOrder` hook.
39
- *
40
- * @group Hooks
41
- */
42
- export function useNostoOrder({ order, placements }: NostoOrderProps) {
43
- const { renderCampaigns } = useRenderCampaigns()
44
-
45
- useNostoApi(
46
- async (api) => {
47
- const data = await api.defaultSession()
48
- .addOrder(snakeize(order))
49
- .setPlacements(placements || api.placements.getPlacements())
50
- .load()
51
- renderCampaigns(data)
52
- },
53
- [order],
54
- { deep: true }
55
- )
56
- }
@@ -1,9 +1,4 @@
1
- import { useRenderCampaigns, useNostoApi } from "../hooks"
2
-
3
- /**
4
- * @group Components
5
- */
6
- export type NostoOtherProps = { placements?: string[] }
1
+ import { NostoOtherProps, useNostoOther } from "../hooks/useNostoOther"
7
2
 
8
3
  /**
9
4
  * You can personalise your miscellaneous pages by using the NostoOther component.
@@ -24,25 +19,7 @@ export type NostoOtherProps = { placements?: string[] }
24
19
  *
25
20
  * @group Components
26
21
  */
27
- export default function NostoOther(props: NostoOtherProps) {
22
+ export function NostoOther(props: NostoOtherProps) {
28
23
  useNostoOther(props)
29
24
  return null
30
25
  }
31
-
32
- /**
33
- * You can personalise your miscellaneous pages by using the useNostoOther hook.
34
- *
35
- * @group Hooks
36
- */
37
- export function useNostoOther(props?: NostoOtherProps) {
38
- const { renderCampaigns } = useRenderCampaigns()
39
-
40
- useNostoApi(
41
- async (api) => {
42
- const data = await api.defaultSession()
43
- .viewOther()
44
- .setPlacements(props?.placements || api.placements.getPlacements())
45
- .load()
46
- renderCampaigns(data)
47
- })
48
- }
@@ -16,6 +16,6 @@ export type NostoPlacementProps = { id: string; pageType?: string }
16
16
  *
17
17
  * @group Components
18
18
  */
19
- export default function NostoPlacement({ id, pageType }: NostoPlacementProps) {
19
+ export function NostoPlacement({ id, pageType }: NostoPlacementProps) {
20
20
  return <div className="nosto_element" id={id} key={id + (pageType || "")} />
21
21
  }
@@ -1,14 +1,4 @@
1
- import { useRenderCampaigns, useNostoApi } from "../hooks"
2
- import { Product } from "../types"
3
-
4
- /**
5
- * @group Components
6
- */
7
- export type NostoProductProps = {
8
- product: string
9
- tagging?: Product
10
- placements?: string[]
11
- }
1
+ import { NostoProductProps, useNostoProduct } from "../hooks/useNostoProduct"
12
2
 
13
3
  /**
14
4
  * The NostoProduct component must be used to personalise the product page.
@@ -34,27 +24,7 @@ export type NostoProductProps = {
34
24
  *
35
25
  * @group Components
36
26
  */
37
- export default function NostoProduct(props: NostoProductProps) {
27
+ export function NostoProduct(props: NostoProductProps) {
38
28
  useNostoProduct(props)
39
29
  return null
40
30
  }
41
-
42
- /**
43
- * You can personalise your product pages by using the useNostoProduct hook.
44
- *
45
- * @group Hooks
46
- */
47
- export function useNostoProduct({ product, tagging, placements }: NostoProductProps) {
48
- const { renderCampaigns } = useRenderCampaigns()
49
-
50
- useNostoApi(
51
- async (api) => {
52
- const data = await api.defaultSession()
53
- .viewProduct(tagging ?? product)
54
- .setPlacements(placements || api.placements.getPlacements())
55
- .load()
56
- renderCampaigns(data)
57
- },
58
- [product, tagging?.selected_sku_id]
59
- )
60
- }
@@ -1,8 +1,9 @@
1
1
  import { isValidElement } from "react"
2
2
  import { NostoContext, RecommendationComponent } from "../context"
3
- import { useLoadClientScript } from "../hooks"
4
3
  import type { ReactNode } from "react"
5
4
  import { ScriptLoadOptions } from "../hooks/scriptLoader"
5
+ import { useLoadClientScript } from "../hooks/useLoadClientScript"
6
+ import { nostojs } from "@nosto/nosto-js"
6
7
 
7
8
  /**
8
9
  * @group Components
@@ -70,13 +71,8 @@ export interface NostoProviderProps {
70
71
  *
71
72
  * @group Components
72
73
  */
73
- export default function NostoProvider(props: NostoProviderProps) {
74
- const {
75
- account,
76
- multiCurrency = false,
77
- children,
78
- recommendationComponent,
79
- } = props
74
+ export function NostoProvider(props: NostoProviderProps) {
75
+ const { account, multiCurrency = false, children, recommendationComponent } = props
80
76
 
81
77
  // Pass currentVariation as empty string if multiCurrency is disabled
82
78
  const currentVariation = multiCurrency ? props.currentVariation : ""
@@ -93,7 +89,7 @@ export default function NostoProvider(props: NostoProviderProps) {
93
89
  const { clientScriptLoaded } = useLoadClientScript(props)
94
90
 
95
91
  if (clientScriptLoaded) {
96
- window.nostojs(api => {
92
+ nostojs(api => {
97
93
  api.defaultSession().setVariation(currentVariation!).setResponseMode(responseMode)
98
94
  })
99
95
  }
@@ -1,12 +1,4 @@
1
- import { useRenderCampaigns, useNostoApi } from "../hooks"
2
-
3
- /**
4
- * @group Components
5
- */
6
- export type NostoSearchProps = {
7
- query: string
8
- placements?: string[]
9
- }
1
+ import { NostoSearchProps, useNostoSearch } from "../hooks/useNostoSearch"
10
2
 
11
3
  /**
12
4
  * You can personalise your search pages by using the NostoSearch component.
@@ -31,27 +23,7 @@ export type NostoSearchProps = {
31
23
  *
32
24
  * @group Components
33
25
  */
34
- export default function NostoSearch(props: NostoSearchProps) {
26
+ export function NostoSearch(props: NostoSearchProps) {
35
27
  useNostoSearch(props)
36
28
  return null
37
29
  }
38
-
39
- /**
40
- * You can personalise your search pages by using the useNostoSearch hook.
41
- *
42
- * @group Hooks
43
- */
44
- export function useNostoSearch({ query, placements }: NostoSearchProps) {
45
- const { renderCampaigns } = useRenderCampaigns()
46
-
47
- useNostoApi(
48
- async (api) => {
49
- const data = await api.defaultSession()
50
- .viewSearch(query)
51
- .setPlacements(placements || api.placements.getPlacements())
52
- .load()
53
- renderCampaigns(data)
54
- },
55
- [query]
56
- )
57
- }
@@ -1,18 +1,4 @@
1
- import { useNostoContext, useDeepCompareEffect } from "../hooks"
2
- import { Cart as CartSnakeCase, Customer as CustomerSnakeCase } from "../types"
3
- import { snakeize } from "../utils/snakeize"
4
- import { ToCamelCase } from "../utils/types"
5
-
6
- type Cart = CartSnakeCase | ToCamelCase<CartSnakeCase>
7
- type Customer = CustomerSnakeCase | ToCamelCase<CustomerSnakeCase>
8
-
9
- /**
10
- * @group Components
11
- */
12
- export type NostoSessionProps = {
13
- cart?: Cart
14
- customer?: Customer
15
- }
1
+ import { NostoSessionProps, useNostoSession } from "../hooks/useNostoSession"
16
2
 
17
3
  /**
18
4
  * Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
@@ -24,33 +10,7 @@ export type NostoSessionProps = {
24
10
  *
25
11
  * @group Components
26
12
  */
27
- export default function NostoSession(props?: NostoSessionProps) {
13
+ export function NostoSession(props?: NostoSessionProps) {
28
14
  useNostoSession(props)
29
15
  return null
30
16
  }
31
-
32
- /**
33
- * Nosto React requires that you pass it the details of current cart contents and the details of the currently logged-in customer, if any, on every route change.
34
- *
35
- * @group Hooks
36
- */
37
- export function useNostoSession({ cart, customer }: NostoSessionProps = {}) {
38
- const { clientScriptLoaded } = useNostoContext()
39
-
40
- useDeepCompareEffect(() => {
41
- const currentCart = cart ? snakeize(cart) : undefined
42
- const currentCustomer = customer ? snakeize(customer) : undefined
43
-
44
- if (clientScriptLoaded) {
45
- window.nostojs(api => {
46
- api
47
- .defaultSession()
48
- .setCart(currentCart)
49
- .setCustomer(currentCustomer)
50
- .viewOther()
51
- .load({ skipPageViews: true })
52
- })
53
- }
54
- }, [clientScriptLoaded, cart, customer])
55
-
56
- }
package/src/context.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createContext, ReactElement } from "react"
2
- import { Recommendation, RenderMode } from "./types"
2
+ import { Recommendation } from "./types"
3
+ import { RenderMode } from "@nosto/nosto-js/client"
3
4
 
4
5
  type AnyFunction = (...args: unknown[]) => unknown
5
6
 
@@ -28,4 +29,3 @@ export const NostoContext = createContext<NostoContextType>({
28
29
  responseMode: "HTML",
29
30
  clientScriptLoaded: false
30
31
  })
31
-
@@ -8,9 +8,9 @@ export default function scriptLoader(scriptSrc: string, options?: ScriptLoadOpti
8
8
  script.onerror = () => reject()
9
9
  Object.entries(options?.attributes ?? {}).forEach(([k, v]) => script.setAttribute(k, v))
10
10
  if (options?.position === "head") {
11
- document.head.appendChild(script)
11
+ document.head.appendChild(script)
12
12
  } else {
13
- document.body.appendChild(script)
13
+ document.body.appendChild(script)
14
14
  }
15
15
  })
16
16
  }
@@ -26,5 +26,5 @@ export type ScriptLoadOptions = {
26
26
  /**
27
27
  * Indicates the attributes of the script element
28
28
  */
29
- attributes?: Record<string, string>
30
- }
29
+ attributes?: Record<string, string>
30
+ }
@@ -1,10 +1,7 @@
1
1
  import { useEffect, useRef, useMemo, type EffectCallback, type DependencyList } from "react"
2
2
  import { deepCompare } from "../utils/compare"
3
3
 
4
- export function useDeepCompareEffect(
5
- callback: EffectCallback,
6
- dependencies: DependencyList
7
- ) {
4
+ export function useDeepCompareEffect(callback: EffectCallback, dependencies: DependencyList) {
8
5
  return useEffect(callback, useDeepCompareMemoize(dependencies))
9
6
  }
10
7
 
@@ -1,20 +1,26 @@
1
1
  import { useState, useEffect } from "react"
2
- import { isNostoLoaded } from "../components/helpers"
3
- import type { NostoClient } from "../types"
4
2
  import type { NostoProviderProps } from "../components/NostoProvider"
5
3
  import scriptLoaderFn from "./scriptLoader"
4
+ import { init, initNostoStub, isNostoLoaded, nostojs } from "@nosto/nosto-js"
5
+ import { reloadNosto } from "@nosto/nosto-js/testing"
6
6
 
7
7
  type NostoScriptProps = Pick<NostoProviderProps, "account" | "host" | "shopifyMarkets" | "loadScript" | "scriptLoader">
8
8
 
9
9
  export function useLoadClientScript(props: NostoScriptProps) {
10
- const { host = "connect.nosto.com", scriptLoader = scriptLoaderFn, account, shopifyMarkets, loadScript = true } = props
10
+ const {
11
+ host = "connect.nosto.com",
12
+ scriptLoader = scriptLoaderFn,
13
+ account,
14
+ shopifyMarkets,
15
+ loadScript = true
16
+ } = props
11
17
  const [clientScriptLoaded, setClientScriptLoaded] = useState(false)
12
18
 
13
19
  useEffect(() => {
14
20
  function scriptOnload() {
15
21
  // Override for production scripts to work in unit tests
16
22
  if ("nostoReactTest" in window) {
17
- window.nosto?.reload({
23
+ reloadNosto({
18
24
  site: "localhost"
19
25
  })
20
26
  }
@@ -49,29 +55,32 @@ export function useLoadClientScript(props: NostoScriptProps) {
49
55
  existingScript?.parentNode?.removeChild(existingScript)
50
56
  nostoSandbox?.parentNode?.removeChild(nostoSandbox)
51
57
 
52
- const urlPartial =
53
- `/script/shopify/market/nosto.js?merchant=${account}&market=${marketId}&locale=${language.toLowerCase()}`
58
+ const urlPartial = `/script/shopify/market/nosto.js?merchant=${account}&market=${marketId}&locale=${language.toLowerCase()}`
54
59
  injectScriptElement(urlPartial, { "nosto-language": language, "nosto-market-id": marketId })
55
60
  }
56
61
  }
57
62
 
58
- // Load Nosto API stub
59
- if (!window.nostojs) {
60
- window.nostojs = (cb: (api: NostoClient) => void) => {
61
- (window.nostojs.q = window.nostojs.q || []).push(cb)
62
- }
63
- window.nostojs(api => api.setAutoLoad(false))
64
- }
63
+ initNostoStub()
65
64
 
66
65
  if (!loadScript) {
67
- window.nosto ? scriptOnload() : window.nostojs(scriptOnload)
66
+ nostojs(scriptOnload)
68
67
  return
69
68
  }
70
69
 
70
+ async function initClientScript() {
71
+ await init({
72
+ merchantId: account,
73
+ options: {
74
+ attributes: { "nosto-client-script": ""}
75
+ },
76
+ scriptLoader
77
+ })
78
+ scriptOnload()
79
+ }
80
+
71
81
  // Load Nosto client script if not already loaded externally
72
82
  if (!isNostoLoaded() && !shopifyMarkets) {
73
- const urlPartial = `/include/${account}`
74
- injectScriptElement(urlPartial)
83
+ initClientScript()
75
84
  }
76
85
 
77
86
  // Load Shopify Markets scripts
@@ -81,4 +90,4 @@ export function useLoadClientScript(props: NostoScriptProps) {
81
90
  }, [shopifyMarkets?.marketId, shopifyMarkets?.language])
82
91
 
83
92
  return { clientScriptLoaded }
84
- }
93
+ }
@@ -0,0 +1,25 @@
1
+ import { useNostoApi } from "./useNostoApi"
2
+ import { useRenderCampaigns } from "./useRenderCampaigns"
3
+
4
+ /**
5
+ * @group Hooks
6
+ */
7
+ export type Nosto404Props = { placements?: string[] }
8
+
9
+ /**
10
+ * You can personalise your cart and checkout pages by using the `useNosto404` hook.
11
+ *
12
+ * @group Hooks
13
+ */
14
+ export function useNosto404(props?: Nosto404Props) {
15
+ const { renderCampaigns } = useRenderCampaigns()
16
+
17
+ useNostoApi(async api => {
18
+ const data = await api
19
+ .defaultSession()
20
+ .viewNotFound()
21
+ .setPlacements(props?.placements || api.placements.getPlacements())
22
+ .load()
23
+ renderCampaigns(data)
24
+ })
25
+ }
@@ -1,19 +1,16 @@
1
1
  import { DependencyList, useEffect } from "react"
2
2
  import { useNostoContext } from "./useNostoContext"
3
- import { NostoClient } from "../types"
4
3
  import { useDeepCompareEffect } from "./useDeepCompareEffect"
4
+ import { nostojs } from "@nosto/nosto-js"
5
+ import { API } from "@nosto/nosto-js/client"
5
6
 
6
- export function useNostoApi(
7
- cb: (api: NostoClient) => void,
8
- deps?: DependencyList,
9
- flags?: { deep?: boolean }
10
- ): void {
7
+ export function useNostoApi(cb: (api: API) => void, deps?: DependencyList, flags?: { deep?: boolean }): void {
11
8
  const { clientScriptLoaded } = useNostoContext()
12
9
  const useEffectFn = flags?.deep ? useDeepCompareEffect : useEffect
13
10
 
14
11
  useEffectFn(() => {
15
12
  if (clientScriptLoaded) {
16
- window.nostojs(cb)
13
+ nostojs(cb)
17
14
  }
18
15
  }, [clientScriptLoaded, ...(deps ?? [])])
19
16
  }