Rendering pages
The fundamental unit of inertia request is the page object returned from your app routes. On first request, inertia server will respond with a rendered HTML, and on subsequent requests, it will respond with a JSON payload with information on how to update the client-side app. To create a page response, use therender function.
definePage function
that you set up during the package configuration. It’s thanks to page definitions that we can
provide type safety between the client and server code.
Working with pages is covered in detail in the pages section.
Redirects
If we return normal redirect response from our server, it doesn’t take inertia features into account. Make sure to use a dedicated function that creates an appropriateResponse object for the current inertia request.
State
Every server application comes with global data associated with the request. It would be cumbersome to pass errors, user objects or flash messages manually to every page. Server helper has dedicated functions to make this process easier.Shared data
Shared keys are available to all pages in the application. To provide them, you can call theshare function
somewhere during your request lifecycle. Middleware is the most common place to do this.
shareOnce function.
~/server/inertia.ts
share function. Every page props will have access user and appName keys.
Shared keys will be marked as optional, so in addition to their base type they will get a
undefined added to it.requireShared option when defining the page.
Then it won’t be optional in the generated page prop types. Also, a server error will be thrown if the key was not provided.
Errors
If any errors are caught during the request, you can pass them your page witherrors function.
For errors to work correctly, you have to configure sessions when creating your inertia helper
errors function, you can assign errors to a specific error bag. They won’t be included
in the response unless the request specifically asks for them.
errors object. Error bags are nested under their own keys inside of it.
So if you call inertia.errors({ email: 'Invalid email' }, 'login'), you can access your page errors.login.email key.
Flash messages
Flash messages are a way to share some keys with your client in the next response. They are added with theflash function
and are available under the flash key in the page props.
For flashing to work correctly, you have to configure sessions when creating your inertia helper
flash object. Yes, you have guessed it, they have to be typed too :>