ampproject / amp-toolbox-php

AMP Optimizer PHP library
Apache License 2.0
73 stars 24 forks source link

Investigate moving some extension scripts to the end of <body> #327

Open schlessera opened 3 years ago

schlessera commented 3 years ago

Some extension scripts are only needed to power the interactivity for the user and are not necessary during the initial rendering process.

Moving these scripts to the end of the <body> tag instead of having them in the <head> would thus likely improve LCP.

We should investigate which extensions can safely be moved, and whether it is always a net win for CWV.

See this comment by @westonruter where he already experimented with this approach with promising results: https://github.com/ampproject/amp-wp/issues/6182#issuecomment-867333175

schlessera commented 3 years ago

@ediamin As a start, it would be great to have a configuration value for the AutoExtensions transformer that takes an array of extension names for which to move the corresponding extension scripts to the end of the <body> tag.

With such a configuration value, we'll then be able to write a small custom plugin to configure this via URL arguments for easy experimentation.

westonruter commented 3 years ago

Here's a prototype transformer for moving non-critical scripts to the end of the body: https://gist.github.com/westonruter/e45cd802a75c7deb80a2a1639b4cea7c

westonruter commented 3 years ago

In the context of the AMP-WP plugin, this is something we may want to enable by default if we're not targeting AMP validity (cf. https://github.com/ampproject/amp-wp/pull/6546).

schlessera commented 3 years ago

@westonruter Was that ever discussed with the AMP team to allow for moving AMP scripts to the body? I think that should be valid, given it can improve performance AND it is not in fact custom JS.

westonruter commented 3 years ago

Yes, some planning has gone into it but no amphtml issue has been created for it yet. It's not as simple as just making it valid as currently the runtime, validator, and optimizer are only expecting scripts in the head. cc @rcebulko @jridgewell @erwinmombay

schlessera commented 3 years ago

I'll look into supporting this in the PHP validator and optimizer to see what changes would be needed. I don't know enough about the runtime though to go through that as well.