Skip to content

Conversation

@bartekus
Copy link

Description:

This PR introduces first-class support for Inertia.js endpoints within Encore’s TypeScript runtime by adding a new api.inertia() helper method. This enables Encore applications to serve SSR-friendly single-page applications (SPAs) without leaving the Encore runtime model.

Key Changes:
Dependency Addition
runtimes/js/Cargo.toml

Adds the inertia-rust crate as a dependency to interface with Inertia’s protocol:

inertia = { git = "https://github.com/KaioFelps/inertia-rust", branch = "v2" }

New API Helper
runtimes/js/encore.dev/api/mod.ts

Adds api.inertia(), a new helper for defining Inertia-compatible endpoints:

export type InertiaHandler = (req, res) => Promise<{
  component: string;
  props: Record<string, any>;
  status?: number;
  version?: string;
}>;

api.inertia = function inertia(options, handler) { ... };

This API allows responding to both traditional page loads and X-Inertia JSON-based SPA transitions, offering a unified pattern for rendering pages server-side and client-side with minimal friction.

Why This Matters:

This addition aligns Encore with modern SSR/SPA architecture practices and makes it straightforward to integrate a frontend powered by Vue/React/Svelte via Inertia.js, without abandoning Encore’s opinionated backend design.

Example Use Case:

export const showDashboard = api.inertia(
  { path: "/dashboard", method: "GET", expose: true },
  async (req, res) => ({
    component: "Dashboard",
    props: { user: { name: "Alice" } },
  })
);

The endpoint seamlessly supports both direct HTML entry and client-side navigation with Inertia.

@encore-cla
Copy link

encore-cla bot commented May 10, 2025

All committers have signed the CLA.

@bartekus bartekus closed this May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant