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

[samples] Replace dictionary calls by RestDictionaryService #750

Merged
merged 5 commits into from
Jul 22, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ http://JssReactWeb/sitecore/api/jss/dictionary/JssReactWeb/es-MX/
}
```

It's up to you how you invoke and utilize this data. For example, the React App utilizes the `i18next` module with `i18next-fetch-backend`.
It's up to you how you invoke and utilize this data. For example, the React App utilizes the `i18next` module with [custom plugin](https://www.i18next.com/misc/creating-own-plugins).

> Note for Sitecore devs and admins: By default all JSS apps have a dictionary domain configured for the app. You can alter the app's dictionary domain in the [App configuration](/docs/techniques/content-translation).

Expand Down
2 changes: 2 additions & 0 deletions packages/sitecore-jss-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export {
constants,
isExperienceEditorActive,
resetExperienceEditorChromes,
RestDictionaryService,
DictionaryService,
DictionaryPhrases,
LayoutService,
RestLayoutService,
Expand Down
47 changes: 17 additions & 30 deletions samples/node-headless-ssr-proxy/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const fs = require('fs');
const fetch = require('node-fetch');
const NodeCache = require('node-cache');
const { RestDictionaryService } = require('@sitecore-jss/sitecore-jss');
const httpAgents = require("./httpAgents");

// We keep a cached copy of the site dictionary for performance. Default is 60 seconds.
const dictionaryCache = new NodeCache({ stdTTL: 60 });

/**
* The JSS application name defaults to providing part of the bundle path as well as the dictionary service endpoint.
* If not passed as an environment variable or set here, any application name exported from the bundle will be used instead.
Expand All @@ -25,6 +21,15 @@ const apiHost = process.env.SITECORE_API_HOST || 'http://my.sitecore.host'

appName = appName || serverBundle.appName;

const apiKey = process.env.SITECORE_API_KEY || serverBundle.apiKey || '{YOUR API KEY HERE}';

const dictionaryService = new RestDictionaryService({
apiHost,
siteName: appName,
apiKey,
cacheTimeout: 60,
});

/**
* @type {ProxyConfig}
*/
Expand All @@ -50,7 +55,7 @@ const config = {
* apiKey: The Sitecore SSC API key your app uses.
* Required.
*/
apiKey: process.env.SITECORE_API_KEY || serverBundle.apiKey || '{YOUR API KEY HERE}',
apiKey,
/**
* pathRewriteExcludeRoutes: A list of absolute paths
* that are NOT app routes and should not attempt to render a route
Expand Down Expand Up @@ -135,31 +140,13 @@ const config = {
return {};
}

const cacheKey = `${site}_${language}`;

const cached = dictionaryCache.get(cacheKey);

if (cached) return Promise.resolve(cached);

return fetch(
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
`${config.apiHost}/sitecore/api/jss/dictionary/${appName}/${language}?sc_apikey=${
config.apiKey
}`,
{
headers: {
connection: "keep-alive",
},
}
)
.then((result) => result.json())
.then((json) => {
const viewBag = {
dictionary: json && json.phrases,
};
return dictionaryService.fetchDictionaryData(language).then(phrases => {
const viewBag = {
dictionary: phrases,
};

dictionaryCache.set(cacheKey, viewBag);
return viewBag;
});
return viewBag;
});
},
};

Expand Down
24 changes: 0 additions & 24 deletions samples/node-headless-ssr-proxy/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions samples/node-headless-ssr-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
"homepage": "https://jss.sitecore.com",
"license": "Apache-2.0",
"dependencies": {
"@sitecore-jss/sitecore-jss": "^19.0.0-canary.22",
"@sitecore-jss/sitecore-jss-proxy": "^19.0.0-canary.22",
"agentkeepalive": "^4.1.3",
"compression": "~1.7.3",
"express": "~4.16.4",
"memory-cache": "^0.2.0",
"node-cache": "~4.2.0",
"node-fetch": "^2.6.1"
"memory-cache": "^0.2.0"
},
"private": true
}
Loading