Skip to main content

Server-side Masonite adapter for Inertia.js

Project description

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).

This package is compatible with both Masonite 2, 3 and 4 see below.

Browse documentation 📚.

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!

Also be sure to join the Slack channel!

Installation

Requirements

To get started you will need the following:

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

Install NPM dependencies

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

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

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]

Finally publish the package configuration (to get config/inertia.py) to your project:

python craft install: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:

$ 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.

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

masonite-inertia-4.0.0.tar.gz (267.3 kB view details)

Uploaded Source

Built Distribution

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

masonite_inertia-4.0.0-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file masonite-inertia-4.0.0.tar.gz.

File metadata

  • Download URL: masonite-inertia-4.0.0.tar.gz
  • Upload date:
  • Size: 267.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for masonite-inertia-4.0.0.tar.gz
Algorithm Hash digest
SHA256 a4f0454e7da3b2b5308ae5699923228ebb4dd1f0d5a96eed20da6a26d0f68b28
MD5 19d6ae1cd8239a282aa07b01e4f7a4d5
BLAKE2b-256 5939a581bc9a38c541d404c852f6b636634237f1dc947f758ff4934274d36a54

See more details on using hashes here.

File details

Details for the file masonite_inertia-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: masonite_inertia-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for masonite_inertia-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd9b2ad6d0914e0533e0b5495b0e0a85ddc01666059a52112e6375f989431253
MD5 ceab3f297fbc2eade07f45ea467564f1
BLAKE2b-256 9d93362a744bfe21a66e06d256a49b7f929727f5da9b5b9ac788ba1e69e6701b

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