Skip to main content

Add your description here

Project description

Inertia.js FastAPI Adapter

A Python adapter for using Inertia.js with FastAPI.

📚 Documentation | 🚀 Quick Start | 🗺️ Roadmap

Features

  • ✅ Full Inertia.js protocol support
  • ✅ Vite integration (dev & production)
  • ✅ Auto-detection of Vite entry point from vite.config.ts/js
  • ✅ Asset versioning for cache busting
  • ✅ Validation error handling (422 responses)
  • ✅ History encryption for sensitive data
  • ✅ External redirects (OAuth, payments, etc.)
  • ✅ Partial reloads & shared data
  • ✅ TypeScript support

Installation

# This package (when published)
pip install inertia-fastapi

# Or install from source
pip install -e .

Quick Start

1. Basic Setup

from fastapi import FastAPI
from inertia import InertiaDep

app = FastAPI()

@app.get("/")
async def home(inertia: InertiaDep):
    return inertia.render(
        "Home",
        {
            "message": "Hello from Inertia!"
        }
    )

2. Custom Configuration

If you need to customize the Inertia configuration (e.g., different template directory or Vite settings):

from fastapi import FastAPI, Request
from inertia import InertiaResponse, Inertia

# Create custom InertiaResponse instance
inertia_response = InertiaResponse(
    template_dir="my_templates",
    vite_dev_url="http://localhost:5173",
    manifest_path="dist/.vite/manifest.json",
    vite_entry="src/main.tsx",  # Optional: auto-detected from vite.config
    vite_config_path="vite.config.ts"  # Optional: defaults to vite.config.ts
)

app = FastAPI()

def get_custom_inertia(request: Request) -> Inertia:
    return Inertia(request, inertia_response)

@app.get("/")
async def home(inertia: Inertia = Depends(get_custom_inertia)):
    return inertia.render("Home", {"message": "Hello!"})

Configuration Options

InertiaResponse Parameters

Parameter Type Default Description
template_dir str "templates" Directory containing your root HTML template
vite_dev_url str "http://localhost:5173" Vite dev server URL
manifest_path str "static/build/.vite/manifest.json" Path to Vite manifest file (production)
vite_entry str | None None Vite entry point (auto-detected from config if None)
vite_config_path str "vite.config.ts" Path to vite.config.ts/js for auto-detection

Auto-Detection of Vite Entry

By default, the adapter will attempt to read your vite.config.ts (or .js) file and extract the entry point from:

// vite.config.ts
export default defineConfig({
  build: {
    rollupOptions: {
      input: "frontend/app.tsx",  // ← Auto-detected
    },
  },
});

This means you don't need to specify vite_entry manually - it will match your Vite configuration automatically!

Root Template

Create a template file (default: templates/app.html):

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    {{ vite()|safe }}
</head>
<body>
    <div id="app" data-page='{{ page }}'></div>
</body>
</html>

The {{ vite() }} function will automatically include:

  • React Fast Refresh scripts (dev mode)
  • Vite client scripts (dev mode)
  • Your entry point script
  • Built CSS and JS files (production mode)

Using Custom Entry Points

<!-- Use default entry (from config/auto-detection) -->
{{ vite()|safe }}

<!-- Use custom entry point -->
{{ vite('admin/app.js')|safe }}

Backward compatibility: The old {{ vite_tags|safe }} variable is still supported.

Validation Errors

Validation errors are automatically handled:

@app.post("/users")
async def create_user(inertia: InertiaDep):
    errors = validate_user(request_data)

    if errors:
        # Returns 422 status for Inertia requests
        return inertia.render(
            "Users/Create",
            {"user": request_data},
            errors=errors
        )

    # Create user...
    return inertia.render("Users/Show", {"user": new_user})

External Redirects

Use inertia.location() to redirect to external websites or non-Inertia pages:

@app.get("/auth/github")
async def github_oauth(inertia: InertiaDep):
    """Redirect to GitHub OAuth"""
    oauth_url = f"https://github.com/login/oauth/authorize?client_id={CLIENT_ID}"
    return inertia.location(oauth_url)

@app.get("/shelter/{id}/directions")
async def get_directions(id: int, inertia: InertiaDep):
    """Redirect to Google Maps"""
    shelter = get_shelter(id)
    maps_url = f"https://maps.google.com/?q={shelter.address}"
    return inertia.location(maps_url)

This returns a 409 Conflict response with X-Inertia-Location header, which the client automatically follows with a full page navigation.

History Encryption

Protect sensitive data in browser history by encrypting page state. This prevents users from viewing sensitive information after logging out by pressing the back button.

# Encrypt sensitive pages
@app.get("/account/transactions")
async def transactions(inertia: InertiaDep):
    inertia.encrypt_history()  # Enable encryption
    return inertia.render("Transactions", {
        "balance": user.balance,
        "transactions": user.get_transactions()
    })

# Clear encrypted history on logout
@app.post("/logout")
async def logout(inertia: InertiaDep):
    clear_user_session()
    inertia.clear_history()  # Rotate encryption keys
    return inertia.render("Login", {})

How it works:

  • Uses browser's Web Crypto API (AES-GCM encryption)
  • Encryption keys stored in sessionStorage
  • clear_history() rotates keys, making old history unreadable
  • Only works over HTTPS (except localhost)

Use cases: Banking apps, healthcare (HIPAA), admin panels, any sensitive data

Development vs Production

The adapter automatically detects whether Vite dev server is running:

  • Dev mode: Includes Vite dev server scripts and React Fast Refresh
  • Production mode: Reads from manifest.json and includes built assets

No configuration changes needed - it just works!

Feature Status

Feature Laravel Inertia FastAPI Inertia Priority
Basic protocol -
Template function {{ vite() }} @vite -
Auto Vite entry detection -
Dev/Prod mode detection -
Validation errors (422) -
Asset versioning (basic) -
Asset version mismatch (409) -
Partial reloads -
Shared data -
External redirects -
History encryption -
Lazy props ⏳ Planned 🟡 Medium
Deferred props ⏳ Planned 🟡 Medium
Merging props ⏳ Planned 🟡 Medium
Error bags ⏳ Planned 🟢 Low
Prefetching ⏳ Planned 🟢 Low
SSR ❌ Not planned -

See ROADMAP.md for detailed implementation plans and progress tracking.

Current Status

Version: v0.2.0 "Production Ready"

This adapter implements all production-critical Inertia features and is ready for production use.

Production-ready features:

  • ✅ Basic page rendering
  • ✅ Form submissions with validation
  • ✅ Navigation between pages
  • ✅ Development with Vite HMR
  • ✅ Asset version mismatch handling (409 Conflict)
  • ✅ Partial reloads for performance
  • ✅ Shared data (auth, flash messages)
  • ✅ External redirects (OAuth, payments)
  • ✅ History encryption (sensitive data protection)

Coming soon:

  • ⏳ Lazy props evaluation
  • ⏳ Deferred props
  • ⏳ Merging props (infinite scroll)

Contributing

Contributions are very welcome! This adapter aims to match the Laravel adapter's feature set.

How to contribute:

  1. Check GitHub Issues for open tasks
  2. Pick a feature (look for good first issue or high-priority labels)
  3. Read the linked Inertia.js documentation
  4. Implement following existing patterns
  5. Write tests and update documentation
  6. Submit a PR!

See ROADMAP.md for the full project roadmap and milestone planning.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cross_inertia-0.1.0.tar.gz (297.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cross_inertia-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file cross_inertia-0.1.0.tar.gz.

File metadata

  • Download URL: cross_inertia-0.1.0.tar.gz
  • Upload date:
  • Size: 297.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for cross_inertia-0.1.0.tar.gz
Algorithm Hash digest
SHA256 64a0e79817a4edc769de7f841643d9cba553fcb7089ca3d0e817545507fc7e52
MD5 64ace2fcca901dfd8d833412d236493e
BLAKE2b-256 e47dad568c22902a8b90c4515856eba77406e393116a7527d6152c9b4cb90fa9

See more details on using hashes here.

File details

Details for the file cross_inertia-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cross_inertia-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f04e2c8ead47d16c73f660b9ddcdf58c95ea68d49a7944dace9781d3f6f32f27
MD5 78e9e6c8c0953409b3c39d8d37c79cdb
BLAKE2b-256 a69baf43dc10de15eb0a83234a7c7cbfa96d1121cdc0f58aeb5d21ecfff8b72b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page