Skip to content

Automatically set the ajax_load request parameter. - #4188

Closed
thet wants to merge 2 commits into
6.1.xfrom
auto-ajax-load--61
Closed

Automatically set the ajax_load request parameter.#4188
thet wants to merge 2 commits into
6.1.xfrom
auto-ajax-load--61

Conversation

@thet

@thet thet commented Jun 3, 2025

Copy link
Copy Markdown
Member

Related:
#4188
plone/plone.base#85
plone/plonetheme.barceloneta#404
plone/plone.app.portlets#203

Plone 6.1: #4188 (this one)
Plone 6.2: #4169

Zope maintains the "HTTP_X_REQUESTED_WITH" request header. If this is set to "XMLHttpRequest", we have an AJAX request. In this case the ajax_load parameter is set to 1 directly on the request object.

This should make use for the ajax_main_template for any AJAX request, regardless if the ajax_load parameter was manually set or not and potentially speed up Plone Classic UI rendering.

@mister-roboto

Copy link
Copy Markdown

@thet thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

@thet
thet marked this pull request as draft June 3, 2025 16:25
@thet
thet force-pushed the auto-ajax-load--61 branch from 9fb445c to 85b87cf Compare June 3, 2025 16:28
@thet

thet commented Jun 3, 2025

Copy link
Copy Markdown
Member Author

@jenkins-plone-org please run jobs

Zope maintains the "HTTP_X_REQUESTED_WITH" request header. If this is set to
"XMLHttpRequest", we have an AJAX request. In this case the ajax_load parameter
is set to True directly on the request object.
If the ajax_load parameter was already set to a true-ish or false-ish value, it
is not overwritten.

This should make use for the ajax_main_template for any AJAX request,
regardless if the ajax_load parameter was manually set or not and potentially
speed up Plone Classic UI rendering.
@thet
thet force-pushed the auto-ajax-load--61 branch from f111e3d to 17c9e54 Compare June 11, 2025 09:29
@mauritsvanrees

Copy link
Copy Markdown
Member

The related PRs seem fine, and could be merged and used in 6.1, even without the current PR, in my opinion.

But I have a doubt about this one for Plone 6.1. You already saw some unexpected problem in plone.app.portlets caused by this. I wonder which other problems lurk in custom code where someone may do an XMLHTTPRequest from Javascript and expect this to contain the full page. If this assumption breaks in 6.2, I would say it is not so bad, and can be fixed by appending ?ajax_load=0 to the url. But for a 6.1 bug fix release I am not so sure.

What do others think?

checkPermission python:context.restrictedTraverse('portal_membership').checkPermission;
ajax_include_head python:request.get('ajax_include_head', False);
ajax_load python:False;"
ajax_load python:True;"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why this ajax_main_template has ajax_load=False in the first place. This variable seems really obscure defined like this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

But I think it's maybe needed by other templates and was incorrectly defined due to a copy paste error.

I did now looked it up and saw:

  • It was added here:‌ 4f58ba6
  • The copy error changing from ajax_load python:True to ajax_load python:False happened in this commit at some Barceloneta LTS syncing:
    954942a

Now it makes sense.

But no big deal apparently that it was defined incorrectly, because everything was working just fine.

Comment on lines +86 to +91
if (
request.getHeader("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
and not request.get("ajax_load", MARKER) is not MARKER
):
# Directly set on the request object
request.set("ajax_load", True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like this approach.
You are checking the request, fiddling with the request parameters, and then checking the request again.

IMO this is too complicated.
There should be some come somewhere that has a method is_ajax_load (or whatever other name) that is the unique source of truth about the fact that we are loading stuff through AJAX.

Ideally, this should be a method in a view (plone_context_state?) so that it is easily configurable (e.g. you might want to always load some portal type as if it was called as AJAX, add a check for the referrer or whatever).

BTW even if you want to have this traverser, the second condition has to be simplified because it contains a double negation.

Something like:

request.get("ajax_load", MARKER) is MARKER

or even:

"ajax_load" not in request

is preferable.

@thet thet Jun 16, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL, and not request.get("ajax_load", MARKER) is not MARKER .... yes, i'll take your last suggestion - this is much better 😄

@thet thet Jun 16, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the other suggestion. Sounds good, but I always saw the ajax_load request parameter as the source of truth to set something in ajax mode. In the end, the Diazo template uses it. In Plone 5 and in Plone 6 since since plone/plonetheme.barceloneta#404
Also some Plone links are configured with ajax_load, IIRC.

For that, we need to set it early, I think - therefore the IBeforeTraverse event subscriber here.

Also, we could disable Diazo via another http header here. For example, if a registry entry for disabling diazo is set to True , we could set the header X-Theme-Disabled and disable Diazo that way easily for everyone. ... for a future addition.

@thet

thet commented Jun 17, 2025

Copy link
Copy Markdown
Member Author

@ale-rt I have addressed your comments in #4169

I agree with @mauritsvanrees this should probably not be merged into Plone 6.1 but 6.2 instead.

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.

4 participants