-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sitecore-jss][nextjs] Use site query to retrieve dictionary data in …
…XMCloud
- Loading branch information
1 parent
9346470
commit 17574e7
Showing
5 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...es/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/dictionary-service-factory.ts
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,53 @@ | ||
import { | ||
DictionaryService, | ||
RestDictionaryService, | ||
GraphQLDictionaryService, | ||
constants, | ||
} from '@sitecore-jss/sitecore-jss-nextjs'; | ||
import config from 'temp/config'; | ||
import clientFactory from 'lib/graphql-client-factory'; | ||
|
||
/** | ||
* Factory responsible for creating a DictionaryService instance | ||
*/ | ||
export class DictionaryServiceFactory { | ||
/** | ||
* @param {string} siteName site name | ||
* @returns {DictionaryService} service instance | ||
*/ | ||
create(siteName: string): DictionaryService { | ||
return process.env.FETCH_WITH === constants.FETCH_WITH.GRAPHQL | ||
? new GraphQLDictionaryService({ | ||
siteName, | ||
clientFactory, | ||
/* | ||
The Dictionary Service needs a root item ID in order to fetch dictionary phrases for the current app. | ||
When not provided, the service will attempt to figure out the root item for the current JSS App using GraphQL and app name. | ||
For SXA site(s) and multisite setup there's no need to specify it - it will be autoresolved. | ||
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. | ||
rootItemId: '{GUID}' | ||
*/ | ||
/* | ||
GraphQL endpoint may reach its rate limit with the amount of requests it receives and throw a rate limit error. | ||
GraphQL Dictionary and Layout Services can handle rate limit errors from server and attempt a retry on requests. | ||
For this, specify the number of 'retries' the GraphQL client will attempt. | ||
By default it is set to 3. You can disable it by configuring it to 0 for this service. | ||
Additionally, you have the flexibility to customize the retry strategy by passing a 'retryStrategy'. | ||
By default it uses the `DefaultRetryStrategy` with exponential back-off factor of 2 and handles error codes 429, | ||
502, 503, 504, 520, 521, 522, 523, 524, 'ECONNRESET', 'ETIMEDOUT' and 'EPROTO' . You can use this class or your own implementation of `RetryStrategy`. | ||
*/ | ||
retries: (process.env.GRAPH_QL_SERVICE_RETRIES && | ||
parseInt(process.env.GRAPH_QL_SERVICE_RETRIES, 10)) as number, | ||
useSiteQuery: true, | ||
}) | ||
: new RestDictionaryService({ | ||
apiHost: config.sitecoreApiHost, | ||
apiKey: config.sitecoreApiKey, | ||
siteName, | ||
}); | ||
} | ||
} | ||
|
||
/** DictionaryServiceFactory singleton */ | ||
export const dictionaryServiceFactory = new DictionaryServiceFactory(); |
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
18 changes: 18 additions & 0 deletions
18
packages/sitecore-jss/src/test-data/mockDictionarySiteQueryResponse.json
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,18 @@ | ||
{ | ||
"data": { | ||
"site": { | ||
"siteInfo": { | ||
"dictionary": [ | ||
{ | ||
"key": "foo", | ||
"value": "foo" | ||
}, | ||
{ | ||
"key": "bar", | ||
"value": "bar" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |