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 #fallback><p>正在读取统计数据……</p></template>
<section>
<p>订单数:{{ props.stats.count }}</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" 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" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/skill13222" title="Build MCP servers and clients for PHP applications with the official PHP SDK">Build MCP servers and clients for PHP applications with the official PHP SDK</a>
<p>使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。</p>
</div>
<a 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><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>
<p>金额合计:{{ props.stats.total }}</p>
</section>
</Deferred>
</template>
Vue Deferred组件及等待内容模板
在网络面板核对初始页面与后续数据请求,确认等待内容会在stats到达后替换。登录失效、查询异常或网络失败时应走项目既有错误处理,不能把错误写成0条订单;defer也不会自动缓存结果或免除每次请求的授权检查。


















