Inertia Response

class litestar_vite.inertia.response.InertiaResponse[source]

Bases: Response[T]

Inertia Response

__init__(content: T, *, template_name: str | None = None, template_str: str | None = None, background: BackgroundTask | BackgroundTasks | None = None, context: dict[str, Any] | None = None, cookies: ResponseCookies | None = None, encoding: str = 'utf-8', headers: ResponseHeaders | None = None, media_type: MediaType | str | None = None, status_code: int = 200, type_encoders: TypeEncodersMap | None = None, encrypt_history: bool | None = None, clear_history: bool = False, scroll_props: ScrollPropsConfig | None = None) None[source]

Handle the rendering of a given template into a bytes string.

Parameters:
  • content – A value for the response body that will be rendered into bytes string.

  • template_name – Path-like name for the template to be rendered, e.g. index.html.

  • template_str – A string representing the template, e.g. tmpl = "Hello <strong>World</strong>".

  • background – A BackgroundTask instance or BackgroundTasks to execute after the response is finished. Defaults to None.

  • context – A dictionary of key/value pairs to be passed to the temple engine’s render method.

  • cookies – A list of Cookie instances to be set under the response Set-Cookie header.

  • encoding – Content encoding

  • headers – A string keyed dictionary of response headers. Header keys are insensitive.

  • media_type – A string or member of the MediaType enum. If not set, try to infer the media type based on the template name. If this fails, fall back to text/plain.

  • status_code – A value for the response HTTP status code.

  • type_encoders – A mapping of types to callables that transform them into types supported for serialization.

  • encrypt_history – Enable browser history encryption for this response (v2 feature). When True, the Inertia client encrypts history state using the browser’s crypto API. If None, falls back to InertiaConfig.encrypt_history. See: https://inertiajs.com/history-encryption

  • clear_history – Clear previously encrypted history state (v2 feature). When True, the client will regenerate its encryption key, invalidating all previously encrypted history entries. Use during logout to ensure sensitive data cannot be recovered from browser history.

  • scroll_props – Configuration for infinite scroll (v2 feature). Provides next/previous page information for paginated data. Use the scroll_props() helper to create this configuration.

Raises:

ValueError – If both template_name and template_str are provided.

create_template_context(request: Request[UserT, AuthT, StateT], page_props: PageProps[T], type_encoders: TypeEncodersMap | None = None) dict[str, Any][source]

Create a context object for the template.

Parameters:
  • request – A Request instance.

  • page_props – A formatted object to return the inertia configuration.

  • type_encoders – A mapping of types to callables that transform them into types supported for serialization.

Returns:

A dictionary holding the template context

to_asgi_response(app: Litestar | None, request: Request[UserT, AuthT, StateT], *, background: BackgroundTask | BackgroundTasks | None = None, cookies: Iterable[Cookie] | None = None, encoded_headers: Iterable[tuple[bytes, bytes]] | None = None, headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) ASGIResponse[source]

Create an ASGIResponse from a Response instance.

Parameters:
  • app – The Litestar application instance.

  • background – Background task(s) to be executed after the response is sent.

  • cookies – A list of cookies to be set on the response.

  • encoded_headers – A list of already encoded headers.

  • headers – Additional headers to be merged with the response headers. Response headers take precedence.

  • is_head_response – Whether the response is a HEAD response.

  • media_type – Media type for the response. If media_type is already set on the response, this is ignored.

  • request – The Request instance.

  • status_code – Status code for the response. If status_code is already set on the response, this is

  • type_encoders – A dictionary of type encoders to use for encoding the response content.

Returns:

An ASGIResponse instance.

Delete a cookie.

Parameters:
  • key – Key of the cookie.

  • path – Path of the cookie.

  • domain – Domain of the cookie.

Returns:

None.

render(content: Any, media_type: str, enc_hook: Serializer = <function default_serializer>) bytes

Handle the rendering of content into a bytes string.

Returns:

An encoded bytes string

Set a cookie on the response. If passed a Cookie instance, keyword arguments will be ignored.

Parameters:
  • key – Key for the cookie or a Cookie instance.

  • value – Value for the cookie, if none given defaults to empty string.

  • max_age – Maximal age of the cookie before its invalidated.

  • expires – Seconds from now until the cookie expires.

  • path – Path fragment that must exist in the request url for the cookie to be valid. Defaults to /.

  • domain – Domain for which the cookie is valid.

  • secure – Https is required for the cookie.

  • httponly – Forbids javascript to access the cookie via document.cookie.

  • samesite – Controls whether a cookie is sent with cross-site requests. Defaults to lax.

Returns:

None.

set_etag(etag: str | ETag) None

Set an etag header.

Parameters:

etag – An etag value.

Returns:

None

set_header(key: str, value: Any) None

Set a header on the response.

Parameters:
  • key – Header key.

  • value – Header value.

Returns:

None.

class litestar_vite.inertia.response.InertiaExternalRedirect[source]

Bases: Response[Any]

External redirect via Inertia protocol (409 + X-Inertia-Location).

This response type triggers a client-side hard redirect in Inertia.js. Unlike InertiaRedirect, this does NOT validate the redirect URL as same-origin because external redirects are explicitly intended for cross-origin navigation (e.g., OAuth callbacks, external payment pages).

Note

Request cookies are intentionally NOT passed to the response to prevent cookie leakage in redirect responses.

__init__(request: Request[Any, Any, Any], redirect_to: str, **kwargs: Any) None[source]

Initialize external redirect with 409 status and X-Inertia-Location header.

Parameters:
  • request – The request object.

  • redirect_to – The URL to redirect to (can be external).

  • **kwargs – Additional keyword arguments passed to the Response constructor.

Delete a cookie.

Parameters:
  • key – Key of the cookie.

  • path – Path of the cookie.

  • domain – Domain of the cookie.

Returns:

None.

render(content: Any, media_type: str, enc_hook: Serializer = <function default_serializer>) bytes

Handle the rendering of content into a bytes string.

Returns:

An encoded bytes string

Set a cookie on the response. If passed a Cookie instance, keyword arguments will be ignored.

Parameters:
  • key – Key for the cookie or a Cookie instance.

  • value – Value for the cookie, if none given defaults to empty string.

  • max_age – Maximal age of the cookie before its invalidated.

  • expires – Seconds from now until the cookie expires.

  • path – Path fragment that must exist in the request url for the cookie to be valid. Defaults to /.

  • domain – Domain for which the cookie is valid.

  • secure – Https is required for the cookie.

  • httponly – Forbids javascript to access the cookie via document.cookie.

  • samesite – Controls whether a cookie is sent with cross-site requests. Defaults to lax.

Returns:

None.

set_etag(etag: str | ETag) None

Set an etag header.

Parameters:

etag – An etag value.

Returns:

None

set_header(key: str, value: Any) None

Set a header on the response.

Parameters:
  • key – Header key.

  • value – Header value.

Returns:

None.

to_asgi_response(app: Litestar | None, request: Request, *, background: BackgroundTask | BackgroundTasks | None = None, cookies: Iterable[Cookie] | None = None, encoded_headers: Iterable[tuple[bytes, bytes]] | None = None, headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) ASGIResponse

Create an ASGIResponse from a Response instance.

Parameters:
  • app – The Litestar application instance.

  • background – Background task(s) to be executed after the response is sent.

  • cookies – A list of cookies to be set on the response.

  • encoded_headers – A list of already encoded headers.

  • headers – Additional headers to be merged with the response headers. Response headers take precedence.

  • is_head_response – Whether the response is a HEAD response.

  • media_type – Media type for the response. If media_type is already set on the response, this is ignored.

  • request – The Request instance.

  • status_code – Status code for the response. If status_code is already set on the response, this is

  • type_encoders – A dictionary of type encoders to use for encoding the response content.

Returns:

An ASGIResponse instance.

class litestar_vite.inertia.response.InertiaRedirect[source]

Bases: Redirect

Redirect to a specified URL with same-origin validation.

This class validates the redirect URL to prevent open redirect attacks. If the URL is not same-origin, it falls back to the application’s base URL.

Note

Request cookies are intentionally NOT passed to the response to prevent cookie leakage in redirect responses.

__init__(request: Request[Any, Any, Any], redirect_to: str, **kwargs: Any) None[source]

Initialize redirect with safe URL validation.

Parameters:
  • request – The request object.

  • redirect_to – The URL to redirect to. Must be same-origin or relative.

  • **kwargs – Additional keyword arguments passed to the Redirect constructor.

Delete a cookie.

Parameters:
  • key – Key of the cookie.

  • path – Path of the cookie.

  • domain – Domain of the cookie.

Returns:

None.

render(content: Any, media_type: str, enc_hook: Serializer = <function default_serializer>) bytes

Handle the rendering of content into a bytes string.

Returns:

An encoded bytes string

Set a cookie on the response. If passed a Cookie instance, keyword arguments will be ignored.

Parameters:
  • key – Key for the cookie or a Cookie instance.

  • value – Value for the cookie, if none given defaults to empty string.

  • max_age – Maximal age of the cookie before its invalidated.

  • expires – Seconds from now until the cookie expires.

  • path – Path fragment that must exist in the request url for the cookie to be valid. Defaults to /.

  • domain – Domain for which the cookie is valid.

  • secure – Https is required for the cookie.

  • httponly – Forbids javascript to access the cookie via document.cookie.

  • samesite – Controls whether a cookie is sent with cross-site requests. Defaults to lax.

Returns:

None.

set_etag(etag: str | ETag) None

Set an etag header.

Parameters:

etag – An etag value.

Returns:

None

set_header(key: str, value: Any) None

Set a header on the response.

Parameters:
  • key – Header key.

  • value – Header value.

Returns:

None.

to_asgi_response(app: Litestar | None, request: Request, *, background: BackgroundTask | BackgroundTasks | None = None, cookies: Iterable[Cookie] | None = None, encoded_headers: Iterable[tuple[bytes, bytes]] | None = None, headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) ASGIResponse

Create an ASGIResponse from a Response instance.

Parameters:
  • app – The Litestar application instance.

  • background – Background task(s) to be executed after the response is sent.

  • cookies – A list of cookies to be set on the response.

  • encoded_headers – A list of already encoded headers.

  • headers – Additional headers to be merged with the response headers. Response headers take precedence.

  • is_head_response – Whether the response is a HEAD response.

  • media_type – Media type for the response. If media_type is already set on the response, this is ignored.

  • request – The Request instance.

  • status_code – Status code for the response. If status_code is already set on the response, this is

  • type_encoders – A dictionary of type encoders to use for encoding the response content.

Returns:

An ASGIResponse instance.

class litestar_vite.inertia.response.InertiaBack[source]

Bases: Redirect

Redirect back to the previous page using the Referer header.

This class safely validates the Referer header to prevent open redirect attacks. If the Referer is not same-origin or is missing, it falls back to the application’s base URL.

Note

Request cookies are intentionally NOT passed to the response to prevent cookie leakage in redirect responses.

__init__(request: Request[Any, Any, Any], **kwargs: Any) None[source]

Initialize back redirect with safe URL validation.

Parameters:
  • request – The request object.

  • **kwargs – Additional keyword arguments passed to the Redirect constructor.

Delete a cookie.

Parameters:
  • key – Key of the cookie.

  • path – Path of the cookie.

  • domain – Domain of the cookie.

Returns:

None.

render(content: Any, media_type: str, enc_hook: Serializer = <function default_serializer>) bytes

Handle the rendering of content into a bytes string.

Returns:

An encoded bytes string

Set a cookie on the response. If passed a Cookie instance, keyword arguments will be ignored.

Parameters:
  • key – Key for the cookie or a Cookie instance.

  • value – Value for the cookie, if none given defaults to empty string.

  • max_age – Maximal age of the cookie before its invalidated.

  • expires – Seconds from now until the cookie expires.

  • path – Path fragment that must exist in the request url for the cookie to be valid. Defaults to /.

  • domain – Domain for which the cookie is valid.

  • secure – Https is required for the cookie.

  • httponly – Forbids javascript to access the cookie via document.cookie.

  • samesite – Controls whether a cookie is sent with cross-site requests. Defaults to lax.

Returns:

None.

set_etag(etag: str | ETag) None

Set an etag header.

Parameters:

etag – An etag value.

Returns:

None

set_header(key: str, value: Any) None

Set a header on the response.

Parameters:
  • key – Header key.

  • value – Header value.

Returns:

None.

to_asgi_response(app: Litestar | None, request: Request, *, background: BackgroundTask | BackgroundTasks | None = None, cookies: Iterable[Cookie] | None = None, encoded_headers: Iterable[tuple[bytes, bytes]] | None = None, headers: dict[str, str] | None = None, is_head_response: bool = False, media_type: MediaType | str | None = None, status_code: int | None = None, type_encoders: TypeEncodersMap | None = None) ASGIResponse

Create an ASGIResponse from a Response instance.

Parameters:
  • app – The Litestar application instance.

  • background – Background task(s) to be executed after the response is sent.

  • cookies – A list of cookies to be set on the response.

  • encoded_headers – A list of already encoded headers.

  • headers – Additional headers to be merged with the response headers. Response headers take precedence.

  • is_head_response – Whether the response is a HEAD response.

  • media_type – Media type for the response. If media_type is already set on the response, this is ignored.

  • request – The Request instance.

  • status_code – Status code for the response. If status_code is already set on the response, this is

  • type_encoders – A dictionary of type encoders to use for encoding the response content.

Returns:

An ASGIResponse instance.