Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

HHV's avatar
Level 1

unexpected return when using storage put

I use the storage->put method to store a json file. This works as desired as follows:

    Storage::disk('public_data')->put('test.json', $jsonData);

However, storage->put ignores all subsequent lines of code and even reloads the page, even if the post request was made asynchronously using ajax, as in my case.

Save file Methode:

public function create_json($request)
{
    $jsonDecode = json_decode($request);
    $jsonData = json_encode($jsonDecode);
    
    Storage::disk('public_data')->put('test.json', $jsonData);

    //this return will not run, because somehow Storage->put returns already
    return "if you read this it was successful";
}

The POST request:

fetch(url, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-CSRF-TOKEN': document.querySelector("#csrf-token").getAttribute('content')
    },
    body: JSON.stringify(data)
}).then(response => {
    //this will never be reached
    console.log(response);
})

I have read the storage documentation several times and could not find a sentence that would explain why this behavior occurs. Why is it that way?

TL;DR: storage->put ignores all subsequent lines of code and causes the frontend to reload. (Both should not be), why?

0 likes
7 replies
Sinnbeck's avatar

Or there is an error with thejson_encode/decode? Does it trigger if you add a dd after those?

    $jsonDecode = json_decode($request);
    $jsonData = json_encode($jsonDecode);
   dd('hit');
HHV's avatar
Level 1

@Sinnbeck Hi thanks for the quick replay! There is no obvious error in the lines above and below. If I comment out the storage put line, the unexpected return does not happen anymore. Therefore I am quite sure that it must have something to do with storage put.

For better comprehension, here is the creation of the storage disk used:

'public_data' => [
    'driver' => 'local',
    'root'   => public_path() . '/data',
],
Sinnbeck's avatar

@HHV What is the response then? You can check in the network tab in dev tools in the browser

HHV's avatar
Level 1

@Sinnbeck I can't see the return value of the request because the page is immediately reloaded in the frontend when storage put is fired. Is there a way to stop the reload in the browser and read the return value first?

Sinnbeck's avatar

@HHV Why is the page reloaded if the request is done with ajax ?

HHV's avatar
Level 1

@Sinnbeck Yes that is part of my question ^^

The javascript used is shown in the question above, as is the PHP method used. Both methods together produce the extremely unusual behavior.

I can't understand how storage->put can cause the page to reload when the request happens via ajax.

HHV's avatar
Level 1

@Sinnbeck I found out why the page reloads despite ajax! I almost went crazy but as so often it now makes sense.

The saved json is included elsewhere in the javascript via an import statement. As a build tool I use vite, and as I could see from the vite documentation, vite goes and makes a full reload because the imported json file is bundled and has changed after saving.

This information is already worth a lot!

But now I have the question how to proceed on the basis of this information. I would like to continue to add the json file via import, but wonder how useful it is to bundle the json when it is changed in the course of use.

My idea would be to load the json files via fetch instead of import, but that would eliminate bundling and as far as I know performance would suffer slightly.

However, I would welcome any solution approach

Please or to participate in this conversation.