1+ import {
2+ applyCommercePagesRankingStrategy ,
3+ generateContentHash ,
4+ } from './utils.js' ;
15import { buildQueryStringFromObject } from '../helpers/index.js' ;
26import { contentEntries } from '../entities/schemas/content.js' ;
3- import { generateContentHash } from './utils .js' ;
7+ import { type ContentsState } from './types/index .js' ;
48import { get , merge } from 'lodash-es' ;
59import { INITIAL_STATE_CONTENT } from './reducer.js' ;
610import { normalize } from 'normalizr' ;
711import parse from 'url-parse' ;
8- import type { ContentsState } from './types/index.js' ;
912import type { ServerInitialState } from '../types/serverInitialState.types.js' ;
1013
1114/**
@@ -15,28 +18,34 @@ import type { ServerInitialState } from '../types/serverInitialState.types.js';
1518 *
1619 * @returns Initial state for the contents reducer.
1720 */
18- const serverInitialState : ServerInitialState = ( { model } ) => {
21+ const serverInitialState : ServerInitialState = ( { model, strategy } ) => {
1922 if ( ! get ( model , 'searchContentRequests' ) ) {
2023 return { contents : INITIAL_STATE_CONTENT } ;
2124 }
2225
2326 const { searchContentRequests, slug, seoMetadata, subfolder } = model ;
27+ const url = subfolder !== '/' ? slug ?. replace ( subfolder , '' ) : slug ;
28+ const normalizedUrl = url . replace ( '?json=true' , '' ) . replace ( '&json=true' , '' ) ;
2429
2530 const contents = searchContentRequests . reduce ( ( acc , item ) => {
26- const { searchResponse } = item ;
27- const firstSearchResponseItem = searchResponse . entries [ 0 ] ;
28-
29- if ( ! firstSearchResponseItem ) {
30- return acc ;
31- }
32-
31+ const {
32+ searchResponse,
33+ filters : { codes , contentTypeCode } ,
34+ } = item ;
35+ let response = searchResponse ;
36+ const isCommercePage = contentTypeCode === 'commerce_pages' ;
37+ const code = isCommercePage ? normalizedUrl : codes ?. [ 0 ] ;
3338 const hash = generateContentHash ( {
34- codes : firstSearchResponseItem . code ,
35- contentTypeCode : firstSearchResponseItem . contentTypeCode ,
39+ codes : code ,
40+ contentTypeCode : contentTypeCode ,
3641 } ) ;
3742
43+ if ( isCommercePage ) {
44+ response = applyCommercePagesRankingStrategy ( searchResponse , strategy ) ;
45+ }
46+
3847 const { entities, result } = {
39- ...normalize ( { hash, ...searchResponse } , contentEntries ) ,
48+ ...normalize ( { hash, ...response } , contentEntries ) ,
4049 } ;
4150
4251 return merge ( acc , {
@@ -53,7 +62,6 @@ const serverInitialState: ServerInitialState = ({ model }) => {
5362 } ) ;
5463 } , { } ) ;
5564
56- const url = subfolder !== '/' ? slug ?. replace ( subfolder , '' ) : slug ;
5765 const { pathname, query } = parse ( url , true ) ;
5866
5967 delete query . json ;
0 commit comments