Problem summary
AJAX requests are always made using POST, which disables render caching (https://www.drupal.org/node/956186).
Further (reason behind creating separate issue) - AJAX requests in form submit, form element validate, ajax at form element levels use the same URL as the page (partially covered in https://www.drupal.org/node/2504115).
When the AJAX request is made - first because of POST, second because of URL being the same as current page, it processes every region, block on the page including hook_ENTITY_TYPE_view, hook_form_alter (for all the forms) and anything inside as even render cache is disabled.
In such calls, we usually want only part of the form to be processed and updated and don't want anything except the form to be processed. This results into huge impact to performance, especially if there is dynamic or processed content added to node in hook_node_view or for instance there are related content blocks on the page, hook_node_view is called for each node rendered on the page (same for any other entity if used)
Proposed solution
- Allow AJAX callbacks to use different URL than current URL.
- For POST (or any) requests, allow setting a configuration in routes.yml which, if set true, will not process any region/block on the page and only the required form is processed.
Comments
Comment #2
dawehnerThere is also #2503429: [PP-*] Allow both AJAX and non-AJAX forms to POST to dedicated URLs which seems to be basically the same issue.
Comment #3
nikunjkotechahmmm, I went through all the open issues in ajax system, missed going through ones in forms :(
I would still like to have this one separate, reason is the place where I want it to be supported.
Imagine an address form on product page to check if delivery is possible, you select a country and do an ajax call to populate states based on the selected country but that ajax call loads the whole page even bypassing the render cache.
I wish if we can convert this:
into:
And something similar for #element_validate
Comment #4
nikunjkotechaComment #5
nikunjkotechaComment #6
nikunjkotechaAt-least form form_item[#ajax], [#ajax][url] is good option, still checking how we can return rendered form here or update form easily.
Comment #17
catchMarking this duplicate of #2504115: AJAX forms should submit to $form['#action'] instead of <current>.