Skip to main content

Masonite Package GitHub Workflow Status (branch) Python Version PyPI License Code style: black

Introduction

Inertia is a new approach to building classic server-driven web apps. From their own web page:

Inertia allows you to create fully client-side rendered, single-page apps, without much of the complexity that comes with modern SPAs. It does this by leveraging existing server-side frameworks.

Inertia requires an adapter for each backend framework. This repo contains the Masonite server-side adapter for Inertia.js. You can find the legacy Inertia PingCRM demo with Masonite here demo (WIP).

Documentation 📚 Quick Start ⚡️

Features

Almost all features of the official server-side adapters are present 😃

  • Shared data
  • Partial reloads
  • Lazy loaded props
  • Set root view in a provider
  • Set root view per view
  • Enable sharing Masonite routes (prefer using masonite-js-routes)
  • Enable sharing Masonite flash messages

Official Masonite Documentation

New to Masonite ? Please first read the Official Documentation. Masonite strives to have extremely comprehensive documentation 😃. It would be wise to go through the tutorials there. If you find any discrepencies or anything that doesn't make sense, be sure to comment directly on the documentation to start a discussion!

Hop on Masonite Discord Community to ask any questions you need!

Installation

Requirements

To get started you will need the following:

  • Masonite 4.X (use inertia-masonite>=4.0) / Masonite 3.X(use inertia-masonite>=3.0) / Masonite 2.3 (use inertia-masonite>=2.X<3.0)
  • Laravel Mix installed (new Masonite projects come with this installed already)
  • a Node.js environment (npm or yarn)
pip install inertia-masonite

Install NPM dependencies

First we'll need to install some NPM packages (we are using Vue 3 here as frontend framework and inertia-vue3 as Inertia.js client-side adapter). You can find more info on this on Inertia.js documentation.

npm install vue @inertiajs/inertia @inertiajs/inertia-vue3

Configuration

Add InertiaProvider to your project in config/providers.py:

# config/providers.py
# ...
from masonite.inertia import InertiaProvider

# ...
PROVIDERS = [
    # ...

    # Third Party Providers
    InertiaProvider,
]

Inertia adapter comes with a middleware that will control some of the flow of data. Add InertiaMiddleware to your project in HttpKernel:

# AppHttpKernel.py

from masonite.inertia import InertiaMiddleware

class AppHttpKernel(HttpKernel):
    http_middleware = [InertiaMiddleware]
    route_middleware = {
      "web": [EncryptCookies, SessionMiddleware, VerifyCsrfToken]
    }

Make sure that this middleware is added in HTTP middlewares and that EncryptCookies middleware is added as a route middleware else you will get some issues with CSRF token validation as XSRF-TOKEN value won't be encrypted.

Finally if you want to change some parameters you can publish the package configuration file in your project:

python craft package:publish inertia

Congratulations! You have now setup Inertia in your project! For more information on how to use Inertia.js got to its documentation.

Getting started

This section quickly explains how to use Inertia.js with Masonite. For more details please read the documentation 📚.

How to use Inertia.js with Masonite adapter

We will create two routes and a controller which will load the two components scaffolded with previous command and see Inertia.js behaviour. In order to create Inertia response in our Controller, we are going to use newly available response Inertia. And that's it !

If you scaffolded the inertia demo you will already have the files, else:

python craft controller WelcomeController

This will create a controller WelcomeController but you can name it whatever you like. It would be good to keep the standard of whatever setup you have now for your home page. Then create two routes to that controller if you don't have them already:

ROUTES = [
    Route.get('/', 'WelcomeController@index'),
    Route.get('/helloworld', 'WelcomeController@helloworld')
]

And finally create the controller methods. We just need to use the new Inertia to render our controller.

# app/controllers/InertiaController.py
from masonite.inertia import Inertia

## ..
def inertia(self, view: Inertia):
    return view.render('Index')

def helloworld(self, view: Inertia):
  return view.render('HelloWorld')

## ..

This controller will render the view based on template templates/app.html and will load the Vue components into it depending on the route. Note that instead of specifying a Jinja template like we normally do we can just specify a page here. So since we have ../pages/Index.vue we specify to render Index here.

Test it !

Ok now we need to do 2 more commands. The first thing is to run npm run dev (at root) to compile all of this (with webpack mix):

npm run dev

Now we can run the server like we normally do:

python craft serve

When we go to our homepage we will see we see Index.vue component:

Home Page

Click on the link you can now see HelloWorld without page refresh !!!!

Contributing

Please read the Contributing Documentation here.

Maintainers

Thanks to Joseph Mancuso for making the PoC to couple Masonite and Inertia.js !

License

Masonite Inertia is open-sourced software licensed under the MIT license.

Metadata

Release files for inertia-masonite 4.2.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for inertia-masonite 4.2.11
File Size Uploaded Provenance
inertia-masonite-4.2.11.tar.gz 13.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for inertia-masonite 4.2.11
File Interpreter ABI Platform
inertia_masonite-4.2.11-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / inertia-masonite-4.2.11.tar.gz

Download URL inertia-masonite-4.2.11.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b33293242460994f5637f63854517b75e63683f3731f47822da6292123b3f841
BLAKE2b-256 checksum
How to use checksums
6892bd714a0c9c13aa6e81cdf22547de98905bd5e7f9b70aad1e4e4c90908a98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / inertia_masonite-4.2.11-py3-none-any.whl

Download URL inertia_masonite-4.2.11-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
16a68179325e1b7d93e8c569e4a8e37cbaa44e3f75f439e7c88b8c599cb1ea40
BLAKE2b-256 checksum
How to use checksums
79ef02e3dc4f4383919a1be28b5b36b235e23e2a275e5798252943e0a32530be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Security for inertia-masonite 4.2.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page