Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(headless/ssr-commerce): standalone search box only for listing & standalone #4735

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/headless/src/app/commerce-ssr-engine/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ interface SearchAndListingController {
[SolutionType.listing]: true;
}

interface ListingAndStandaloneController {
/**
* @internal
*/
[SolutionType.listing]: true;
/**
* @internal
*/
[SolutionType.standalone]: true;
}

export type SearchOnlyControllerDefinitionWithoutProps<
TController extends Controller,
> = ControllerDefinitionWithoutProps<TController> & SearchOnlyController;
Expand All @@ -190,6 +201,11 @@ export type SearchOnlyControllerDefinitionWithProps<
TProps,
> = ControllerDefinitionWithProps<TController, TProps> & SearchOnlyController;

export type ListingAndStandaloneControllerWithoutProps<
TController extends Controller,
> = ControllerDefinitionWithoutProps<TController> &
ListingAndStandaloneController;

export type ListingOnlyControllerDefinitionWithoutProps<
TController extends Controller,
> = ControllerDefinitionWithoutProps<TController> & ListingOnlyController;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NonRecommendationControllerDefinitionWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {ListingAndStandaloneControllerWithoutProps} from '../../../app/commerce-ssr-engine/types/common.js';
import {StandaloneSearchBoxProps} from '../../standalone-search-box/headless-standalone-search-box.js';
import {
StandaloneSearchBox,
Expand All @@ -10,7 +10,7 @@ export type {StandaloneSearchBoxState} from './headless-standalone-search-box.js
export type {StandaloneSearchBox, StandaloneSearchBoxProps};

export interface StandaloneSearchBoxDefinition
extends NonRecommendationControllerDefinitionWithoutProps<StandaloneSearchBox> {}
extends ListingAndStandaloneControllerWithoutProps<StandaloneSearchBox> {}

/**
* Defines the `StandaloneSearchBox` controller for the purpose of server-side rendering.
Expand All @@ -26,7 +26,6 @@ export function defineStandaloneSearchBox(
): StandaloneSearchBoxDefinition {
return {
listing: true,
search: true,
standalone: true,
build: (engine) => buildStandaloneSearchBox(engine, props),
};
Expand Down
8 changes: 5 additions & 3 deletions packages/samples/headless-ssr-commerce/app/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import PopularBought from '@/components/recommendations/popular-bought';
import StandaloneSearchBox from '@/components/standalone-search-box';
import {
recommendationEngineDefinition,
searchEngineDefinition,
standaloneEngineDefinition,
} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {defaultContext} from '@/utils/context';
Expand All @@ -18,13 +18,15 @@ import {headers} from 'next/headers';
export default async function Search() {
// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
const navigatorContext = new NextJsNavigatorContext(headers());
searchEngineDefinition.setNavigatorContextProvider(() => navigatorContext);
standaloneEngineDefinition.setNavigatorContextProvider(
() => navigatorContext
);

// Fetches the cart items from an external service
const items = await externalCartAPI.getCart();

// Fetches the static state of the app with initial state (when applicable)
const staticState = await searchEngineDefinition.fetchStaticState({
const staticState = await standaloneEngineDefinition.fetchStaticState({
controllers: {
cart: {initialState: {items}},
context: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as externalCartAPI from '@/actions/external-cart-api';
import ContextDropdown from '@/components/context-dropdown';
import {StandaloneProvider} from '@/components/providers/providers';
import StandaloneSearchBox from '@/components/standalone-search-box';
import {searchEngineDefinition} from '@/lib/commerce-engine';
import {standaloneEngineDefinition} from '@/lib/commerce-engine';
import {NextJsNavigatorContext} from '@/lib/navigatorContextProvider';
import {defaultContext} from '@/utils/context';
import {headers} from 'next/headers';
Expand All @@ -16,13 +16,15 @@ export default async function ProductDescriptionPage({
}) {
// Sets the navigator context provider to use the newly created `navigatorContext` before fetching the app static state
const navigatorContext = new NextJsNavigatorContext(headers());
searchEngineDefinition.setNavigatorContextProvider(() => navigatorContext);
standaloneEngineDefinition.setNavigatorContextProvider(
() => navigatorContext
);

// Fetches the cart items from an external service
const items = await externalCartAPI.getCart();

// Fetches the static state of the app with initial state (when applicable)
const staticState = await searchEngineDefinition.fetchStaticState({
const staticState = await standaloneEngineDefinition.fetchStaticState({
controllers: {
cart: {initialState: {items}},
context: {
Expand Down
Loading