Inertia.js is a tool for creating modern single—page applications (SPA) without having to work with a full-fledged API (as in the traditional REST or GraphQL approach). It allows you to develop the frontend and backend as a single application using classic server routes and controllers, but with dynamic page updates without a complete reboot.
Inertia.js acts as a bridge between the frontend (React, Vue, Svelte) and the backend (Laravel, Rails, Django, etc.). Here are the key principles of its operation:
return req.Inertia.render({
component: 'component',
props: props
})
const submit = () => post(‘/users’);
* When submitting the form, Inertia makes a POST request, the server processes it and returns the updated component data.
# Inertia Express Adapter
## Configuration Options
The adapter accepts the following configuration options:
```typescript
interface Options {
readonly enableReload?: boolean; // Enable component reloading
readonly version: string; // Asset version for cache busting
readonly html: (page: Item, viewData: props) => string; // HTML template function
readonly flashMessages?: (req: Request) => props; // Flash messages handler
readonly csrf?: { // CSRF protection configuration
enabled: boolean;
cookieName?: string; // Default: 'XSRF-TOKEN'
headerName?: string; // Default: 'x-xsrf-token'
};
}
The adapter adds an Inertia
object to the Express request with these methods:
setViewData(viewData: props)
shareProps(sharedProps: props)
setStatusCode(statusCode: number)
setHeaders(headers: Record<string, string>)
render(page: Item)
redirect(url: string)
```typescript type props = Record<string | number | symbol, unknown>
interface Item { readonly component: string; // Name of the frontend component props: props; // Component props readonly url?: string; // Current URL readonly version?: string; // Asset version }
## Headers
The adapter uses these Inertia-specific headers:
* `x-inertia`: Identifies Inertia requests
* `x-inertia-version`: Asset version for version checking
* `x-inertia-location`: Used for version mismatch redirects
* `x-inertia-partial-data`: Specifies partial reload data fields
* `x-inertia-partial-component`: Specifies component for partial reloads
* `x-inertia-current-component`: Tracks current component for reloads
## CSRF Protection
When enabled, the adapter:
1. Generates and sets a CSRF token cookie (`XSRF-TOKEN`)
* Validates the token on non-GET requests by comparing:
2. Cookie value with header value (`x-xsrf-token` by default)
3. Returns 403 for invalid tokens
## Version Checking
The adapter automatically:
* Checks version headers on GET requests
* Returns 409 with location header when versions mismatch
* Destroys the session before redirecting
## Usage Example
```typescript
// flash messages
adminizer.app.use(flash());
// inertia adapter
adminizer.app.use(
inertia({
version: '1',
html: getHtml,
flashMessages: (req: ReqType) => {
return req.flash.flashAll();
},
csrf: {
enabled: true,
cookieName: 'XSRF-TOKEN',
headerName: 'x-xsrf-token'
},
})
);
req.Inertia.setViewData({
lang: req.session.UserAP?.locale || 'en',
})
const menuHelper = new InertiaMenuHelper(adminizer)
req.Inertia.shareProps({
auth: {
user: req.session.UserAP
},
menu: req.session.UserAP ? menuHelper.getMenuItems(req) : null,
brand: menuHelper.getBrandTitle(),
logout: menuHelper.getLogoutUrl(),
//...
})
See the implementation here src/system/bindInertia.ts
Example:
//add.ts
const props = inertiaAddHelper(req, entity, fields)
return req.Inertia.render({
component: 'add',
props: props
})
The Flash Middleware provides session-based flash message management for Express.js applications. Flash messages are temporary messages that persist across requests, typically used for displaying notifications, errors, or other one-time information to users.
req.flash.setFlashMessage('success', 'Works!');
req.flash.setFlashMessage('error', 'Oops!');
For more information, see src/lib/inertia/flash.ts
Note:
Client use React 19. For more information about how inertia works on the client, see http://inertiajs.com