From 939e175e48f281e67ac0a484c42a8318a0754e49 Mon Sep 17 00:00:00 2001 From: ylakhdar Date: Wed, 13 Nov 2024 14:27:29 -0500 Subject: [PATCH 1/2] add standalone provider https://coveord.atlassian.net/browse/KIT-3739 --- .../app/products/[productId]/page.tsx | 10 ++- .../providers/standalone-provider.tsx | 72 +++++++++++++++++++ packages/samples/headless-ssr-hydrogen | 1 + 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 packages/samples/headless-ssr-commerce/components/providers/standalone-provider.tsx create mode 160000 packages/samples/headless-ssr-hydrogen diff --git a/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx b/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx index e968419c120..2208ae33475 100644 --- a/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx +++ b/packages/samples/headless-ssr-commerce/app/products/[productId]/page.tsx @@ -1,6 +1,8 @@ import * as externalCartAPI from '@/actions/external-cart-api'; import ContextDropdown from '@/components/context-dropdown'; import ProductPage from '@/components/pages/product-page'; +import StandaloneProvider from '@/components/providers/standalone-provider'; +import StandaloneSearchBox from '@/components/standalone-search-box'; import {searchEngineDefinition} from '@/lib/commerce-engine'; import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider'; import {defaultContext} from '@/utils/context'; @@ -34,9 +36,13 @@ export default async function ProductDescriptionPage({ }, }); return ( - <> +

Product description page

+ Loading...

}>
- +
); } diff --git a/packages/samples/headless-ssr-commerce/components/providers/standalone-provider.tsx b/packages/samples/headless-ssr-commerce/components/providers/standalone-provider.tsx new file mode 100644 index 00000000000..a32a78d16ed --- /dev/null +++ b/packages/samples/headless-ssr-commerce/components/providers/standalone-provider.tsx @@ -0,0 +1,72 @@ +'use client'; + +import { + StandaloneHydratedState, + StandaloneStaticState, + standaloneEngineDefinition, +} from '@/lib/commerce-engine'; +import {NavigatorContext} from '@coveo/headless-react/ssr-commerce'; +import {PropsWithChildren, useEffect, useState} from 'react'; + +interface StandalonePageProps { + staticState: StandaloneStaticState; + navigatorContext: NavigatorContext; +} + +export default function StandaloneProvider({ + staticState, + navigatorContext, + children, +}: PropsWithChildren) { + const [hydratedState, setHydratedState] = useState< + StandaloneHydratedState | undefined + >(undefined); + + // Setting the navigator context provider also in client-side before hydrating the application + standaloneEngineDefinition.setNavigatorContextProvider( + () => navigatorContext + ); + + useEffect(() => { + standaloneEngineDefinition + .hydrateStaticState({ + searchAction: staticState.searchAction, + controllers: { + cart: { + initialState: {items: staticState.controllers.cart.state.items}, + }, + context: staticState.controllers.context.state, + }, + }) + .then(({engine, controllers}) => { + setHydratedState({engine, controllers}); + + // Refreshing recommendations in the browser after hydrating the state in the client-side + // Recommendation refresh in the server is not supported yet. + // controllers.popularBoughtRecs.refresh(); + }); + }, [staticState]); + + if (hydratedState) { + return ( + + {/* // TODO: KIT-3701: Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react/index").ReactNode'. + Type 'bigint' is not assignable to type 'ReactNode'.*/} + <>{children} + + ); + } else { + return ( + + {/* // TODO: KIT-3701: Type 'React.ReactNode' is not assignable to type 'import(".../node_modules/@types/react/index").ReactNode'. + Type 'bigint' is not assignable to type 'ReactNode'.*/} + <>{children} + + ); + } +} diff --git a/packages/samples/headless-ssr-hydrogen b/packages/samples/headless-ssr-hydrogen new file mode 160000 index 00000000000..4130fba353e --- /dev/null +++ b/packages/samples/headless-ssr-hydrogen @@ -0,0 +1 @@ +Subproject commit 4130fba353e7cf09cd30bd71568014af18cd86cf From 4e76b23ca438647d65bd0b5793c61a5a03184f45 Mon Sep 17 00:00:00 2001 From: Alex Prudhomme <78121423+alexprudhomme@users.noreply.github.com> Date: Mon, 18 Nov 2024 13:37:10 -0500 Subject: [PATCH 2/2] Delete headless-ssr-hydrogen https://coveord.atlassian.net/browse/KIT-3739 --- packages/samples/headless-ssr-hydrogen | 1 - 1 file changed, 1 deletion(-) delete mode 160000 packages/samples/headless-ssr-hydrogen diff --git a/packages/samples/headless-ssr-hydrogen b/packages/samples/headless-ssr-hydrogen deleted file mode 160000 index 4130fba353e..00000000000 --- a/packages/samples/headless-ssr-hydrogen +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4130fba353e7cf09cd30bd71568014af18cd86cf