PHP 8.6项目使用Inertia 3的defer时,可以先返回页面主体,随后通过另一次请求读取较慢的统计数据,由前端Deferred组件显示等待内容。它不是后台队列任务;延迟回调依然属于受权限约束的页面请求,需要在统计查询中保留当前用户或租户范围。
核对服务端和前端主版本
1、检查inertiajs/inertia-laravel及前端@inertiajs/vue3都使用项目已经验证的3.x兼容版本,再运行composer平台检查和前端构建。这里采用的官方文档是inertia 3,不把其中的全部行为推定为旧版2.x相同。
2、以下控制器假设已有Order模型,orders表包含user_id与total列,页面路由已要求登录。按真实表结构调整,不能删除用户范围来解决查不到记录的问题。
把较慢查询移到延迟回调
use App\Models\Order;
use Illuminate\Http\Request;
use Inertia\Inertia;
public function dashboard(Request $request) {
$userId = $request->user()->id;
return Inertia::render('Dashboard', [
'heading' => '我的订单概览',
'stats' => Inertia::defer(fn () => [
'count' => Order::where('user_id', $userId)->count(),
'total' => Order::where('user_id', $userId)->sum('total'),
]),
]);
}

Inertia服务端defer与请求分组
多个延迟属性默认可以合并请求;第二个分组参数可让不同组分开请求。不要为了“并行”把同一用户任务拆成大量很小的查询请求,先看实际瓶颈。
让Vue在数据到达前有可见状态
<script setup>
import { Deferred } from '@inertiajs/vue3'
const props = defineProps({ heading: String, stats: Object })
</script><template><h1>{{ props.heading }}</h1>
<deferred data="stats"><template><p>正在读取统计数据……</p></template><section><p>订单数:{{ props.stats.count }}</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20e352aa140.jpg" alt="Build MCP servers and clients for PHP applications with the official PHP SDK" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="overflowclass">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p class="overflowclass">使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
<p>金额合计:{{ props.stats.total }}</p>
</section></deferred></template>

Vue Deferred组件及等待内容模板
在网络面板核对初始页面与后续数据请求,确认等待内容会在stats到达后替换。登录失效、查询异常或网络失败时应走项目既有错误处理,不能把错误写成0条订单;defer也不会自动缓存结果或免除每次请求的授权检查。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










