SSR template rendering #3 - re-format requests#18189
SSR template rendering #3 - re-format requests#18189dreamofabear merged 12 commits intoampproject:masterfrom
Conversation
|
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. |
|
Please hold, this doesn't work. I'll ping this thread again when it's ready. Sorry Will! |
| dev().assertString(this.xhrAction_), this.method_); | ||
| setupInit(request.fetchOpt); | ||
| setupAMPCors(this.win_, request.xhrUrl, request.fetchOpt); | ||
| request.fetchOpt = setupInit(request.fetchOpt); |
There was a problem hiding this comment.
So the setupXxx function actually has a side effect (it mutates the passed-in argument), so maybe it should be left in the previous state where the return value is simply ignored... When I see a function that takes an input and returns an ouput, I often assume that it's side effect-free.
There was a problem hiding this comment.
Hmmm problem is we have to use setupInit this way, because it doesn't mutate passed-in parameters.
https://github.com/ampproject/amphtml/blob/master/src/utils/xhr-utils.js#L250
I used others this way for consistency, but can definitely change them if that's better.
There was a problem hiding this comment.
I looked at that code and it definitely looks like to me it's mutating the passed-in opt_init. It's just saving another init reference to opt_init and mutating through the init reference, but it's still mutating the same object in memory.
I mean, you didn't modify those setupXxx functions in this PR, and the existing code on master is not using the return values, so the existing code must be relying on these functions to modify the passed-in init in place.
There was a problem hiding this comment.
Yea, the old code was just wrong. Thanks for fixing this.
There was a problem hiding this comment.
Hmmm I wasn't so sure about the correctness previously 😟
I remember it doesn't work until I made it this way, but it could because there wan't setupInput before.
And actually there are using them like this way in the other reference (from amp-list):
https://github.com/ampproject/amphtml/blob/master/src/service/xhr-impl.js#L113
| dev().assertString(this.xhrAction_), this.method_); | ||
| setupInit(request.fetchOpt); | ||
| setupAMPCors(this.win_, request.xhrUrl, request.fetchOpt); | ||
| request.fetchOpt = setupInit(request.fetchOpt); |
There was a problem hiding this comment.
I looked at that code and it definitely looks like to me it's mutating the passed-in opt_init. It's just saving another init reference to opt_init and mutating through the init reference, but it's still mutating the same object in memory.
I mean, you didn't modify those setupXxx functions in this PR, and the existing code on master is not using the return values, so the existing code must be relying on these functions to modify the passed-in init in place.
src/ssr-template-helper.js
Outdated
| 'successTemplate': { | ||
| const ampComponent = dict({'type': this.sourceComponent_}); | ||
|
|
||
| const successTemplate = (opt_templates && opt_templates['successTemplate']) |
There was a problem hiding this comment.
If you pass in an actual template object to mustacheTemplate (which can probably be better-named, since currently there's mustacheTemplate and opt_templates which is kinda confusing), then you can just do:
const successTemplate = (opt_templates && opt_templates['successTemplate'])
? opt_templates['successTemplate'] : mustacheTemplate;
if (successTemplate) {
...
'payload': successTemplate.innerHTML,
...
}There was a problem hiding this comment.
Hmmm does it really work? mustacheTemplate (or ampListTemplate now) can also be null.
There was a problem hiding this comment.
If mustacheTemplate (or ampListTemplate now) is null, it will fail the if (successTemplate) check, wouldn't it? I'm not sure if you need all those conditionals... seems redundant
There was a problem hiding this comment.
I see what you mean, yes this works, done.
| dev().assertString(this.xhrAction_), this.method_); | ||
| setupInit(request.fetchOpt); | ||
| setupAMPCors(this.win_, request.xhrUrl, request.fetchOpt); | ||
| request.fetchOpt = setupInit(request.fetchOpt); |
There was a problem hiding this comment.
Yea, the old code was just wrong. Thanks for fixing this.
src/ssr-template-helper.js
Outdated
| }, | ||
| 'errorTemplate': { | ||
| 'payload': successTemplate | ||
| ? successTemplate : mustacheTemplate, |
There was a problem hiding this comment.
Nit: successTemplate || mustacheTemplate
There was a problem hiding this comment.
Can't find that code version anymore but seems not necessary anymore, we have a
if (ampFormSuccessTemplate || ampListTemplate) {
out of it.
src/ssr-template-helper.js
Outdated
| if (additionalAttr) { | ||
| Object.keys(opt_attributes).forEach(key => { | ||
| data[key] = opt_attributes[key]; | ||
| ampComponent[key] = opt_attributes[key]; |
There was a problem hiding this comment.
Not your fault but additionalAttr is a bit weird.
This should suffice:
if (opt_attributes) {
Object.assign(ampComponent, opt_attributes);
}There was a problem hiding this comment.
Done. This is nice!
src/ssr-template-helper.js
Outdated
| // serializeToString for a string representation of the dom tree. | ||
| mustacheTemplate = this.xmls_.serializeToString( | ||
| this.templates_.findTemplate(element)); | ||
| mustacheTemplate = template./*REVIEW*/innerHTML; |
There was a problem hiding this comment.
Eh, we can probably make this API clearer. /cc @alabiaga
fetchAndRenderTemplate(element, request, successTemplate, opt_errorTemplate = null, opt_attributes = {})No action needed. :)
There was a problem hiding this comment.
Or, I can make this
fetchAndRenderTemplate(element, request, ampListTemplate, ampFormTemplates = null, opt_attributes = {})
for now which might be slightly clearer(?)
And won't involve too much work.
There was a problem hiding this comment.
It's ok, I'd rather not name the params by its callers.
|
Ideally we need some unit tests to prevent bugs like this in the future... |
Agree. |
|
This pull request introduces 1 alert when merging e7fcecb into 146cde4 - view on LGTM.com new alerts:
Comment posted by LGTM.com |
Codecov Report
@@ Coverage Diff @@
## master #18189 +/- ##
===========================================
- Coverage 85.7% 22.99% -62.71%
===========================================
Files 1270 1270
Lines 121893 121897 +4
===========================================
- Hits 104471 28036 -76435
- Misses 17422 93861 +76439
Continue to review full report at Codecov.
|
bd7763a to
574a931
Compare
|
check cla... |
|
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
|
CLAs look good, thanks! |
|
@choumx |
|
Nice work TC! |
* fix AmpComponent format * lint * fix * add more * add /*review*/ for .innerHTML * comments * comments round 2 * comments round 3 * comments round 4 * revert one more * FIX TYPE
…18189) * fix AmpComponent format * lint * fix * add more * add /*review*/ for .innerHTML * comments * comments round 2 * comments round 3 * comments round 4 * revert one more * FIX TYPE
Reformat the AmpComponent passed to viewer, to make sure ssr-template-helper:
successTemplateorerrorTemplateas null if it doesn't exist, rather than now always returns both templates butpayloadmight be nulladditionalAttrwhich correctly only hasampListAttributesunderAmpComponentwhere makes more sensesingleItemwhich is a boolean to be consistent with normal amp-list and also prevent bugs where different browsers have inconsistent behaviors on whether to return an empty string when the attribute doesn't existThis also fixed a bug where if some template doesn't not exist in amp-form,
xmls_.serializeToStringwill throw an error saying that:'''
Form submission failed: TypeError: Failed to execute 'serializeToString' on 'XMLSerializer': parameter 1 is not of type 'Node'.
'''
TESTED=amp-list with items=".", singleItem=true and amp-form with no templates
I will test more later but want to check in this first to make sure our first bunch of AMP4EMAIL(s) work.