Jump to content

[SOLVED] Error 500 when adding hook to Pagefile::url or Pagefile::filename


monollonom
 Share

Recommended Posts

Hi,

I can't really explain why (I have a hard time finding valuable error logs) but when I try to hook on either Pagefile::url or Pagefile::filename, it results in 500 errors. The only log I manage to have, sometimes, is a "Server gone away".

One thing to note: I don't have the error when hooking onto Pagefile::httpUrl, and the only difference between this method and the two others is this condition on line 862, calling Wire::__call. Maybe this is where the culprit is ? Does it go into a loop of some sort it can't escape ?

I have the same issue both on my client's server and locally.

Context: I'm trying to change (in ready.php) the return of Pagefile::url to use a subdomain pointing to site/assets/files.

Does it ring a bell to someone ? Many thanks in advance !

Link to comment
Share on other sites

3 hours ago, monollonom said:

Does it go into a loop of some sort it can't escape ?

I think this might be happening! I remember this happening to me when working with the webp hooks. 

https://processwire.com/blog/posts/webp-images-and-more/#webp-image-strategies-in-processwire

$wire->addHookAfter('Pageimage::url', function($event) {
  static $n = 0;
  if(++$n === 1) $event->return = $event->object->webp()->url();
  $n--;
}); 

Word from Ryan on that post:

Quote

The reason we have to keep that static $n variable in there is that the $image->webp->url() method actually calls the $image->url() method too, so if we didn’t have that static $n variable keeping track of recursion depth, then we’d end up with an infinite loop!

 

  • Thanks 1
Link to comment
Share on other sites

It...worked.

Thank you so much @elabx, I absolutely am going to save this for later!

Now that I am seeing the solution I do understand what is going on, but shouldn't something like this (the safe-guard) be built-in when hooks are facing this recursion issue ? Or it should be made clearer maybe in the doc ?

Also it's weird because at first I just tried @markus_blue_tomato snippet, and he doesn't seem to have faced my issue. Is it because he added the hook in the init() function of a module ?

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...