Skip to content

Commit 8e486ec

Browse files
committed
Resolved build failures with layout service
1 parent 909de74 commit 8e486ec

File tree

7 files changed

+62
-95
lines changed

7 files changed

+62
-95
lines changed

headapps/Sugcon2024/src/lib/dictionary-service-factory.ts

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
import {
2-
DictionaryService,
3-
RestDictionaryService,
4-
GraphQLDictionaryService,
5-
constants,
6-
} from '@sitecore-jss/sitecore-jss-nextjs';
7-
import config from 'temp/config';
1+
import { DictionaryService, GraphQLDictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';
82
import clientFactory from 'lib/graphql-client-factory';
3+
94
/**
105
* Factory responsible for creating a DictionaryService instance
116
*/
@@ -15,34 +10,19 @@ export class DictionaryServiceFactory {
1510
* @returns {DictionaryService} service instance
1611
*/
1712
create(siteName: string): DictionaryService {
18-
return process.env.FETCH_WITH === constants.FETCH_WITH.GRAPHQL
19-
? new GraphQLDictionaryService({
20-
clientFactory,
21-
siteName,
22-
/*
23-
The Dictionary Service needs a root item ID in order to fetch dictionary phrases for the current app.
24-
When not provided, the service will attempt to figure out the root item for the current JSS App using GraphQL and app name.
25-
For SXA site(s) and multisite setup there's no need to specify it - it will be autoresolved.
26-
Otherwise, if your Sitecore instance only has 1 JSS App (i.e. in a Sitecore XP setup), you can specify the root item ID here.
27-
rootItemId: '{GUID}'
28-
*/
29-
/*
30-
GraphQL endpoint may reach its rate limit with the amount of Layout and Dictionary requests it receives and throw a rate limit error.
31-
GraphQL Dictionary and Layout Services can handle rate limit errors from server and attempt a retry on requests.
32-
For this, specify the number of retries the GraphQL client will attempt.
33-
It will only try the request once by default.
34-
retries: 'number'
35-
*/
36-
retries:
37-
(process.env.GRAPH_QL_SERVICE_RETRIES &&
38-
parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) ||
39-
0,
40-
})
41-
: new RestDictionaryService({
42-
apiHost: config.sitecoreApiHost,
43-
apiKey: config.sitecoreApiKey,
44-
siteName,
45-
});
13+
return new GraphQLDictionaryService({
14+
siteName,
15+
clientFactory,
16+
/*
17+
GraphQL endpoint may reach its rate limit with the amount of requests it receives and throw a rate limit error.
18+
GraphQL Dictionary and Layout Services can handle rate limit errors from server and attempt a retry on requests.
19+
For this, specify the number of 'retries' the GraphQL client will attempt.
20+
By default it is set to 3. You can disable it by configuring it to 0 for this service.
21+
*/
22+
retries: (process.env.GRAPH_QL_SERVICE_RETRIES &&
23+
parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) as number,
24+
useSiteQuery: true,
25+
});
4626
}
4727
}
4828

headapps/Sugcon2024/src/lib/layout-service-factory.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import {
22
LayoutService,
3-
RestLayoutService,
4-
GraphQLLayoutService,
5-
constants,
3+
GraphQLLayoutService
64
} from '@sitecore-jss/sitecore-jss-nextjs';
7-
import config from 'temp/config';
85
import clientFactory from 'lib/graphql-client-factory';
96

107
/**
@@ -16,28 +13,12 @@ export class LayoutServiceFactory {
1613
* @returns {LayoutService} service instance
1714
*/
1815
create(siteName: string): LayoutService {
19-
return process.env.FETCH_WITH === constants.FETCH_WITH.GRAPHQL
20-
? new GraphQLLayoutService({
21-
clientFactory,
22-
siteName,
23-
/*
24-
GraphQL endpoint may reach its rate limit with the amount of Layout and Dictionary requests it receives and throw a rate limit error.
25-
GraphQL Dictionary and Layout Services can handle rate limit errors from server and attempt a retry on requests.
26-
For this, specify the number of retries the GraphQL client will attempt.
27-
It will only try the request once by default.
28-
retries: 'number'
29-
*/
30-
retries:
31-
(process.env.GRAPH_QL_SERVICE_RETRIES &&
32-
parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) ||
33-
0,
34-
})
35-
: new RestLayoutService({
36-
apiHost: config.sitecoreApiHost,
37-
apiKey: config.sitecoreApiKey,
38-
siteName,
39-
configurationName: config.layoutServiceConfigurationName,
40-
});
16+
return new GraphQLLayoutService({
17+
siteName,
18+
clientFactory,
19+
retries: (process.env.GRAPH_QL_SERVICE_RETRIES &&
20+
parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) as number,
21+
});
4122
}
4223
}
4324

headapps/Sugcon2024/src/lib/middleware/plugins/personalize.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { NextRequest, NextResponse } from 'next/server';
22
import { PersonalizeMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/middleware';
33
import { MiddlewarePlugin } from '..';
4+
import clientFactory from 'lib/graphql-client-factory';
45
import config from 'temp/config';
56
import { siteResolver } from 'lib/site-resolver';
6-
import { createGraphQLClientFactory } from 'lib/graphql-client-factory/create';
7+
78
/**
89
* This is the personalize middleware plugin for Next.js.
9-
* It is used to enable Sitecore personalization of pages in Next.js.
10+
* It is used to enable Sitecore personalization and A/B testing of pages in Next.js.
1011
*
1112
* The `PersonalizeMiddleware` will
12-
* 1. Make a call to the Sitecore Experience Edge to get the personalization information about the page.
13-
* 2. Based on the response, make a call to the Sitecore CDP (with request/user context) to determine the page variant.
14-
* 3. Rewrite the response to the specific page variant.
13+
* 1. Call Sitecore Experience Edge to get the personalization information about the page.
14+
* 2. Based on the response, call Sitecore Personalize (with request/user context) to determine the page / component variant(s).
15+
* 3. Rewrite the response to the specific page / component variant(s).
1516
*/
1617
class PersonalizePlugin implements MiddlewarePlugin {
1718
private personalizeMiddleware: PersonalizeMiddleware;
@@ -23,31 +24,23 @@ class PersonalizePlugin implements MiddlewarePlugin {
2324
this.personalizeMiddleware = new PersonalizeMiddleware({
2425
// Configuration for your Sitecore Experience Edge endpoint
2526
edgeConfig: {
26-
clientFactory: createGraphQLClientFactory(config),
27-
scope: process.env.NEXT_PUBLIC_PERSONALIZE_SCOPE,
27+
clientFactory,
2828
timeout:
2929
(process.env.PERSONALIZE_MIDDLEWARE_EDGE_TIMEOUT &&
3030
parseInt(process.env.PERSONALIZE_MIDDLEWARE_EDGE_TIMEOUT)) ||
3131
400,
3232
},
3333
// Configuration for your Sitecore CDP endpoint
3434
cdpConfig: {
35-
sitecoreEdgeContextId: process.env.SITECORE_EDGE_CONTEXT_ID
36-
? process.env.SITECORE_EDGE_CONTEXT_ID
37-
: (() => {
38-
throw new Error(
39-
'Environment variable SITECORE_EDGE_CONTEXT_ID is required but not set.'
40-
);
41-
})(),
42-
sitecoreEdgeUrl:
43-
process.env.NEXT_PUBLIC_CDP_EDGE_URL || 'https://edge-platform.sitecorecloud.io',
44-
channel: process.env.NEXT_PUBLIC_CDP_CHANNEL || 'WEB',
45-
currency: process.env.NEXT_PUBLIC_CDP_CURRENCY || 'USD',
35+
sitecoreEdgeUrl: config.sitecoreEdgeUrl,
36+
sitecoreEdgeContextId: config.sitecoreEdgeContextId,
4637
timeout:
4738
(process.env.PERSONALIZE_MIDDLEWARE_CDP_TIMEOUT &&
4839
parseInt(process.env.PERSONALIZE_MIDDLEWARE_CDP_TIMEOUT)) ||
4940
400,
5041
},
42+
// Optional Sitecore Personalize scope identifier.
43+
scope: process.env.NEXT_PUBLIC_PERSONALIZE_SCOPE,
5144
// This function determines if the middleware should be turned off.
5245
// IMPORTANT: You should implement based on your cookie consent management solution of choice.
5346
// You may wish to keep it disabled while in development mode.
@@ -58,9 +51,6 @@ class PersonalizePlugin implements MiddlewarePlugin {
5851
excludeRoute: () => false,
5952
// Site resolver implementation
6053
siteResolver,
61-
// Personalize middleware will use PosResolver.resolve(site, language) (same as CdpPageView) by default to get point of sale.
62-
// You can also pass a custom point of sale resolver into middleware to override it like so:
63-
// getPointOfSale: (site, language) => { ... }
6454
});
6555
}
6656

headapps/Sugcon2024/src/lib/next-config/plugins/cors-header.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ const corsHeaderPlugin = (nextConfig = {}) => {
99
}
1010
return Object.assign({}, nextConfig, {
1111
async headers() {
12-
const extendHeaders = typeof nextConfig.headers === "function" ? await nextConfig.headers() : [];
12+
const extendHeaders =
13+
typeof nextConfig.headers === 'function' ? await nextConfig.headers() : [];
1314
return [
14-
...await extendHeaders,
15+
...(await extendHeaders),
1516
{
1617
source: '/_next/:path*',
17-
headers: [{ key: 'Access-Control-Allow-Origin', value: config.sitecoreApiHost }],
18+
headers: [
19+
{
20+
key: 'Access-Control-Allow-Origin',
21+
value: config.sitecoreApiHost.replace(/\/$/, ''),
22+
},
23+
],
1824
},
1925
];
2026
},

headapps/Sugcon2024/src/lib/page-props-factory/plugins/component-props.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { Plugin, isServerSidePropsContext } from '..';
77
class ComponentPropsPlugin implements Plugin {
88
private componentPropsService: ComponentPropsService;
99

10-
order = 2;
10+
// Make sure to run this plugin last to ensure that the updated layout data is used
11+
order = 10;
1112

1213
constructor() {
1314
this.componentPropsService = new ComponentPropsService();

headapps/Sugcon2024/src/middleware.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ export const config = {
1414
* 3. /sitecore/api (Sitecore API routes)
1515
* 4. /- (Sitecore media)
1616
* 5. /healthz (Health check)
17-
* 6. all root files inside /public
17+
* 6. /feaas-render (FEaaS render)
18+
* 7. all root files inside /public
1819
*/
19-
matcher: ['/', '/((?!api/|_next/|healthz|sitecore/api/|-/|favicon.ico|sc_logo.svg).*)'],
20+
matcher: [
21+
'/',
22+
'/((?!api/|_next/|feaas-render|healthz|sitecore/api/|-/|favicon.ico|sc_logo.svg).*)',
23+
],
2024
};

headapps/Sugcon2024/src/pages/[[...path]].tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useEffect } from 'react';
2-
import { JSX } from 'react';
1+
import { useEffect, JSX } from 'react';
32
import { GetStaticPaths, GetStaticProps } from 'next';
43
import NotFound from 'src/NotFound';
54
import Layout from 'src/Layout';
@@ -12,6 +11,7 @@ import { handleEditorFastRefresh } from '@sitecore-jss/sitecore-jss-nextjs/utils
1211
import { SitecorePageProps } from 'lib/page-props';
1312
import { sitecorePagePropsFactory } from 'lib/page-props-factory';
1413
import { componentBuilder } from 'temp/componentBuilder';
14+
import config from 'temp/config';
1515
import { sitemapFetcher } from 'lib/sitemap-fetcher';
1616

1717
const SitecorePage = ({
@@ -37,11 +37,13 @@ const SitecorePage = ({
3737
<SitecoreContext
3838
componentFactory={componentBuilder.getComponentFactory({ isEditing })}
3939
layoutData={layoutData}
40+
api={{
41+
edge: {
42+
contextId: config.sitecoreEdgeContextId,
43+
edgeUrl: config.sitecoreEdgeUrl,
44+
},
45+
}}
4046
>
41-
{/*
42-
Sitecore Pages supports component rendering to avoid refreshing the entire page during component editing.
43-
If you are using Experience Editor only, this logic can be removed, Layout can be left.
44-
*/}
4547
<Layout layoutData={layoutData} headLinks={headLinks} />
4648
</SitecoreContext>
4749
</ComponentPropsContext>
@@ -62,7 +64,10 @@ export const getStaticPaths: GetStaticPaths = async (context) => {
6264
let paths: StaticPath[] = [];
6365
let fallback: boolean | 'blocking' = 'blocking';
6466

65-
if (process.env.NODE_ENV !== 'development' && !process.env.DISABLE_SSG_FETCH) {
67+
if (
68+
process.env.NODE_ENV !== 'development' &&
69+
process.env.DISABLE_SSG_FETCH?.toLowerCase() !== 'true'
70+
) {
6671
try {
6772
// Note: Next.js runs export in production mode
6873
paths = await sitemapFetcher.fetch(context);

0 commit comments

Comments
 (0)