create-sitecore-jss 22.2.0-canary.10 → 22.2.0-canary.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/templates/angular/.env +4 -0
  2. package/dist/templates/angular/package.json +0 -2
  3. package/dist/templates/angular/scripts/config/plugins/fallback.ts +0 -1
  4. package/dist/templates/angular/scripts/generate-config.ts +14 -3
  5. package/dist/templates/angular/scripts/update-graphql-fragment-data.ts +20 -28
  6. package/dist/templates/angular/server.bundle.ts +4 -5
  7. package/dist/templates/angular/src/app/lib/dictionary-service-factory.ts +4 -1
  8. package/dist/templates/angular/src/app/lib/graphql-client-factory/config.ts +21 -0
  9. package/dist/templates/angular/src/app/lib/graphql-client-factory/index.ts +16 -0
  10. package/dist/templates/angular/src/app/lib/layout-service-factory.ts +1 -1
  11. package/dist/templates/angular-sxp/scripts/config/plugins/disconnected.ts +4 -2
  12. package/dist/templates/angular-xmcloud/.env +6 -0
  13. package/dist/templates/angular-xmcloud/scripts/config/plugins/xmcloud.ts +16 -0
  14. package/dist/templates/angular-xmcloud/src/app/lib/config.ts +2 -0
  15. package/dist/templates/angular-xmcloud/src/app/lib/graphql-client-factory/config.ts +58 -0
  16. package/dist/templates/angular-xmcloud/src/app/routing/layout/layout.component.ts +3 -5
  17. package/dist/templates/nextjs/scripts/config/plugins/fallback.ts +0 -1
  18. package/dist/templates/nextjs/scripts/generate-config.ts +8 -1
  19. package/dist/templates/nextjs-styleguide/scripts/config/plugins/disconnected.ts +1 -0
  20. package/dist/templates/node-xmcloud-proxy/package.json +2 -2
  21. package/dist/templates/node-xmcloud-proxy/src/config.ts +1 -1
  22. package/dist/templates/node-xmcloud-proxy/src/index.ts +28 -4
  23. package/dist/templates/node-xmcloud-proxy/src/types.ts +5 -3
  24. package/dist/templates/react/scripts/generate-config.js +10 -3
  25. package/dist/templates/vue/scripts/generate-config.js +5 -0
  26. package/package.json +2 -2
  27. package/dist/templates/angular/src/app/lib/graphql-client-factory.ts +0 -28
  28. package/dist/templates/angular-xmcloud/src/app/lib/graphql-client-factory.ts +0 -44
@@ -1,3 +1,5 @@
1
+ # ====== Sitecore Preview / Delivery Edge ======
2
+
1
3
  # Your Sitecore API key is needed to build the app. Typically, the API key is
2
4
  # defined in `scjssconfig.json` (as `sitecore.apiKey`). This file may not exist
3
5
  # when building locally (if you've never run `jss setup`), or when building in a
@@ -17,6 +19,8 @@ SITECORE_API_HOST=
17
19
  # the resolved Sitecore API hostname + the `graphQLEndpointPath` defined in your `package.json`.
18
20
  GRAPH_QL_ENDPOINT=
19
21
 
22
+ # ==============================================
23
+
20
24
  # Your Sitecore site name.
21
25
  # Uses your `package.json` config `appName` if empty.
22
26
  SITECORE_SITE_NAME=
@@ -81,7 +81,6 @@
81
81
  "@sitecore-jss/sitecore-jss-angular-schematics": "~22.2.0-canary",
82
82
  "@sitecore-jss/sitecore-jss-cli": "~22.2.0-canary",
83
83
  "@sitecore-jss/sitecore-jss-dev-tools": "~22.2.0-canary",
84
- "@types/isomorphic-fetch": "0.0.35",
85
84
  "@types/jasmine": "~3.6.7",
86
85
  "@types/jasminewd2": "~2.0.8",
87
86
  "@types/node": "~20.14.10",
@@ -99,7 +98,6 @@
99
98
  "eslint-plugin-import": "2.29.1",
100
99
  "eslint-plugin-jsdoc": "48.7.0",
101
100
  "eslint-plugin-prefer-arrow": "1.2.3",
102
- "isomorphic-fetch": "^3.0.0",
103
101
  "jasmine-core": "~3.7.1",
104
102
  "jasmine-spec-reporter": "~6.0.0",
105
103
  "karma": "^6.3.2",
@@ -12,7 +12,6 @@ class FallbackPlugin implements ConfigPlugin {
12
12
  async exec(config: JssConfig) {
13
13
  return Object.assign({}, config, {
14
14
  defaultLanguage: config.defaultLanguage || 'en',
15
- sitecoreApiKey: config.sitecoreApiKey || 'no-api-key-set',
16
15
  <% if (!locals.xmcloud) { -%>
17
16
  layoutServiceConfigurationName: config.layoutServiceConfigurationName || 'default',
18
17
  <% } -%>
@@ -1,3 +1,4 @@
1
+ import 'dotenv/config';
1
2
  import * as fs from 'fs';
2
3
  import * as path from 'path';
3
4
  import { constantCase } from 'constant-case';
@@ -38,6 +39,14 @@ export function generateConfig(
38
39
  defaultConfig: JssConfig = defaultConfigValue,
39
40
  configOverrides?: { [key: string]: unknown }
40
41
  ) {
42
+ // Handle undefined values
43
+ defaultConfig = Object.keys(defaultConfig).reduce((acc, key) => {
44
+ return {
45
+ ...acc,
46
+ [key]: defaultConfig[key] || '',
47
+ };
48
+ }, {});
49
+
41
50
  jssConfigFactory
42
51
  .create(defaultConfig)
43
52
  .then((config) => {
@@ -67,9 +76,11 @@ export function writeConfig(config: JssConfig, outputPath?: string) {
67
76
 
68
77
  // Set base configuration values, allowing override with environment variables
69
78
  Object.keys(config).forEach((prop) => {
70
- configText += `config.${prop} = process.env.${constantCase(prop)} || "${config[prop]
71
- ?.toString()
72
- .trim()}";\n`;
79
+ // Handle undefined values
80
+ const value = config[prop] || '';
81
+ configText += `config.${prop} = process.env.${constantCase(
82
+ prop
83
+ )} || "${value.toString().trim()}";\n`;
73
84
  });
74
85
 
75
86
  configText += `module.exports.environment = config;`;
@@ -1,6 +1,7 @@
1
- import * as fetch from 'isomorphic-fetch';
2
1
  import * as fs from 'fs';
3
2
  import { generateConfig } from './generate-config';
3
+ import clientFactory from 'lib/graphql-client-factory';
4
+ import { getGraphQLClientFactoryConfig } from 'lib/graphql-client-factory/config';
4
5
 
5
6
  // Apollo Client supports caching GraphQL responses, which can greatly reduce network traffic needs.
6
7
  // In order to work correctly with interfaces in GraphQL, it needs to know some basic information about
@@ -11,26 +12,21 @@ import { generateConfig } from './generate-config';
11
12
 
12
13
  generateConfig('src/environments/environment.js');
13
14
 
14
- let jssConfig;
15
+ const clientFactoryConfig = getGraphQLClientFactoryConfig();
15
16
 
16
- try {
17
- jssConfig = require('../src/environments/environment').environment;
18
- } catch (e) {
19
- console.error(
20
- 'Unable to require JSS config. Ensure `jss setup` has been run, and the app has been started at least once after setup.'
21
- );
22
- console.error(e);
23
- process.exit(1);
24
- }
17
+ console.log(`Updating GraphQL fragment type data from ${clientFactoryConfig.endpoint}...`);
25
18
 
26
- console.log(`Updating GraphQL fragment type data from ${jssConfig.graphQLEndpoint}...`);
27
-
28
- fetch(jssConfig.graphQLEndpoint, {
29
- method: 'POST',
30
- headers: { 'Content-Type': 'application/json', sc_apikey: jssConfig.sitecoreApiKey },
31
- body: JSON.stringify({
32
- query: `
33
- {
19
+ clientFactory()
20
+ .request<{
21
+ [key: string]: unknown;
22
+ __schema: {
23
+ kind: string;
24
+ name: string;
25
+ types: { possibleTypes?: { name: string } }[];
26
+ };
27
+ }>(
28
+ `
29
+ {
34
30
  __schema {
35
31
  types {
36
32
  kind
@@ -41,22 +37,18 @@ fetch(jssConfig.graphQLEndpoint, {
41
37
  }
42
38
  }
43
39
  }
44
- `,
45
- }),
46
- })
47
- .then((result) => result.json())
40
+ `
41
+ )
48
42
  .then((result) => {
49
43
  // here we're filtering out any type information unrelated to unions or interfaces
50
- const filteredData = result.data.__schema.types.filter(
51
- (type: { possibleTypes: Array<string> }) => type.possibleTypes !== null
52
- );
44
+ const filteredData = result.__schema.types.filter((type) => type.possibleTypes !== null);
53
45
 
54
46
  const filteredResult = { ...result };
55
- filteredResult.data.__schema.types = filteredData;
47
+ filteredResult.__schema.types = filteredData;
56
48
 
57
49
  fs.writeFile(
58
50
  './src/graphql-fragment-types.ts',
59
- `export default ${JSON.stringify(filteredResult.data, null, 2)}`,
51
+ `export default ${JSON.stringify(filteredResult, null, 2)}`,
60
52
  (err) => {
61
53
  if (err) {
62
54
  console.error('Error writing GraphQLFragmentTypes file', err);
@@ -6,7 +6,8 @@ import 'zone.js';
6
6
  import { JssRouteBuilderService } from './src/app/routing/jss-route-builder.service';
7
7
  import { environment } from './src/environments/environment';
8
8
  import { AppServerModule, renderModule } from './src/main.server';
9
- import { clientFactory } from './src/app/lib/graphql-client-factory';
9
+ import clientFactory from './src/app/lib/graphql-client-factory';
10
+ import { getGraphQLClientFactoryConfig } from './src/app/lib/graphql-client-factory/config';
10
11
  import { dictionaryServiceFactory } from './src/app/lib/dictionary-service-factory';
11
12
  import { layoutServiceFactory } from './src/app/lib/layout-service-factory';
12
13
 
@@ -103,8 +104,7 @@ function parseRouteUrl(url: string) {
103
104
  const apiKey = environment.sitecoreApiKey;
104
105
  const siteName = environment.sitecoreSiteName;
105
106
  const defaultLanguage = environment.defaultLanguage;
106
- const graphQLEndpointPath = environment.graphQLEndpointPath;
107
- const graphQLEndpoint = environment.graphQLEndpoint;
107
+ const getClientFactoryConfig = getGraphQLClientFactoryConfig;
108
108
 
109
109
  export {
110
110
  renderView,
@@ -113,9 +113,8 @@ export {
113
113
  apiKey,
114
114
  siteName,
115
115
  clientFactory,
116
+ getClientFactoryConfig,
116
117
  dictionaryServiceFactory,
117
118
  layoutServiceFactory,
118
119
  defaultLanguage,
119
- graphQLEndpointPath,
120
- graphQLEndpoint,
121
120
  };
@@ -7,7 +7,7 @@ import {
7
7
  <% } -%>
8
8
  } from '@sitecore-jss/sitecore-jss-angular';
9
9
  import { environment as env } from '../../environments/environment';
10
- import { clientFactory } from './graphql-client-factory';
10
+ import clientFactory from './graphql-client-factory';
11
11
 
12
12
  export class DictionaryServiceFactory {
13
13
  create(): DictionaryService {
@@ -24,6 +24,9 @@ export class DictionaryServiceFactory {
24
24
  new GraphQLDictionaryService({
25
25
  clientFactory,
26
26
  siteName: env.sitecoreSiteName,
27
+ <% if (locals.xmcloud) { -%>
28
+ useSiteQuery: true,
29
+ <% } -%>
27
30
  /*
28
31
  The Dictionary Service needs a root item ID in order to fetch dictionary phrases for the current
29
32
  app. If your Sitecore instance only has 1 JSS App, you can specify the root item ID here;
@@ -0,0 +1,21 @@
1
+ import { GraphQLRequestClientFactoryConfig } from '@sitecore-jss/sitecore-jss-angular/cjs';
2
+ import { environment as env } from '../../../environments/environment';
3
+
4
+ /**
5
+ * Gets the configuration for the GraphQLRequestClientFactory
6
+ * @returns GraphQLRequestClientFactoryConfig
7
+ */
8
+ export const getGraphQLClientFactoryConfig = () => {
9
+ let clientConfig: GraphQLRequestClientFactoryConfig;
10
+
11
+ if (env.graphQLEndpoint && env.sitecoreApiKey) {
12
+ clientConfig = {
13
+ endpoint: env.graphQLEndpoint,
14
+ apiKey: env.sitecoreApiKey,
15
+ };
16
+ } else {
17
+ throw new Error('Please configure your graphQLEndpoint and sitecoreApiKey.');
18
+ }
19
+
20
+ return clientConfig;
21
+ };
@@ -0,0 +1,16 @@
1
+ import { GraphQLRequestClient } from '@sitecore-jss/sitecore-jss-angular/cjs';
2
+ import { getGraphQLClientFactoryConfig } from './config';
3
+
4
+ // The GraphQLRequestClientFactory serves as the central hub for executing GraphQL requests within the application
5
+
6
+ /**
7
+ * Creates a new GraphQLRequestClientFactory instance
8
+ * @returns GraphQLRequestClientFactory instance
9
+ */
10
+ const createGraphQLClientFactory = () => {
11
+ const clientConfig = getGraphQLClientFactoryConfig();
12
+
13
+ return GraphQLRequestClient.createClientFactory(clientConfig);
14
+ };
15
+
16
+ export default createGraphQLClientFactory();
@@ -7,7 +7,7 @@ import {
7
7
  <% } -%>
8
8
  } from '@sitecore-jss/sitecore-jss-angular';
9
9
  import { environment } from '../../environments/environment';
10
- import { clientFactory } from './graphql-client-factory';
10
+ import clientFactory from './graphql-client-factory';
11
11
 
12
12
  export class LayoutServiceFactory {
13
13
  create(): LayoutService {
@@ -8,7 +8,7 @@ const chalk = require('chalk');
8
8
  */
9
9
  class DisconnectedPlugin implements ConfigPlugin {
10
10
  // should come before other plugins
11
- order = 0;
11
+ order = 2;
12
12
 
13
13
  async exec(config: JssConfig) {
14
14
  const disconnected = process.env.JSS_MODE === constants.JSS_MODE.DISCONNECTED;
@@ -20,7 +20,9 @@ class DisconnectedPlugin implements ConfigPlugin {
20
20
  );
21
21
  }
22
22
 
23
- return config;
23
+ return Object.assign({}, config, {
24
+ sitecoreApiKey: config.sitecoreApiKey || 'no-api-key-set',
25
+ });
24
26
  }
25
27
  }
26
28
 
@@ -1,4 +1,10 @@
1
1
 
2
+ # ========== Sitecore Edge Platform ===========
3
+
4
+ # Your unified Sitecore Edge Context Id.
5
+ # This will be used over any Sitecore Preview / Delivery Edge variables (above).
6
+ SITECORE_EDGE_CONTEXT_ID=
7
+
2
8
  # ========== XM Cloud Proxy ===========
3
9
 
4
10
  # Your XM Cloud Proxy hostname is needed to build the app and execute the client-side requests against the proxy server.
@@ -1,6 +1,7 @@
1
1
  import { JssConfig } from 'lib/config';
2
2
  import { ConfigPlugin } from '..';
3
3
  import { constantCase } from 'constant-case';
4
+ import chalk from 'chalk';
4
5
 
5
6
  /**
6
7
  * This plugin will set XM Cloud related config props.
@@ -13,9 +14,24 @@ class XMCloudPlugin implements ConfigPlugin {
13
14
  const proxyBuildPath = process.env[`${constantCase('proxyBuildPath')}`]?.replace(/\/$/, '');
14
15
  const proxyHost = process.env[`${constantCase('proxyHost')}`];
15
16
 
17
+ const sitecoreEdgeUrl =
18
+ process.env[`${constantCase('sitecoreEdgeUrl')}`]?.replace(/\/$/, '') ||
19
+ 'https://edge-platform.sitecorecloud.io';
20
+ const sitecoreEdgeContextId = process.env[`${constantCase('sitecoreEdgeContextId')}`];
21
+
22
+ if (config.sitecoreApiKey && sitecoreEdgeContextId) {
23
+ console.log(
24
+ chalk.yellow(
25
+ "You have configured both 'sitecoreApiKey' and 'sitecoreEdgeContextId' values. The 'sitecoreEdgeContextId' is used instead."
26
+ )
27
+ );
28
+ }
29
+
16
30
  return Object.assign({}, config, {
17
31
  proxyBuildPath,
18
32
  proxyHost,
33
+ sitecoreEdgeUrl,
34
+ sitecoreEdgeContextId,
19
35
  });
20
36
  }
21
37
  }
@@ -12,4 +12,6 @@ export interface JssConfig extends Record<string, string | boolean | undefined>
12
12
  defaultServerRoute?: string;
13
13
  proxyBuildPath?: string;
14
14
  proxyHost?: string;
15
+ sitecoreEdgeUrl?: string;
16
+ sitecoreEdgeContextId?: string;
15
17
  }
@@ -0,0 +1,58 @@
1
+ import {
2
+ GraphQLRequestClientFactoryConfig,
3
+ getEdgeProxyContentUrl,
4
+ } from '@sitecore-jss/sitecore-jss-angular/cjs';
5
+ import { environment as env } from '../../../environments/environment';
6
+
7
+ /**
8
+ * Gets the configuration for the GraphQLRequestClientFactory
9
+ * @returns GraphQLRequestClientFactoryConfig
10
+ */
11
+ export const getGraphQLClientFactoryConfig = () => {
12
+ let clientConfig: GraphQLRequestClientFactoryConfig;
13
+
14
+ // Server side requests should go directly to the Sitecore, browser requests should go through the proxy.
15
+ const isServer = typeof window === 'undefined';
16
+ // If we are in a production environment we are going to use Node XM Cloud proxy
17
+ const isProduction = env.production === 'true';
18
+
19
+ if (isProduction) {
20
+ if (!env.proxyHost) {
21
+ throw new Error('Please configure your proxyHost.');
22
+ }
23
+
24
+ if (env.sitecoreEdgeContextId) {
25
+ clientConfig = {
26
+ endpoint: isServer
27
+ ? getEdgeProxyContentUrl(env.sitecoreEdgeContextId, env.sitecoreEdgeUrl)
28
+ : getEdgeProxyContentUrl(env.sitecoreEdgeContextId, env.proxyHost),
29
+ };
30
+ } else if (env.graphQLEndpoint && env.sitecoreApiKey) {
31
+ const graphQLEndpointPath = new URL(env.graphQLEndpoint).pathname;
32
+
33
+ clientConfig = {
34
+ endpoint: isServer ? env.graphQLEndpoint : `${env.proxyHost}${graphQLEndpointPath}`,
35
+ apiKey: env.sitecoreApiKey,
36
+ };
37
+ }
38
+ } else {
39
+ if (env.sitecoreEdgeContextId) {
40
+ clientConfig = {
41
+ endpoint: getEdgeProxyContentUrl(env.sitecoreEdgeContextId, env.sitecoreEdgeUrl),
42
+ };
43
+ } else if (env.graphQLEndpoint && env.sitecoreApiKey) {
44
+ clientConfig = {
45
+ endpoint: env.graphQLEndpoint,
46
+ apiKey: env.sitecoreApiKey,
47
+ };
48
+ }
49
+ }
50
+
51
+ if (!clientConfig.endpoint) {
52
+ throw new Error(
53
+ 'Please configure either your sitecoreEdgeContextId, or your graphQLEndpoint and sitecoreApiKey.'
54
+ );
55
+ }
56
+
57
+ return clientConfig;
58
+ };
@@ -11,6 +11,7 @@ import { Subscription } from 'rxjs';
11
11
  import { JssState } from '../../JssState';
12
12
  import { JssMetaService } from '../../jss-meta.service';
13
13
  import { JssLinkService } from '../../jss-link.service';
14
+ import { environment as env } from '../../../environments/environment';
14
15
 
15
16
  enum LayoutState {
16
17
  Layout,
@@ -58,13 +59,10 @@ export class LayoutComponent implements OnInit, OnDestroy {
58
59
  ? 'editing-mode'
59
60
  : 'prod-mode';
60
61
 
61
- /** TODO: get contextId and edgeUrl properly **/
62
- const sitecoreEdgeContextId = '';
63
- const sitecoreEdgeUrl = '';
64
62
  const contentStyles = getContentStylesheetLink(
65
63
  { sitecore: data.jssState.sitecore },
66
- sitecoreEdgeContextId,
67
- sitecoreEdgeUrl
64
+ env.sitecoreEdgeContextId,
65
+ env.sitecoreEdgeUrl
68
66
  );
69
67
 
70
68
  // Clear existing stylesheets
@@ -13,7 +13,6 @@ class FallbackPlugin implements ConfigPlugin {
13
13
  async exec(config: JssConfig) {
14
14
  return Object.assign({}, config, {
15
15
  defaultLanguage: config.defaultLanguage || 'en',
16
- sitecoreApiKey: config.sitecoreApiKey || 'no-api-key-set',
17
16
  layoutServiceConfigurationName: config.layoutServiceConfigurationName || 'default',
18
17
  publicUrl: config.publicUrl || getPublicUrl(),
19
18
  });
@@ -29,6 +29,11 @@ generateConfig(defaultConfig);
29
29
  * @param {JssConfig} defaultConfig Default configuration.
30
30
  */
31
31
  function generateConfig(defaultConfig: JssConfig): void {
32
+ // Handle undefined values
33
+ Object.keys(defaultConfig).forEach(prop => {
34
+ defaultConfig[prop] = defaultConfig[prop] || '';
35
+ }, {});
36
+
32
37
  jssConfigFactory
33
38
  .create(defaultConfig)
34
39
  .then(config => {
@@ -53,7 +58,9 @@ const config = {};\n`;
53
58
 
54
59
  // Set configuration values, allowing override with environment variables
55
60
  Object.keys(config).forEach(prop => {
56
- configText += `config.${prop} = process.env.${constantCase(prop)} || '${config[prop]?.trim()}';\n`;
61
+ // Handle undefined values
62
+ const value = config[prop] || '';
63
+ configText += `config.${prop} = process.env.${constantCase(prop)} || '${value.trim()}';\n`;
57
64
  });
58
65
 
59
66
  configText += `module.exports = config;`;
@@ -29,6 +29,7 @@ class DisconnectedPlugin implements ConfigPlugin {
29
29
 
30
30
  return Object.assign({}, config, {
31
31
  sitecoreApiHost: `http://localhost:${port}`,
32
+ sitecoreApiKey: config.sitecoreApiKey || 'no-api-key-set',
32
33
  });
33
34
  }
34
35
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-xmcloud-sample",
3
- "version": "22.1.0-canary",
3
+ "version": "22.2.0-canary",
4
4
  "description": "Node XM Cloud Proxy sample for running XM Cloud JSS SPA apps",
5
5
  "author": {
6
6
  "name": "Sitecore Corporation",
@@ -16,7 +16,7 @@
16
16
  "http-proxy-middleware": "^3.0.0"
17
17
  },
18
18
  "devDependencies": {
19
- "@sitecore-jss/sitecore-jss": "~22.1.0-canary",
19
+ "@sitecore-jss/sitecore-jss": "~22.2.0-canary",
20
20
  "@types/compression": "^1.7.2",
21
21
  "@types/express": "^4.17.17",
22
22
  "ts-node": "^10.9.1",
@@ -3,7 +3,7 @@ import { Config, ServerBundle } from './types';
3
3
  /**
4
4
  * The server.bundle.js file from your pre-built SPA app.
5
5
  */
6
- const bundlePath = process.env.PROXY_BUNDLE_PATH || `../dist/server.bundle`;
6
+ const bundlePath = process.env.PROXY_BUNDLE_PATH || '../dist/server.bundle';
7
7
 
8
8
  const serverBundle: ServerBundle = require(bundlePath);
9
9
 
@@ -21,12 +21,11 @@ const requiredProperties = [
21
21
  'renderView',
22
22
  'parseRouteUrl',
23
23
  'clientFactory',
24
+ 'getClientFactoryConfig',
24
25
  'siteName',
25
26
  'defaultLanguage',
26
27
  'layoutServiceFactory',
27
28
  'dictionaryServiceFactory',
28
- 'graphQLEndpointPath',
29
- 'graphQLEndpoint',
30
29
  ];
31
30
 
32
31
  const missingProperties = requiredProperties.filter((property) => !config.serverBundle[property]);
@@ -39,6 +38,31 @@ if (missingProperties.length > 0) {
39
38
  );
40
39
  }
41
40
 
41
+ /**
42
+ * GraphQL endpoint resolution to meet the requirements of the http-proxy-middleware
43
+ */
44
+ const graphQLEndpoint = (() => {
45
+ const clientFactoryConfig = config.serverBundle.getClientFactoryConfig();
46
+
47
+ try {
48
+ const graphQLEndpoint = new URL(clientFactoryConfig.endpoint);
49
+ // Browser request path to the proxy. Includes only the pathname.
50
+ const pathname = graphQLEndpoint.pathname;
51
+ // Target URL for the proxy. Can't include the query string.
52
+ const target = `${graphQLEndpoint.protocol}//${graphQLEndpoint.hostname}${pathname}`;
53
+
54
+ return {
55
+ target,
56
+ path: pathname,
57
+ };
58
+ } catch (error) {
59
+ throw new Error(
60
+ `ERROR: The serverBundle should export a getClientFactoryConfig function with valid GraphQL endpoint URL returned, current value is ${clientFactoryConfig.endpoint}. ` +
61
+ 'Please check your server bundle.'
62
+ );
63
+ }
64
+ })();
65
+
42
66
  const layoutService = layoutServiceFactory.create();
43
67
 
44
68
  const dictionaryService = dictionaryServiceFactory.create();
@@ -94,9 +118,9 @@ server.use(
94
118
  * Proxy browser GraphQL requests to the Sitecore GraphQL endpoint
95
119
  */
96
120
  server.use(
97
- config.serverBundle.graphQLEndpointPath,
121
+ graphQLEndpoint.path,
98
122
  createProxyMiddleware({
99
- target: config.serverBundle.graphQLEndpoint,
123
+ target: graphQLEndpoint.target,
100
124
  changeOrigin: true,
101
125
  })
102
126
  );
@@ -1,4 +1,7 @@
1
- import { GraphQLRequestClientFactory } from '@sitecore-jss/sitecore-jss';
1
+ import {
2
+ GraphQLRequestClientFactory,
3
+ GraphQLRequestClientFactoryConfig,
4
+ } from '@sitecore-jss/sitecore-jss';
2
5
  import { DictionaryService } from '@sitecore-jss/sitecore-jss/i18n';
3
6
  import { LayoutService, LayoutServiceData } from '@sitecore-jss/sitecore-jss/layout';
4
7
 
@@ -44,12 +47,11 @@ export interface ServerBundle {
44
47
  renderView: AppRenderer;
45
48
  parseRouteUrl: RouteUrlParser;
46
49
  clientFactory: GraphQLRequestClientFactory;
50
+ getClientFactoryConfig: () => GraphQLRequestClientFactoryConfig;
47
51
  siteName: string;
48
52
  defaultLanguage: string;
49
53
  layoutServiceFactory: { create: () => LayoutService };
50
54
  dictionaryServiceFactory: { create: () => DictionaryService };
51
- graphQLEndpointPath: string;
52
- graphQLEndpoint: string;
53
55
  }
54
56
 
55
57
  export interface Config {
@@ -31,6 +31,11 @@ generateConfig();
31
31
  * NOTE! Any configs returned here will be written into the client-side JS bundle. DO NOT PUT SECRETS HERE.
32
32
  */
33
33
  function generateConfig() {
34
+ // Handle undefined values
35
+ Object.keys(defaultConfig).forEach((prop) => {
36
+ defaultConfig[prop] = defaultConfig[prop] || '';
37
+ });
38
+
34
39
  try {
35
40
  config = jssConfigFactory.create(defaultConfig);
36
41
  } catch (error) {
@@ -50,9 +55,11 @@ const config = {};\n`;
50
55
 
51
56
  // Set base configuration values, allowing override with environment variables
52
57
  Object.keys(config).forEach(prop => {
53
- configText += `config.${prop} = process.env.REACT_APP_${constantCase(prop)} || "${
54
- config[prop]?.trim()
55
- }";\n`;
58
+ // Handle undefined values
59
+ const value = config[prop] || '';
60
+ configText += `config.${prop} = process.env.REACT_APP_${constantCase(
61
+ prop
62
+ )} || "${value.trim()}";\n`;
56
63
  });
57
64
  configText += 'module.exports = config;';
58
65
 
@@ -33,6 +33,11 @@ module.exports = function generateConfig(configOverrides) {
33
33
  // and finally config passed in the configOverrides param wins.
34
34
  const config = Object.assign(defaultConfig, scjssConfig, packageJson, configOverrides);
35
35
 
36
+ // Handle undefined values
37
+ Object.keys(config).forEach((prop) => {
38
+ config[prop] = config[prop] || '';
39
+ });
40
+
36
41
  // The GraphQL endpoint is an example of making a _computed_ config setting
37
42
  // based on other config settings.
38
43
  const computedConfig = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sitecore-jss",
3
- "version": "22.2.0-canary.10",
3
+ "version": "22.2.0-canary.11",
4
4
  "description": "Sitecore JSS initializer",
5
5
  "bin": "./dist/index.js",
6
6
  "scripts": {
@@ -63,5 +63,5 @@
63
63
  "ts-node": "^10.9.1",
64
64
  "typescript": "~4.9.5"
65
65
  },
66
- "gitHead": "d5b46f1661d99e8936746e964dc3c2ecd61b9e05"
66
+ "gitHead": "f4cca11e7c8662590ae43f3100065f0adb1728c5"
67
67
  }
@@ -1,28 +0,0 @@
1
- import {
2
- GraphQLRequestClientFactoryConfig,
3
- GraphQLRequestClient,
4
- } from '@sitecore-jss/sitecore-jss-angular';
5
- import { environment as env } from '../../environments/environment';
6
-
7
- // The GraphQLRequestClientFactory serves as the central hub for executing GraphQL requests within the application
8
-
9
- /**
10
- * Creates a new GraphQLRequestClientFactory instance
11
- * @returns GraphQLRequestClientFactory instance
12
- */
13
- export const createGraphQLClientFactory = () => {
14
- let clientConfig: GraphQLRequestClientFactoryConfig;
15
-
16
- if (env.graphQLEndpoint && env.sitecoreApiKey) {
17
- clientConfig = {
18
- endpoint: env.graphQLEndpoint,
19
- apiKey: env.sitecoreApiKey,
20
- };
21
- } else {
22
- throw new Error('Please configure your graphQLEndpoint and sitecoreApiKey.');
23
- }
24
-
25
- return GraphQLRequestClient.createClientFactory(clientConfig);
26
- };
27
-
28
- export const clientFactory = createGraphQLClientFactory();
@@ -1,44 +0,0 @@
1
- import {
2
- GraphQLRequestClientFactoryConfig,
3
- GraphQLRequestClient,
4
- } from '@sitecore-jss/sitecore-jss-angular';
5
- import { environment as env } from '../../environments/environment';
6
-
7
- // The GraphQLRequestClientFactory serves as the central hub for executing GraphQL requests within the application
8
-
9
- /**
10
- * Creates a new GraphQLRequestClientFactory instance
11
- * @returns GraphQLRequestClientFactory instance
12
- */
13
- export const createGraphQLClientFactory = () => {
14
- let clientConfig: GraphQLRequestClientFactoryConfig;
15
-
16
- // If we are in a production environment we are going to use Node XM Cloud proxy
17
- if (env.production === 'true') {
18
- if (env.proxyHost && env.sitecoreApiKey && env.graphQLEndpoint) {
19
- // Server side requests should go directly to the Sitecore, browser requests should go through the proxy.
20
- clientConfig = {
21
- endpoint:
22
- typeof window === 'undefined'
23
- ? env.graphQLEndpoint
24
- : `${env.proxyHost}${env.graphQLEndpointPath}`,
25
- apiKey: env.sitecoreApiKey,
26
- };
27
- } else {
28
- throw new Error('Please configure your proxyHost, sitecoreApiKey, graphQLEndpoint.');
29
- }
30
- } else {
31
- if (env.graphQLEndpoint && env.sitecoreApiKey) {
32
- clientConfig = {
33
- endpoint: env.graphQLEndpoint,
34
- apiKey: env.sitecoreApiKey,
35
- };
36
- } else {
37
- throw new Error('Please configure your graphQLEndpoint and sitecoreApiKey.');
38
- }
39
- }
40
-
41
- return GraphQLRequestClient.createClientFactory(clientConfig);
42
- };
43
-
44
- export const clientFactory = createGraphQLClientFactory();