Skip to content

Add Inertia.js API handler -> api.inertia #1906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

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