Lazy Evaluation Not Working as Expected #2237
-
Hi, I’m trying to use lazy data evaluation in Inertia.js as per the documentation. However, when I wrap a prop in a closure (e.g., Example Code:return Inertia::render('TestPage', [
'users' => fn () => {
sleep(10); // Simulate delay
return User::all();
},
'companies' => fn () => Company::all(),
]); If I visit TestPage that doesn’t use the as prop, it still takes 10 seconds to load, meaning the closure is being evaluated. Expected Behavior: Questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
That is expected. As per the documentation that you linked, wrapping the value in a Closure will only make it lazy-loaded on subsequent requests via Inertia when requested. First visit will always be evaluated and included with this method. Whether you accept it as a prop or not does not matter as the props definition in the page only tells the page what props are available to be used. It does not mean the page will request only those props when loading the page. In order to make sure that it's only evaluated when requested (meaning: completely lazy-loaded via a separate request), you need to wrap it in For reference, see the examples below: (Copy-pasted the documentation page)
|
Beta Was this translation helpful? Give feedback.
That is expected. As per the documentation that you linked, wrapping the value in a Closure will only make it lazy-loaded on subsequent requests via Inertia when requested. First visit will always be evaluated and included with this method.
Whether you accept it as a prop or not does not matter as the props definition in the page only tells the page what props are available to be used. It does not mean the page will request only those props when loading the page.
In order to make sure that it's only evaluated when requested (meaning: completely lazy-loaded via a separate request), you need to wrap it in
Inertia::lazy()
.For reference, see the examples below: (Copy-pasted the documentation…