Skip to content

Commit

Permalink
fix(storefront): strf-10193 Bad request for custom front matter: (#1019)
Browse files Browse the repository at this point in the history
* fix(storefront): strf-10193 Bad request for custom front matter:

- template is get locally;
- test fix;
  • Loading branch information
bc-max authored Nov 4, 2022
1 parent 127cde2 commit b7aefca
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 149 deletions.
28 changes: 7 additions & 21 deletions server/plugins/renderer/renderer.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const {
} = require('../../../lib/utils/frontmatter');

const networkUtils = new NetworkUtils();
const { DEFAULT_CUSTOM_LAYOUTS_CONFIG } = require('../../../constants');

const internals = {
options: {},
Expand Down Expand Up @@ -79,14 +78,12 @@ internals.getResponse = async (request) => {
const withoutPageQuery = new URL(fullUrl.toString());
withoutPageQuery.searchParams.delete('page');

const { customLayouts } = internals.options;

const httpOpts = {
url: withoutPageQuery.toString(),
headers: internals.buildReqHeaders({
request,
stencilOptions: { get_template_file: true, get_data_only: true },
extraHeaders: { host: storeUrlObj.host, stencil_custom_templates: customLayouts },
extraHeaders: { host: storeUrlObj.host },
}),
accessToken: internals.options.accessToken,
data: request.payload,
Expand Down Expand Up @@ -119,7 +116,6 @@ internals.getResponse = async (request) => {
// clear when making a non-get request because smth may be changed
cache.clear();
}

const response = await networkUtils.sendApiRequest(httpOpts);

internals.processResHeaders(response.headers);
Expand All @@ -137,6 +133,7 @@ internals.getResponse = async (request) => {
? JSON.parse(await readFromStream(response.data))
: response.data;

internals.updateTemplatePath(request.path, bcAppData);
// cache response
cache.put(
requestSignature,
Expand All @@ -156,13 +153,10 @@ internals.getResponse = async (request) => {
* @param request
* @param response
* @param responseArgs
* @param customLayouts
* @returns {*}
*/
internals.parseResponse = async (bcAppData, request, response, responseArgs) => {
const { httpOpts, storeUrlObj } = responseArgs;
// eslint-disable-next-line camelcase
const { stencil_custom_templates } = httpOpts.headers;

if (typeof bcAppData !== 'object' || !('pencil_response' in bcAppData)) {
delete response.headers['x-frame-options'];
Expand All @@ -185,7 +179,6 @@ internals.parseResponse = async (bcAppData, request, response, responseArgs) =>
stencilConfig: internals.getResourceConfig(bcAppData, request, configuration),
extraHeaders: {
host: storeUrlObj.host,
stencil_custom_templates,
},
});
httpOpts.responseType = 'json'; // In the second request we always expect json
Expand Down Expand Up @@ -344,9 +337,9 @@ internals.redirect = async (response, request) => {
*
* @param {string} requestPath
* @param {Object} data
* @returns {string}
* @returns {void}
*/
internals.getTemplatePath = (requestPath, data) => {
internals.updateTemplatePath = (requestPath, data) => {
const customLayouts = internals.options.customLayouts || {};
const pageType = data.page_type;
let templatePath;
Expand All @@ -369,10 +362,10 @@ internals.getTemplatePath = (requestPath, data) => {

if (templatePath) {
templatePath = path.join('pages/custom', pageType, templatePath.replace(/\.html$/, ''));
// eslint-disable-next-line no-param-reassign
data.template_file = templatePath;
}
}

return templatePath || data.template_file;
};

function getAcceptLanguageHeader(request) {
Expand Down Expand Up @@ -413,7 +406,7 @@ internals.getPencilResponse = (data, request, response, configuration, renderedR

return new PencilResponse(
{
template_file: internals.getTemplatePath(request.path, data),
template_file: data.template_file,
templates: data.templates,
remote: data.remote,
remote_data: data.remote_data,
Expand Down Expand Up @@ -451,23 +444,16 @@ internals.buildReqHeaders = ({
? JSON.parse(request.headers['stencil-options'])
: {};

const templates =
JSON.stringify(extraHeaders.stencil_custom_templates) ||
JSON.stringify(DEFAULT_CUSTOM_LAYOUTS_CONFIG);

const headers = {
'stencil-options': JSON.stringify({ ...stencilOptions, ...currentOptions }),
'accept-encoding': 'identity',
'stencil-custom-templates': templates,
};

const config = request.headers['stencil-config'];

if ((!config || config === '{}') && stencilConfig) {
headers['stencil-config'] = JSON.stringify(stencilConfig);
}
// eslint-disable-next-line no-param-reassign
delete extraHeaders.stencil_custom_templates;

return { ...request.headers, ...headers, ...extraHeaders };
};
Expand Down
116 changes: 0 additions & 116 deletions server/plugins/renderer/renderer.module.option2.spec.js

This file was deleted.

12 changes: 0 additions & 12 deletions server/plugins/renderer/renderer.module.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ describe('Renderer Plugin', () => {
port: 4000,
username: 'testUser',
token: '6832b1c755bb9de13aa8990216a69a7623043fd7',
customLayouts: {
brand: {
a: '/abc/',
},
category: {},
page: {},
product: {},
},
},
useCache: false,
themePath: themeConfigManager.themePath,
Expand Down Expand Up @@ -167,8 +159,6 @@ describe('Renderer Plugin', () => {
'stencil-options': '{"get_template_file":true,"get_data_only":true}',
'stencil-version': PACKAGE_INFO.config.stencil_version,
'accept-encoding': 'identity',
'stencil-custom-templates':
'{"brand":{"a":"/abc/"},"category":{},"page":{},"product":{}}',
});
});

Expand Down Expand Up @@ -264,8 +254,6 @@ describe('Renderer Plugin', () => {
'stencil-options': '{"get_template_file":true,"get_data_only":true}',
'stencil-version': PACKAGE_INFO.config.stencil_version,
'accept-encoding': 'identity',
'stencil-custom-templates':
'{"brand":{"a":"/abc/"},"category":{},"page":{},"product":{}}',
});
});

Expand Down

0 comments on commit b7aefca

Please sign in to comment.