消息

WhenMounted and BigInt Props in Inertia.js v3.8

发布
WhenMounted and BigInt Props in Inertia.js v3.8 image

The Inertia.js team released v3.8.0 with a WhenMounted component for rendering browser-only code under server-side rendering, opt-in support for integers beyond JavaScript's safe range as native BigInt values, and more:

WhenMounted for Browser-Only Components

Evan Burrell 贡献了一个 WhenMounted component for the React, Vue, and Svelte adapters. A component that reads window 或者 document while rendering crashes server-side rendering with a "window is not defined" error.

这 WhenMounted component renders its fallback on the server and its children once the component has mounted in the browser:

进口 { WhenMounted } 从 '@inertiajs/vue3'
< 模板 >
< WhenMounted >
< SalesChart />
< 模板 # 倒退 >
< 页 >Loading chart...</ 页 >
</ 模板 >
</ WhenMounted >
</ 模板 >

In React, pass the children as a function so the browser-only code does not run on the server, since JSX children are evaluated before the component renders:

进口 { WhenMounted } 从 '@inertiajs/react'
< WhenMounted 倒退 = {< 页 >Loading chart...</ 页 >}>
{() => < SalesChart 宽度 = {window.innerWidth} />}
</ WhenMounted >

The fallback shows only during SSR and the first hydration render, so the browser's first render matches the server HTML and there is no hydration mismatch. A WhenMounted that mounts later, such as after a client-side visit, renders its children right away.

看 #3218 更多细节。

Big Integers as Native BigInt Values

帕斯卡·巴尔杰特 added support for integers outside JavaScript's safe integer range. PHP sends a 64-bit integer, such as a Snowflake ID, as a JSON number, and the browser rounds it when parsing the page, so 900719925474099988 变成 900719925474100000 with no error.

The feature is opt-in on the server and needs inertia-laravel v3.5.0 . Turn it on for every response with the preserve_big_integers 选项 config/inertia.php (或 INERTIA_PRESERVE_BIG_INTEGERS environment variable), or for a single response:

返回 惯性 :: 使成为 ( 'Orders/Show' ,[
'ID' => $订单 -> ID,
]) -> preserveBigIntegers ();

The adapter encodes large integers as { "$bigint": "..." } markers, and the client turns them back into BigInt values. Integers inside the safe range stay regular numbers:

定义Props ({ id: BigInt })
< 模板 >
< 页 >Order {{ id }}</ 页 >
<!-- Order 900719925474099988 -->
</ 模板 >

This works for the initial page, visits, partial reloads, encrypted history, and SSR. A BigInt sent back through the router, a form, or Precognition is submitted as a string of digits, which Laravel's integer validation rule and $request->integer() accept.

看 #3237 更多细节。

Form Forwards Error and Flash Callbacks

Dale Weaver fixed the <Form> component so it passes onHttpException , onNetworkError , onBeforeUpdate , 和 onFlash to the visit. These callbacks were already part of the component's props, but the adapters never forwarded them, so an HTTP 500 or a network failure on a <Form> submission never called them. They now work the same as on useForm , and returning false from either error callback stops the global event and the default handling:

<Form
action="/orders"
method="post"
@http-exception="(response) => { toast.error('Something went wrong'); return false }"
>

看 #3262 更多细节。

Out-of-Order Optimistic Responses

Pascal Baljet fixed a case where two optimistic updates were in flight and the first request's response arrived last. Its props were read before the second write, so applying them rolled the second update back on screen. The page then disagreed with the server until the next visit.

Optimistic requests now carry an ID, and a response is ignored when a later optimistic request has already succeeded. A failed request does not count, so an older successful response still updates the page after a newer request fails and rolls back.

看 #3245 更多细节。

History Restores After a Deploy

Lazizbek Ergashev fixed back and forward navigation across a deploy. When the browser reloaded the document on a back or forward navigation, Inertia restored the page from history state without checking its asset version. The new JavaScript bundle then rendered props from the previous deploy, and a prop added or renamed since then was undefined . Inertia now compares the versions and renders the freshly loaded page when they differ.

看 #3264 更多细节。

发行说明

You can see the complete list of changes below and the diff between 3.7.1 and 3.8.0 在 GitHub 上。以下发行说明直接来自 GitHub release :

v3.8.0

保罗·雷德蒙德照片

Laravel News 特约撰稿人。全栈 Web 开发人员兼作家。

赞助

掌握 Laravel 标志
Laravel 代码审查

几天内即可获得 Laravel 代码审查方面的专家指导

访问 Laravel 代码审查
Postcodes for Laravel: GB Postcode Lookup and Geography Data image

Postcodes for Laravel: GB Postcode Lookup and Geography Data

阅读文章
Laravel 14 Adds a defaults() Method to Eloquent Models image

Laravel 14 Adds a defaults() Method to Eloquent Models

阅读文章
Laravel AI SDK and Laravel MCP Security Fixes: Update Now image

Laravel AI SDK and Laravel MCP Security Fixes: Update Now

阅读文章
Mailbox for Laravel: Preview and Test Rendered Email image

Mailbox for Laravel: Preview and Test Rendered Email

阅读文章
Count Worker Crashes as Job Exceptions in Laravel 13.34 image

Count Worker Crashes as Job Exceptions in Laravel 13.34

阅读文章
Elastic Bridge: Eloquent-Style Queries for Elasticsearch and OpenSearch image

Elastic Bridge: Eloquent-Style Queries for Elasticsearch and OpenSearch

阅读文章