Problem/Motivation
#2559011: Ensure form tokens are marked max-age=0 is the last blocker for #2429617: Make D8 2x as fast: Dynamic Page Cache: context-dependent page caching (for *all* users!). It was split off from #2526472: Ensure forms are marked max-age=0, but have a way to opt-in to being cacheable, and uses a more granular, more precise approach to ensure SmartCache doesn't cache forms that should not be cached.
So, I tested #2429617-339: Make D8 2x as fast: Dynamic Page Cache: context-dependent page caching (for *all* users!) + #2559011-13: Ensure form tokens are marked max-age=0 (the RTBC patch there), to see if SmartCache would still be green. But, unfortunately, the answer is no
: #2560959-4: Testing issue for #2429617 is red.
The reason for the failures is:
public function prepareForm($form_id, &$form, FormStateInterface &$form_state) {
…
// Only update the action if it is not already set.
if (!isset($form['#action'])) {
$form['#action'] = $this->buildFormAction();
}
…
}
protected function buildFormAction() {
…
return $parsed['path'] . ($parsed['query'] ? ('?' . UrlHelper::buildQuery($parsed['query'])) : '');
}
In other words: any form that doesn't have $form['#action'] set, gets that generated automatically, based on the URL's path and all query arguments.
Proposed resolution
When FormBuilder automatically generates $form['#action'], also associate the 'url.path' and 'url.query_args' cache contexts.
Remaining tasks
Review.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 2561775-5.patch | 16.86 KB | wim leers |
Comments
Comment #2
wim leersThis implements the proposed solution. #2560959-10: Testing issue for #2429617 should prove that this patch together with SmartCache will be green.
A bunch of the changes here have been split off to #2561757: Follow-up for #2443457: improve \Drupal\block\Tests\Views\DisplayBlockTest::testBlockEmptyRendering(). That should be able to land very easily (it makes that test insensitive to cacheability changes in forms) and make this patch a lot smaller.
Comment #4
wim leersRebased.
Comment #5
catchIsn't the bug itself a duplicate of #2504139: Blocks containing a form include the form action in the cache, so they always submit to the first URL the form was viewed at?
Comment #6
wim leersFeedback from @Moshe and @tim.plunkett in chat:
Done. Went with Tim's concrete implementation suggestion because he's a Form system maintainer.
Comment #7
wim leers#5: interesting, yes, it is! But this one goes for a much simpler solution: it just adds the missing cache contexts.
That other issue goes further, it ensures that blocks with forms are cacheable across pages.
So IMO, this is a better first step: correctness. The other issue is a next step: improved cache hit ratio.
Comment #8
wim leersFrom IRC:
Hence I think this issue is effectively superseded by #2504139. I've worked on getting that to green, starting at #2504139-81: Blocks containing a form include the form action in the cache, so they always submit to the first URL the form was viewed at, and if all is well, #2504139-84: Blocks containing a form include the form action in the cache, so they always submit to the first URL the form was viewed at should be green.
Comment #9
wim leers