Problem/Motivation

If both modules big_pipe and prefetch_cache are turned on, then the prefetch caching would not work and an exception will be thrown, that the container is being serialized.

Prefetch cache caches the response object, which in the case of big pipe is BigPipeResponse. The class references some services, but doesn't include the DependencySerializationTrait. If we add the trait to the class, then it will be serialized properly, but now the response with the prefetch cache ID will be streamed. Thinking further it doesn't really make to use big pipe on prefetch cache requests, but there is no way for dynamically disabling big pipe on requests.

Proposed resolution

Make it possible through e.g. a query parameter to disable big pipe on requests.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

hchonov created an issue. See original summary.

hchonov’s picture

hchonov’s picture

Issue summary: View changes
hchonov’s picture

Status: Active » Needs review
StatusFileSize
new1.33 KB
hchonov’s picture

Issue tags: +DrupalEurope
tstoeckler’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

@hchonov mentioned this needs a test, so marking accordingly.

The code in itself looks good to me, but I'm not in a position to give a proper thumbs-up/thumbs-down on this.

mstiem’s picture

StatusFileSize
new10.43 KB
new11.76 KB

I made some adaptions for the test BigPipeStrategyTest::testProcessPlaceholders here you will find the both patch files.
The one include the code from comment 4 and the other only the test class changes.

wim leers’s picture

Title: BigPipe breaks Prefetch Cache » Prefetch Cache contrib module + BigPipe: prefetching
Category: Bug report » Feature request
Status: Needs work » Postponed (maintainer needs more info)

Note that https://www.drupal.org/project/big_pipe_demo already includes the ability to disable BigPipe on a per-request basis. See https://cgit.drupalcode.org/big_pipe_demo/tree/src/Render/Placeholder/Bi... and https://cgit.drupalcode.org/big_pipe_demo/tree/src/Plugin/Block/BigPipeO....


I'm not sure I fully agree with

Thinking further it doesn't really make to use big pipe on prefetch cache requests

(I think you meant to say "doesn't really make sense"?)

It indeed does not make sense to use JS-powered placeholder replacement. But I don't think there's a reason to do not have the server-side (no JS required) placeholder replacement? You don't gain anything, but you also don't lose anything.

I'm very hesitant to add this ability because before you know it, people are sharing links with BigPipe disabled, which A) is bad for performance, B) makes for atrocious URLs. Sensible URLs are holy on the web.

Looking at https://cgit.drupalcode.org/prefetch_cache/tree/js/prefetch_cache_proces..., I see two possible solutions:

  1. Disable BigPipe JS-based placeholder replacement by send a BigPipe no-JS cookie in that AJAX request!
  2. Disable BigPipe placeholder replacement altogether by decorating the placeholder_strategy.big_pipe service: check if the prefetch_cache_request=1 URL query arg is present, and if so, return early in ::processPlaceholders().

Finally: this is definitely not a bug, but a feature request, or task.

hchonov’s picture

I'm very hesitant to add this ability because before you know it, people are sharing links with BigPipe disabled, which A) is bad for performance, B) makes for atrocious URLs. Sensible URLs are holy on the web.

PrefetchCache doesn't put an url with that query parameter in the address bar. The only way this to happen will be if an user manually sets the query parameter.

It indeed does not make sense to use JS-powered placeholder replacement. But I don't think there's a reason to do not have the server-side (no JS required) placeholder replacement? You don't gain anything, but you also don't lose anything.

It depends on how you see it :). Our main idea was that having prefetch cache on will retrieve the whole page without any additional loads, but with BigPipe on this will not happen even if there is enough time to process the response completely.

Additionally having a time between the requests might lead to mixed up data being shown to the user - old one served by PrefetchCache and newer one served by BigPipe, in which case I would say it is better to serve the whole page by PrefetchCache only.

Disable BigPipe JS-based placeholder replacement by send a BigPipe no-JS cookie in that AJAX request!

Unfortunatelly we cannot trigger an AJAX request with custom set of cookies, but the document ones are being used by the AJAX request. In order to use the BigPipe no-JS cookie we have to set it on the document, which makes it available to all AJAX requests and not only to the prefetch cache requests. Do you think this is fine?

Disable BigPipe placeholder replacement altogether by decorating the placeholder_strategy.big_pipe service: check if the prefetch_cache_request=1 URL query arg is present, and if so, return early in ::processPlaceholders().

I guess this would be the best choice if you are totally against disabling big pipe through a query parameter. However this forces modules doing this to implement their own test coverage for ensuring BigPipe is being properly disabled. I personally would like to have such tests in the BigPipe module itself.

wim leers’s picture

The only way this to happen will be if an user manually sets the query parameter.

Search engines will discover this URL in the HTML body, and hence it may end up in search results…

Our main idea was that having prefetch cache on will retrieve the whole page without any additional loads, but with BigPipe on this will not happen even if there is enough time to process the response completely.

What do you mean? BigPipe also does not perform additional requests. That's a key reason it actually accelerates things, even on high-latency connections!

Additionally having a time between the requests might lead to mixed up data being shown to the user - old one served by PrefetchCache and newer one served by BigPipe, in which case I would say it is better to serve the whole page by PrefetchCache only.

Can you clarify this? I don't understand this at all :(

Unfortunatelly we cannot trigger an AJAX request with custom set of cookies, but the document ones are being used by the AJAX request. In order to use the BigPipe no-JS cookie we have to set it on the document, which makes it available to all AJAX requests and not only to the prefetch cache requests. Do you think this is fine?

Ah … I wasn't sure if this would be possible using jQuery.ajax() (which is what your JS code uses). I guess it isn't possible then!

I guess this would be the best choice if you are totally against disabling big pipe through a query parameter. However this forces modules doing this to implement their own test coverage for ensuring BigPipe is being properly disabled. I personally would like to have such tests in the BigPipe module itself.

I would be fine adding it to BigPipe if there are at least 2, preferably 3 use cases for it. So far, there's only one: this module. That's why for now I am opposed to adding this to Drupal core.

P.S.: For similar reasons, https://www.drupal.org/project/big_pipe_sessionless is a separate contrib module!

hchonov’s picture

Search engines will discover this URL in the HTML body, and hence it may end up in search results…

An URL with that query parameter isn't contained in the HTML body neither :).

What do you mean? BigPipe also does not perform additional requests. That's a key reason it actually accelerates things, even on high-latency connections!

Oh right, I am sorry, I had something completely different in my mind.

Can you clarify this? I don't understand this at all :(

You don't understand it, because it doesn't make any sense :D. I am sorry for the confusion.

I would be fine adding it to BigPipe if there are at least 2, preferably 3 use cases for it. So far, there's only one: this module. That's why for now I am opposed to adding this to Drupal core.

Fair enough :). Then I'll give a try to the approach with the decorated service .. unless you know how we can process a BigPipe streamed response in PrefetchCache? I am not sure if this might work.

wim leers’s picture

You don't understand it, because it doesn't make any sense :D. I am sorry for the confusion.

Oh, hah! :D I'm glad we're on the same page then! (Pun intended :P)

unless you know how we can process a BigPipe streamed response in PrefetchCache?

I … did not realize you were implementing an alternative/extended variant of core's Page Cache! Why? Why isn't it enough to have JS prefetch those next pages, why do you also need them to be cached?

hchonov’s picture

I … did not realize you were implementing an alternative/extended variant of core's Page Cache! Why? Why isn't it enough to have JS prefetch those next pages, why do you also need them to be cached?

Well it is mostly because of our edit page, which is pretty heavy currently. It basically contains a lot of inline entity forms and therefore the FormBuilder and then the Renderer need quite some time to process it. So when a user goes to the view page, we start with the preparation of the edit page and when the user clicks on the edit link, then we simply return the already prepared response from the PrefetchCache. What we do isn't really caching, but prefetching and we do it also for resources which are uncacheable.

In order to make it much more seamless for the user, I've been considering not only to cache the response on the server side, but to return it and store it on the client side and simply exchange the current page through JS when the edit link is being clicked on.

I hope this makes the idea of the module more understandable :).

wim leers’s picture

I hope this makes the idea of the module more understandable :).

It does! Thanks :)

Well it is mostly because of our edit page, which is pretty heavy currently. It basically contains a lot of inline entity forms and therefore the FormBuilder and then the Renderer need quite some time to process it.

Why isn't Dynamic Page Cache enough then? I'm willing to bet that DPC says it's an uncacheable response?

What we do isn't really caching, but prefetching and we do it also for resources which are uncacheable.

Right!

I've been considering not only to cache the response on the server side, but to return it and store it on the client side and simply exchange the current page through JS when the edit link is being clicked on.

Idea: just request that next page in a hidden <iframe> to ensure that everything is already primed in the browser anyway? The only difference would be that the server would need to re-generate that HTML response; nothing would change on the client compared to the current implementation of the prefetch_cache module.

hchonov’s picture

Idea: just request that next page in a hidden

to ensure that everything is already primed in the browser anyway?
I guess that a hidden makes sense. Thanks.
The only difference would be that the server would need to re-generate that HTML response;
I don't understand that part. Could you elaborate on that?
wim leers’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

#2994355: Prefetch cache doesn't work when BigPipe is enabled was fixed! That means we can close this.

I don't understand that part. Could you elaborate on that?

I mean that if the <iframe> requested it while the user was still on the current page, that'd only have primed the browser's caches for CSS/JS/images/videos/… If there's no server-side special caching going on, the HTML response would still need to be generated on the server and re-retrieved by the client (unless you work with e.g. a limited time max age, that might work!), but then at least the browser will still be able to render that next page much faster since all other assets are already cached. This would remove the need for any and all special server-side logic; it'd reduce the module to just a little bit of JS :)

hchonov’s picture

I mean that if the

requested it while the user was still on the current page, that'd only have primed the browser's caches for CSS/JS/images/videos/… If there's no server-side special caching going on, the HTML response would still need to be generated on the server and re-retrieved by the client (unless you work with e.g. a limited time max age, that might work!), but then at least the browser will still be able to render that next page much faster since all other assets are already cached. This would remove the need for any and all special server-side logic; it'd reduce the module to just a little bit of JS :)
Now I understand what you mean. However the case I am pursuing is to make the delivery of the page itself faster, as there is the bottleneck in our application. But what you are suggesting is also an interesting idea for a quite different use case. I've opened a feature request for that - #3001236: Option to prefetch only assets :). Thank you for your feedback!