Kirby 4: Error on save

Asked also on Discord, but I really must find a solution: Every time I want to save in Kirby 4 and am on a page that contains a little more content, I am redirected to another page and it just says.

{"status":"error","message":"Unauthenticated","code":401,"exception":"Kirby\\Exception\\AuthException","key":"error.auth","file":"\/pagebuilder-K4.1\/kirby\/config\/api\/authentication.php","line":14,"details":[],"route":"pages\/([a-zA-Z0-9\\.\\-_%= \\+\\@\\(\\)]+)"}

Does anybody know what this could be and how to avoid? The redirect goes to:

http://localhost:8888/pagebuilder-K4.1/api/pages/PAGENAME

PHP Max Limits are all on top. RAM, Execution time, Upload, Post …

I have also made a video now, this appears for 0.001 seconds before the redirect to the API error

Guess that is an understatement? I have no idea, but if you have a lot of custom blocks, maybe there is an error somewhere, the error message points to calling a property of an object that is not defined.

It’s actually not even an exaggeration. On the page there is a layout, a block, a structure field with 40 entries. But it’s good to know that it can also be caused by an undefined object.

Would it also be possible that it is because a certain field type is no longer available or something similar?

What I find a bit strange is the redirection to the API URL, I’ve never seen that before. Was this also the case in Kirby 3 or has something been changed here perhaps?

Does this affect only a single page? Does it really have to do with the amount of content? What’s the difference between this page/these pages and other pages where it doesn’t happen?

I can’t tell from here why that is happening. Maybe make a copy if your project and try to rule things out.

I feel like the error message (Cannot read properties of undefined (reading 'json')) doesn’t necessarily relate to page content. That error is thrown every time the panel doesn’t return a json response, and therefore the panel is redirected.

The details of this are in kirby/panel/src/api/request.js at main · getkirby/kirby · GitHub :

const { response } = await responder(request, await fetch(request));
let data = response.json;

The code expects responder to return a promise which resolves to an object with a response property, the responders promise however sometimes resolves to just false (kirby/panel/src/panel/request.js at main · getkirby/kirby · GitHub).

That means this is essentially run:

const { response } = false;
let data = response.json;

Therefore, response is undefined and you can’t read json on undefined.

You normally don’t notice the error because you’re immediately redirected. However, you’re redirected to the api request url, but without a csrf token (which is normally injected in the ajax requests as header), so you aren’t seeing the actual error, but only an “Unauthenticated” error.

And, finally, because of the immediate redirect, the body of the original response which caused it isn’t even downloaded (you don’t see it in the dev tools network tab), therefore the only way to actually get a chance to see the error is to replay the request from the console:

  1. in chrome’s dev tools network tab, activate “Preserve Log”
  2. Click on save in the panel page that causes the error, wait to be redirected
  3. In the network tab, search for the API request that caused the error (it should be red and immediately before the redirect).
  4. right click on it and choose “Copy” > “Copy as fetch”.
  5. In the JS console, paste the fetch command. Don’t run it immediately.
  6. to the end of the fetch command add a .then(r => r.text()).then(r => console.log(r));
  7. Run it

You should now see the actual error in the console.

Many thanks for your detailed instructions. I have done all this, but could not find anything special. The error message that comes up is the same as above.

When I copy the actual fetch, everything looks fine so far. Then I send it and after about 10 -20 seconds I get a 500 error and actually a 401 error at the same time.

I have tested it on various pages, it definitely has to do with the number and content of the blocks. Initially everything is ok, the more often I duplicate a block with a “lot” of content, the higher the probability of the error. I know this is difficult to debug.