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

Migrate to AMP Optimizer 2.0 #235

Merged
merged 1 commit into from
Jan 31, 2020
Merged

Migrate to AMP Optimizer 2.0 #235

merged 1 commit into from
Jan 31, 2020

Conversation

sebastianbenz
Copy link
Collaborator

  • replace amphtml-autoscript with optimizer built-in functionality
  • change template tags from <% to [% to avoid conflicts with the html
    parsers
  • move AMP Optimizer invocation to built-time instead of runtime
  • let AMP Optimizer inject AMP mandatory tags

* replace amphtml-autoscript with optimizer built-in functionality
* change template tags from <% to [% to avoid conflicts with the html
parsers
* move AMP Optimizer invocation to built-time instead of runtime
* let AMP Optimizer inject AMP mandatory tags
@morsssss
Copy link
Collaborator

Thanks for doing this @sebastianbenz! The code lgtm. I'm especially glad to see you were able to get rid of the part where we override response.send().

I'm wondering about the need to replace template delimiters. Why is this the case?

I'm sorry I haven't had a chance yet to pull down the code and just try it out to make sure it works for me as well as it does for you. Soon!

@morsssss
Copy link
Collaborator

Ok. I tried it out. It works!

I still have the question: why do we need to use different delimiters?

Also, I did find one difference: with the new autoscript includer, one extraneous script gets included on product details page:

<script async src="https://cdn.ampproject.org/v0/amp-lightbox-gallery-0.1.js" custom-element="amp-lightbox-gallery">

@sebastianbenz
Copy link
Collaborator Author

Thanks for checking!

why do we need to use different delimiters?

The old delimiters were using <% .... %> which in looks like html tags and threw off the parser.

Also, I did find one difference: with the new autoscript includer, one extraneous script gets included on product details page:

<script async src="https://cdn.ampproject.org/v0/amp-lightbox-gallery-0.1.js" custom-element="amp-lightbox-gallery">

Interesting! Will investigate!

@morsssss
Copy link
Collaborator

You mean, they threw off the autoscriptinclusion parser that looks for AMP components in HTML tags?

If so, I'd suggest changing the autoscriptinclusion parser, if that's possible. Because otherwise you risk breaking any site that uses the relatively common <% ... %> delimiters 😎

sebastianbenz added a commit to ampproject/amp-toolbox that referenced this pull request Jan 28, 2020
@sebastianbenz
Copy link
Collaborator Author

If so, I'd suggest changing the autoscriptinclusion parser, if that's possible. Because otherwise you risk breaking any site that uses the relatively common <% ... %> delimiters 😎

AMP Optimizer requires an HTML parser, so that's a difficult request. Running AMP Optimizer on templates is very clever, but requires precaution...

@demianrenzulli
Copy link
Contributor

demianrenzulli commented Jan 30, 2020

Hi folks,

Sorry for jumping late into this. Here are my 2 cents:

I think @morsssss is right in that angle brackets might be a common way of writing delimiters in many templates engines. In the case of Mustache, the choice we made <% %>, is the one that appears as example in the official docs. Assuming developers would be inclined to use them as their first option in many cases.

Based on previous comments, it seems like the options are:

  • Not using AMP Optimizer 2.0: The benefits of using it clearly outweigh the benefits of using angle brackets, so this is definitely not an option.
  • Modifying AMP Optimizer to ignore delimiters written with angle brackets: From @sebastianbenz comments this is hard and not likely to happen soon.
  • Would it be possible to run Optimizer, after the template engine runs and produces its output? This doesn't seem feasible either in its current state.

Since none of the previous options are good, what we can do is:

  • Adding a note in AMP Optimizer README to warn developers around this.
  • Include a paragraph in our upcoming blog post talking about templating engines, explaining this limitation.
  • Making sure that if developers end up using angle brackets as well, Optimizer error message is clear enough so they can make the required changes.

Not sure if I'm missing something! These are just my thoughts.
Cheers.

@morsssss
Copy link
Collaborator

I'm torn here. We want to use AMP Optimizer. But I'm also concerned that we need to modify a common templating pattern (even if our templating scheme, as @sebastianbenz points out, is a little unusual).

I'd defer to what others think. @pbakaus , @patrickkettner, @alankent, @fstanis , @antoinebisch , would love to get your opinion here!

@alankent
Copy link

Do I have an opinion? Usually!!! ;-)

I don't really care using [% vs <%. I don't think that is a big deal.

What worries me more is running the optimizer over the template rather than the HTML output by the template. It's kinda cute, kinda dangerous. Who knows when something else will break in the future. The optimizer is not being given valid HTML content - it is being given a text templates that output HTML. E.g. class names or attributes may be substituted in by the templating language. The optimizer won't see them, and that feels risky to me. Even if it works today, it could break at any time in the future.

I was not sure why the optimizer could not be run over the final generated page. To me that makes the most sense in terms of ultimately reliability and safety. You should feed HTML markup (not templating language markup) into the optimizer.

That's my PoV anyway.

@morsssss
Copy link
Collaborator

@alankent , it is a little unusual. @sebastianbenz pointed out the same thing! The advantage here is that we can run it at build time for pages that are dynamically generated. But, yes, there are disadvantages.

@sebastianbenz
Copy link
Collaborator Author

It's kinda cute, kinda dangerous.

Well said Alan.

The more I think about this, the more I think that this approach is not a good idea. Validating templates won't give you 100% confidence that the final pages are valid AMP.

We definitely shouldn't be recommending this approach as it also seriously limits the expressiveness of your templates.

For example, this common use case won't work:

<amp-img src="<% imgSrc %>" height="<% imgHeight %>" width="<% imgWidth %>"></amp-img>
` ``

@morsssss
Copy link
Collaborator

Well, we did use templating like this:

<amp-img option="<%ColorName%>" src="<%& Photo%>">

Anyway, @alankent , the current site does in fact run the optimizer in the server. As far as I can tell it does run the optimizer over the final rendered pages. But now the optimizer has new functionality - it replaces the amphtml-autoscript module used in the current site. It automatically adds the scripts required by AMP components. So @sebastianbenz 's fork uses that functionality and thus it runs the optimizer at build time. This is nice for a few reasons... among which, there's no longer a need to cache optimized pages in the server.

I'm not really here to defend the current build system, which has a couple of things we'd want to change if the site got more complex. fileinclude was already starting to strain, for example. My question is, is it a problem to disallow the <% and %> templates?

So far, @alankent doesn't think so. If no one else who wasn't involved in the project thinks this is a problem, I'm happy to approve this PR as is!

I wish I had time to go back and rework this build process, but at this point I think it's best to get this out there and move on to samples that use <amp-script>! If someone else feels strongly that we shouldn't, let me know. I remember some other prominent AMP projects that suffered from build processes that didn't scale... ampstart, anyone? Not saying this is ideal, just saying that the templating process isn't the main idea here.

@antoinebisch
Copy link
Collaborator

antoinebisch commented Jan 31, 2020 via email

@sebastianbenz
Copy link
Collaborator Author

@morsssss <amp-img option="<%ColorName%>" src="<%& Photo%>"> is no problem because the validator doesn't restrict the values of option or src. However, it will fail for <amp-img height="<% imgHeight %>" ... as height needs to be an integer.

@morsssss
Copy link
Collaborator

@sebastianbenz, yes, exactly. We never faced this issue, but, of course, others who adopt our approach would face it eventually.

To really do this right, I fear a developer would have to run the optimizer during the build process and once again in the server. This worries me. On the other hand, our current scheme worries me more.

So since no one else is really concerned about the loss of the angle-bracket template delimiters, I'm going to merge this PR. I agree this is the best compromise for now. And if we ever revisit this project, we'll see if we can create a more sustainable build process.

I finally tried running the optimizer on the templates, and I was surprised to find that the parser didn't barf. It simply parsed the delimiters as HTML tags the best way that it could, and ultimately the optimizer transformed things like this:

<h2>Reviews (<%ReviewCount%>)</h2>

to things like this:

<h2>Reviews (<%reviewcount%>)</%reviewcount%></h2>

which leads to unexpected - and hard to debug - results in the browser.

Even if AMP CAMP's templating and build is not ideal, I don't think this is a desirable outcome. The optimizer has to just work in predictable ways, no matter what silly things users might do! So I've made an issue to tackle this, either by issuing a warning in such cases, or by preprocessing/postprocessing HTML to allow this approach.

Also, as time permits, would love to see this optimizer issue dealt with. The real world is full of weirdos like us who use your product in ways that you would not have expected 😎

Thanks to everyone for this excellent discussion!

@morsssss morsssss merged commit 2096ca5 into master Jan 31, 2020
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.

5 participants