Skip to content

Commit

Permalink
Bump nosto-js dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
timowestnosto committed Jan 8, 2025
1 parent 1769bb6 commit e94a180
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 48 deletions.
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react-dom": "^18.3.1 || ^19.0.0"
},
"devDependencies": {
"@nosto/nosto-js": "*",
"@nosto/nosto-js": "^1.3.0",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "^16.0.0",
"@testing-library/user-event": "^14.4.3",
Expand Down
4 changes: 2 additions & 2 deletions src/components/NostoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export interface NostoProviderProps {
* Enables Shopify markets with language and market id
*/
shopifyMarkets?: {
language?: string
marketId?: string | number
language: string
marketId: string | number
}
/**
* Load nosto script (should be false if loading the script outside of nosto-react)
Expand Down
42 changes: 2 additions & 40 deletions src/hooks/useLoadClientScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const defaultAttributes = { "nosto-client-script": "" }

export function useLoadClientScript(props: NostoScriptProps) {
const {
host = "connect.nosto.com",
scriptLoader = scriptLoaderFn,
account,
shopifyMarkets,
Expand All @@ -22,39 +21,6 @@ export function useLoadClientScript(props: NostoScriptProps) {
setClientScriptLoaded(true)
}

// Create and append script element
async function injectScriptElement(urlPartial: string, extraAttributes: Record<string, string> = {}) {
const scriptSrc = `//${host}${urlPartial}`
const attributes = { ...defaultAttributes, ...extraAttributes }
await scriptLoader(scriptSrc, { attributes })
scriptOnload()
}

function prepareShopifyMarketsScript() {
const existingScript = document.querySelector("[nosto-client-script]")

const marketId = String(shopifyMarkets?.marketId || "")
const language = shopifyMarkets?.language || ""

const attributeMismatch =
existingScript?.getAttribute("nosto-language") !== language ||
existingScript?.getAttribute("nosto-market-id") !== marketId

if (!existingScript || attributeMismatch) {
if (clientScriptLoaded) {
setClientScriptLoaded(false)
}

const nostoSandbox = document.querySelector("#nosto-sandbox")

existingScript?.parentNode?.removeChild(existingScript)
nostoSandbox?.parentNode?.removeChild(nostoSandbox)

const urlPartial = `/script/shopify/market/nosto.js?merchant=${account}&market=${marketId}&locale=${language.toLowerCase()}`
injectScriptElement(urlPartial, { "nosto-language": language, "nosto-market-id": marketId })
}
}

initNostoStub()

if (!loadScript) {
Expand All @@ -65,6 +31,7 @@ export function useLoadClientScript(props: NostoScriptProps) {
async function initClientScript() {
await init({
merchantId: account,
shopifyInternational: shopifyMarkets,
options: {
attributes: defaultAttributes
},
Expand All @@ -74,14 +41,9 @@ export function useLoadClientScript(props: NostoScriptProps) {
}

// Load Nosto client script if not already loaded externally
if (!isNostoLoaded() && !shopifyMarkets) {
if (!isNostoLoaded()) {
initClientScript()
}

// Load Shopify Markets scripts
if (shopifyMarkets) {
prepareShopifyMarketsScript()
}
}, [shopifyMarkets?.marketId, shopifyMarkets?.language])

return { clientScriptLoaded }
Expand Down

0 comments on commit e94a180

Please sign in to comment.