-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updating Hydrogen * Adding nonce --------- Co-authored-by: Ilja Ganulevics <ilja.ganulevics@nosto.com> Co-authored-by: olsi-qose <olsi.qose@nosto.com> Co-authored-by: Mikko Poutanen <12759991+nosto-mikpou@users.noreply.github.com>
- Loading branch information
1 parent
d588703
commit d0361e2
Showing
16 changed files
with
181 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNosto404 } from "@nosto/nosto-react" | ||
|
||
export default function Nosto404(props) { | ||
useNosto404(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNostoCategory } from "@nosto/nosto-react" | ||
|
||
export default function NostoCategory(props) { | ||
useNostoCategory(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNostoCheckout } from "@nosto/nosto-react" | ||
|
||
export default function NostoCheckout(props) { | ||
useNostoCheckout(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNostoHome } from "@nosto/nosto-react" | ||
|
||
export default function NostoHome(props) { | ||
useNostoHome(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNostoOrder } from "@nosto/nosto-react" | ||
|
||
export default function NostoOrder(props) { | ||
useNostoOrder(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNostoOther } from "@nosto/nosto-react" | ||
|
||
export default function NostoOther(props) { | ||
useNostoOther(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { NostoPlacement as NostoComponent } from "@nosto/nosto-react" | ||
|
||
export default function NostoPlacement(props) { | ||
return <NostoComponent {...props} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useNostoProduct } from "@nosto/nosto-react" | ||
|
||
export default function NostoProduct(props) { | ||
const { selectedVariant } = props.tagging | ||
useNostoProduct({ | ||
product: props.product, | ||
tagging: { product_id: props.product, selected_sku_id: selectedVariant?.sku } | ||
}) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
import { NostoProvider as NostoComponent } from "@nosto/nosto-react"; | ||
import { NostoSession } from '@nosto/shopify-hydrogen' | ||
import { useMatches } from '@remix-run/react' | ||
import { parseGid } from '@shopify/hydrogen' | ||
import { NostoProvider as NostoComponent } from "@nosto/nosto-react" | ||
import { NostoSession } from "@nosto/shopify-hydrogen" | ||
import { useMatches } from "@remix-run/react" | ||
import { parseGid } from "@shopify/hydrogen" | ||
import createScriptLoader from "../createScriptLoader.js"; | ||
|
||
export default function ({ children, shopifyMarkets: shopifyMarketsProp, ...props }) { | ||
export default function ({ | ||
children, | ||
shopifyMarkets: shopifyMarketsProp, | ||
...props | ||
}) { | ||
const [root] = useMatches() | ||
const { language } = root?.data?.selectedLocale || {} | ||
const { market } = root?.data?.nostoProviderData?.localization?.country || {} | ||
|
||
//Get nostoData from root remix loader: | ||
const [root] = useMatches(); | ||
const { language } = root?.data?.selectedLocale || {} | ||
const { market } = root?.data?.nostoProviderData?.localization?.country || {} | ||
const currentVariation = props?.currentVariation || root?.data?.selectedLocale?.currency; | ||
const { id: marketId } = parseGid(market?.id) | ||
const scriptLoader = createScriptLoader(props.nonce) | ||
|
||
const shopifyMarkets = { | ||
marketId: shopifyMarketsProp?.marketId || marketId, | ||
language: shopifyMarketsProp?.language || language | ||
} | ||
const currentVariation = | ||
props?.currentVariation || root?.data?.selectedLocale?.currency | ||
const { id: marketId } = parseGid(market?.id) | ||
|
||
return ( | ||
<NostoComponent {...props} shopifyMarkets={shopifyMarkets} currentVariation={currentVariation} > | ||
<NostoSession /> | ||
{children} | ||
</NostoComponent> | ||
) | ||
const shopifyMarkets = { | ||
marketId: shopifyMarketsProp?.marketId || marketId, | ||
language: shopifyMarketsProp?.language || language, | ||
} | ||
|
||
return ( | ||
<> | ||
<NostoComponent | ||
{...props} | ||
shopifyMarkets={shopifyMarkets} | ||
currentVariation={currentVariation} | ||
scriptLoader={scriptLoader} | ||
> | ||
<NostoSession/> | ||
{children} | ||
</NostoComponent> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { useNostoSearch } from "@nosto/nosto-react" | ||
|
||
export default function NostoSearch(props) { | ||
useNostoSearch(props) | ||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,70 @@ | ||
import { NostoSession as NostoComponent } from "@nosto/nosto-react"; | ||
import { sha256 } from "js-sha256"; | ||
import { useMatches, Await, useAsyncValue } from "@remix-run/react"; | ||
import { Suspense } from "react"; | ||
import { NostoSession as NostoComponent } from "@nosto/nosto-react" | ||
import crypto from "crypto-es" | ||
import { Await, useAsyncValue, useMatches } from "@remix-run/react" | ||
|
||
//Polyfill the Array.prototype.at() method for all browsers | ||
if (!Array.prototype.at) { | ||
Object.defineProperty(Array.prototype, 'at', { | ||
Object.defineProperty(Array.prototype, "at", { | ||
value: function (index) { | ||
if (index >= 0) { | ||
return this[index]; | ||
return this[index] | ||
} else { | ||
return this[this.length + index]; | ||
return this[this.length + index] | ||
} | ||
}, | ||
enumerable: false, | ||
configurable: true, | ||
writable: true | ||
}); | ||
writable: true, | ||
}) | ||
} | ||
|
||
function AsyncSessionWrapper() { | ||
|
||
//Resolve async data: | ||
const { customer: customerData = {}, cart: shopifyCart, storeDomain } = useAsyncValue() || {}; | ||
const { | ||
customer: customerData = {}, | ||
cart: shopifyCart, | ||
storeDomain, | ||
} = useAsyncValue() || {} | ||
|
||
//Get customer data to sync with Nosto: | ||
let customerId = customerData?.id?.split('/').at(-1); | ||
let customer_reference = customerId && storeDomain ? sha256(customerId + storeDomain) : undefined; | ||
let customerId = customerData?.id?.split("/").at(-1) | ||
let customer_reference = | ||
customerId && storeDomain | ||
? crypto.SHA256(customerId + storeDomain).toString() | ||
: undefined | ||
let customer = { | ||
customer_reference, | ||
first_name: customerData?.firstName || undefined, | ||
last_name: customerData?.lastName || undefined, | ||
email: customerData?.email || undefined, | ||
newsletter: customerData?.acceptsMarketing ?? undefined | ||
newsletter: customerData?.acceptsMarketing ?? undefined, | ||
} | ||
|
||
//Get cart data to sync with Nosto: | ||
let items = shopifyCart?.lines?.edges?.map(item => item.node) | ||
let nostoCart = items?.map(item => { | ||
let items = shopifyCart?.lines?.edges?.map((item) => item.node) | ||
let nostoCart = items?.map((item) => { | ||
return { | ||
product_id: item?.merchandise?.product.id.split("/")?.at(-1), | ||
name: item?.merchandise?.product.title, | ||
sku_id: item?.merchandise?.id.split("/")?.at(-1), | ||
product_id: item?.merchandise?.product?.id.split("/")?.at(-1), | ||
name: item?.merchandise?.product?.title, | ||
sku_id: item?.merchandise?.id?.split("/")?.at(-1), | ||
quantity: item?.quantity, | ||
unit_price: +item?.merchandise?.price?.amount, | ||
price_currency_code: item?.merchandise?.price?.currencyCode | ||
price_currency_code: item?.merchandise?.price?.currencyCode, | ||
} | ||
}) | ||
let cart = nostoCart ? { items: nostoCart } : undefined; | ||
let cart = nostoCart ? { items: nostoCart } : undefined | ||
|
||
return <NostoComponent customer={customer} cart={cart} /> | ||
return <NostoComponent customer={customer} cart={cart}/> | ||
} | ||
|
||
export default function NostoSession() { | ||
|
||
//Get nostoSessionData promise from root remix loader: | ||
const [root] = useMatches(); | ||
const [root] = useMatches() | ||
const nostoPromise = root?.data?.nostoSessionData | ||
|
||
return ( | ||
<Suspense> | ||
<Await resolve={nostoPromise}> | ||
<AsyncSessionWrapper /> | ||
</Await> | ||
</Suspense> | ||
<Await resolve={nostoPromise}> | ||
<AsyncSessionWrapper/> | ||
</Await> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Creates a custom script loader function which passed to nosto-react component NostoProvider as a prop | ||
* and used in useLoadClientScript hook for loading scripts. This will override the default script loader behaviour | ||
* since a `nonce` attribute is required by Shopify for third party scripts. Additionally, this lets the useLoadClientScript | ||
* to handle building the URL string (scriptSrc in the returned function param). | ||
*/ | ||
|
||
export default function (nonce) { | ||
return function (scriptSrc, options) { | ||
return new Promise((resolve, reject) => { | ||
const script = document.createElement("script") | ||
script.type = "text/javascript" | ||
script.src = scriptSrc | ||
script.async = true | ||
script.setAttribute("nonce", nonce) | ||
script.onload = () => resolve() | ||
script.onerror = () => reject() | ||
Object.entries(options?.attributes ?? {}).forEach(([k, v]) => script.setAttribute(k, v)) | ||
if (options?.position === "head") { | ||
document.head.appendChild(script) | ||
} else { | ||
document.body.appendChild(script) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters