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

feat(other): MERC-7797 Added feature to render Widgets in the regions of supported page types. #726

Merged
merged 1 commit into from
Jul 13, 2021

Conversation

jkanive
Copy link
Contributor

@jkanive jkanive commented Jun 29, 2021

What?

Added logic to rendered the widgets in the respective regions of a page. This is done by making GraphQL request for each page request and setting the content via the paper handlebar helper setContent.

Tickets / Documentation

Add links to any relevant tickets and documentation.

@junedkazi Would appreciate your input on this PR.

@jkanive jkanive force-pushed the MERC-7797-1 branch 2 times, most recently from 2899f97 to 3f483f3 Compare June 29, 2021 02:12
lib/content-api-client.js Outdated Show resolved Hide resolved
const cachedGraphQLResponse = cache.get(graphQLDataReqSignature);

if (internals.options.useCache && cachedGraphQLResponse) {
({ regionResponse } = cachedGraphQLResponse);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's extracting cached value of regionResponse instead of making a graphQL call.

@jkanive jkanive force-pushed the MERC-7797-1 branch 2 times, most recently from 8d5e5d3 to c650ef2 Compare July 6, 2021 17:32
@jkanive
Copy link
Contributor Author

jkanive commented Jul 7, 2021

♻️ Addressed comments.

@jkanive jkanive requested review from christensenep and jairo-bc July 7, 2021 16:10
* @param {number} entityId
* @returns {Promise<renderedRegions: array}>}
*/
async function getRenderedRegionsByPageTypeAndEntityId({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda surprised these need to be two separate functions, isn't really just affecting the query slightly when you don't supply an entity id?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait right as I clicked the button I realized that parsing the response is slightly different too, yeah fair enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using the GraphQL Playground to build the query it pretty handy because it does the validation of whether a pageType needs entityId or not. I am guessing that was the reasoning for having separate function as its difficult to make a argument conditionally required on the same function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry I was speaking of these functions, not the graphql query itself. But anyway yeah given that it's a different graphql field think this makes sense.

UNSUBSCRIBE: 'UNSUBSCRIBE',
};

const templateFileToPageTypeMap = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really wish we could fetch this from somewhere or include it as a library or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mapping is in Storefront also now, so we technically are doing this in 3 places. BCApp, Storefront and now in Stencil CLI 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we in hurry with this ticket, so we are introducing here tech debt? If so, lets create a ticket to address it by adding an endpoint to Storefront (or other more relevant service)

return false;
}

return pageTypeWithEntityId.indexOf(pageType) >= 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually necessary? Can we not just check if the entityId is present and just trust that it's correct rather than checking another list we have to maintain here?

@@ -137,6 +138,7 @@ class PencilResponse {
this.data.context.in_production = false;

paper.addDecorator(makeDecorator(request, this.data.context));
paper.setContent(this.data.renderedRegions);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What?!? My mind is blown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have paper helper which we use in Storefront renderer as well which takes input of an object with keys as the regionName and value as the html. So I am formatting it and just feeding it here. I was doing literal search a replace before I thought of figuring out how storefront renderer does it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah sorry that was not actually a question, I was just amazed how simple it was. I know you told me before about this, just seeing it is kind of amazing.


const networkUtils = new NetworkUtils();

const internals = {
options: {},
cacheTTL: 1000 * 15, // 15 seconds
graphQLCacheTTL: 1000 * 300, // 5 minutes
Copy link

@christensenep christensenep Jul 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought for the future, it might be worth considering literally never refreshing the cache without user intervention, or making it a cli setting or something. Just imagining in the vast majority of use cases the user wouldn't be expecting or needing the widgets to update. (I don't really have a sense for how many users are using stencil cli at any given time, admittedly it's probably only a handful so not a huge deal at the moment).

Mostly just reacting to this sort of being a weird in-between solution that doesn't serve anyone that well. If you do want to see widgets changing (if you're working on them at the same time as you use stencil cli), 5 minutes is really long and you're probably better off restarting stencil cli each time you change them. And if you don't, there's no need to ever refresh the cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an option to start the stencil-cli without using cache stencil start --no-cache. So it might be work changing the TTL to say 60mins or more. I think the TTL is not optional and would throw an error if not provided.

@jkanive jkanive force-pushed the MERC-7797-1 branch 2 times, most recently from 9331828 to 785ffa2 Compare July 7, 2021 19:29
@jkanive
Copy link
Contributor Author

jkanive commented Jul 7, 2021

♻️ Addressed comments.

});

const {
site: { content },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is content variable for readability?
It's possible to do like this:

Suggested change
site: { content },
const { site: { regions: { renderedRegionsByPageType } } } = response.data.data

renderedRegions: regions,
};
} catch (err) {
console.log(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.error

renderedRegions: regions,
};
} catch (err) {
console.log(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.error
It's also possible to highlight error with red color like here https://github.com/bigcommerce/stencil-cli/blob/master/lib/release/release.js#L145

server/index.js Outdated
@@ -27,6 +27,8 @@ function buildManifest(srcManifest, options) {
pluginsByName['./plugins/renderer/renderer.module'].customLayouts =
options.dotStencilFile.customLayouts;
pluginsByName['./plugins/renderer/renderer.module'].themePath = options.themePath;
pluginsByName['./plugins/renderer/renderer.module'].storeUrl =
parsedSecureUrl.protocol + '//' + parsedSecureUrl.host;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a variable storeUrl and reuse it on line 16 as well

UNSUBSCRIBE: 'UNSUBSCRIBE',
};

const templateFileToPageTypeMap = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we in hurry with this ticket, so we are introducing here tech debt? If so, lets create a ticket to address it by adding an endpoint to Storefront (or other more relevant service)

@jkanive
Copy link
Contributor Author

jkanive commented Jul 12, 2021

Are we in hurry with this ticket, so we are introducing here tech debt? If so, lets create a ticket to address it by adding an endpoint to Storefront (or other more relevant service)

@jairo-bc I have created a ticket MERC-7975 to address the mapping tech debt and will look into it in a follow up PR.

@jkanive
Copy link
Contributor Author

jkanive commented Jul 12, 2021

♻️ Addressed all the comments.
cc: @jairo-bc @christensenep

@jkanive jkanive requested a review from jairo-bc July 12, 2021 19:53
Copy link
Contributor

@jairo-bc jairo-bc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jkanive jkanive merged commit d9c0f3d into bigcommerce:master Jul 13, 2021
@jkanive jkanive deleted the MERC-7797-1 branch July 13, 2021 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants