From 4ab500c7a83ef1b24ea3658ee28943280af64be0 Mon Sep 17 00:00:00 2001 From: Jon Wiese Date: Mon, 6 Dec 2021 09:17:13 -0600 Subject: [PATCH] fix: strf-9535 Add fallback for shopper language default --- lib/stencil-start.js | 37 ++++++++++++---------- lib/store-settings-api-client.js | 20 ++++++++---- server/index.js | 2 ++ server/plugins/renderer/renderer.module.js | 12 ++++++- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lib/stencil-start.js b/lib/stencil-start.js index ce196959..3fe3bb77 100755 --- a/lib/stencil-start.js +++ b/lib/stencil-start.js @@ -68,6 +68,11 @@ class StencilStart { storeInfoFromAPI, ); + this._storeSettingsLocale = await this.getStoreSettingsLocale( + cliOptions, + updatedStencilConfig, + ); + await this.startLocalServer(cliOptions, updatedStencilConfig); this._logger.log(this.getStartUpInfo(updatedStencilConfig)); @@ -75,6 +80,16 @@ class StencilStart { await this.startBrowserSync(cliOptions, updatedStencilConfig, browserSyncPort); } + async getStoreSettingsLocale(cliOptions, stencilConfig) { + const { accessToken } = stencilConfig; + const apiHost = cliOptions.apiHost || stencilConfig.apiHost; + return this._storeSettingsApiClient.getStoreSettingsLocale({ + storeHash: this.storeHash, + accessToken, + apiHost, + }); + } + async getChannelUrl(stencilConfig, cliOptions) { const { accessToken } = stencilConfig; const apiHost = cliOptions.apiHost || stencilConfig.apiHost; @@ -97,16 +112,6 @@ class StencilStart { return foundChannel ? foundChannel.url : null; } - async getDefaultShopperLanguage(cliOptions, stencilConfig) { - const { accessToken } = stencilConfig; - const apiHost = cliOptions.apiHost || stencilConfig.apiHost; - return this._storeSettingsApiClient.getDefaultShopperLanguage({ - storeHash: this.storeHash, - accessToken, - apiHost, - }); - } - /** * @param {Object} cliOptions */ @@ -148,6 +153,7 @@ class StencilStart { useCache: cliOptions.cache, themePath: this._themeConfigManager.themePath, stencilCliVersion: PACKAGE_INFO.version, + storeSettingsLocale: this._storeSettingsLocale, }); } @@ -156,10 +162,6 @@ class StencilStart { const DEFAULT_WATCH_IGNORED = ['/assets/scss', '/assets/css']; const { themePath, configPath } = this._themeConfigManager; const { watchOptions } = this._buildConfigManager; - const defaultShopperLanguage = await this.getDefaultShopperLanguage( - cliOptions, - stencilConfig, - ); // Watch sccs directory and automatically reload all css files if a file changes const stylesPath = path.join(themePath, 'assets/scss'); @@ -203,7 +205,10 @@ class StencilStart { this._browserSync.watch(langsPath, async (event) => { try { if (event === 'change') { - await this.checkLangFiles(langsPath, defaultShopperLanguage); + await this.checkLangFiles( + langsPath, + this._storeSettingsLocale.default_shopper_language, + ); } } catch (e) { this._logger.error(e); @@ -247,7 +252,7 @@ class StencilStart { this._buildConfigManager.initWorker().development(this._browserSync); } - await this.checkLangFiles(langsPath, defaultShopperLanguage); + await this.checkLangFiles(langsPath, this._storeSettingsLocale.default_shopper_language); } /** diff --git a/lib/store-settings-api-client.js b/lib/store-settings-api-client.js index a6c253bb..b48f976c 100644 --- a/lib/store-settings-api-client.js +++ b/lib/store-settings-api-client.js @@ -3,24 +3,32 @@ const NetworkUtils = require('./utils/NetworkUtils'); const networkUtils = new NetworkUtils(); -async function getDefaultShopperLanguage({ apiHost, storeHash, accessToken }) { +async function getStoreSettingsLocale({ apiHost, storeHash, accessToken }) { try { const response = await networkUtils.sendApiRequest({ url: `${apiHost}/stores/${storeHash}/v3/settings/store/locale`, accessToken, }); - if (!response.data.data || !response.data.data.default_shopper_language) { + + if (!response.data.data) { + throw new Error('Received empty store locale in the server response'.red); + } else if (!response.data.data.default_shopper_language) { + throw new Error( + 'Received empty default_shopper_language field in the server response'.red, + ); + } else if (!response.data.data.shopper_language_selection_method) { throw new Error( - 'Received empty default_shopper_language value in the server response'.red, + 'Received empty shopper_language_selection_method field in the server response'.red, ); } - return response.data.data.default_shopper_language; + + return response.data.data; } catch (err) { - err.name = 'DefaultShopperLanguageError'; + err.name = 'StoreSettingsLocaleError'; throw err; } } module.exports = { - getDefaultShopperLanguage, + getStoreSettingsLocale, }; diff --git a/server/index.js b/server/index.js index 2857ce1e..da2850d6 100644 --- a/server/index.js +++ b/server/index.js @@ -29,6 +29,8 @@ function buildManifest(srcManifest, options) { options.dotStencilFile.customLayouts; pluginsByName['./plugins/renderer/renderer.module'].themePath = options.themePath; pluginsByName['./plugins/renderer/renderer.module'].storeUrl = storeUrl; + pluginsByName['./plugins/renderer/renderer.module'].storeSettingsLocale = + options.storeSettingsLocale; pluginsByName['./plugins/theme-assets/theme-assets.module'].themePath = options.themePath; resManifest.register.plugins = _.reduce( diff --git a/server/plugins/renderer/renderer.module.js b/server/plugins/renderer/renderer.module.js index efc73878..ca57fb3d 100644 --- a/server/plugins/renderer/renderer.module.js +++ b/server/plugins/renderer/renderer.module.js @@ -364,6 +364,16 @@ internals.getTemplatePath = (requestPath, data) => { return templatePath || data.template_file; }; +function getAcceptLanguageHeader(request) { + if ( + internals.options.storeSettingsLocale.shopper_language_selection_method === + 'default_shopper_language' + ) { + return internals.options.storeSettingsLocale.default_shopper_language; + } + return request.headers['accept-language']; +} + /** * Creates a new Pencil Response object and returns it. * @@ -400,7 +410,7 @@ internals.getPencilResponse = (data, request, response, configuration, renderedR context, translations: data.translations, method: request.method, - acceptLanguage: request.headers['accept-language'], + acceptLanguage: getAcceptLanguageHeader(request), headers: response.headers, statusCode: response.status, },