Skip to content

Twenty Seventeen's use of uniqid() breaks post-processor response cache #1239

@westonruter

Description

@westonruter

As a site admin running AMP on my WordPress site, I should be able to know when elements cannot be cached and I should be able to cache unique elements (via usage of uniqid() for example) when a WP theme or plugin generates this.

  • AC1: Using TwentySeventeen and its uniqid method break caching as described below. The plugin should automatically catch when a theme (such as this one) breaks caching, reporting that it cannot be fixed automatically.
  • AC2: Propose patch to Twenty Seventeen to use an alternative to use a counter or something more stable than uniqid().
  • AC3: Introduce new filter to apply to buffered content which can be used (sparingly) to fixup content that would otherwise break response caching.

Twenty Seventeen uses uniqid() to generate IDs for HTML elements:

https://github.com/WordPress/wordpress-develop/blob/8e96abbcc2214c3566b814619c4b038d59ad77d9/src/wp-content/themes/twentyseventeen/searchform.php#L13

https://github.com/WordPress/wordpress-develop/blob/8e96abbcc2214c3566b814619c4b038d59ad77d9/src/wp-content/themes/twentyseventeen/inc/icon-functions.php#L77-L94

Unfortunately this causes the response to differ for each request and causes a cache MISS for the plugin's AMP response cache. The result is that the post-processor will always be invoked on a site with Twenty Seventeen, and the object cache will get polluted with values that never get used.

I'm not sure if there is something we can do to fix this. It would have been preferable if Twenty Seventeen had instead introduced a unique-ID generator like:

function twentyseventeen_uniqid() {
    static $id = 0;
    return ++$id;
}

As this would mean the IDs would be unique within a response, but they would be repeated across multiple responses.

Metadata

Metadata

Labels

BugSomething isn't workingRelease

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions