Server-side Masonite adapter for Inertia.js
Project description
Masonite Inertia
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.
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 2.3+
- Laravel Mix installed (new Masonite 2.3 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):
$ 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 config/middleware.py:
# config/middleware.py
# ...
from masonite.inertia import InertiaMiddleware
# ...
HTTP_MIDDLEWARE = [
LoadUserMiddleware,
CsrfMiddleware,
#...
InertiaMiddleware,
]
Then install OR publish the reqired package files (configuration, views ...):
python craft inertia:install
python craft publish InertiaProvider
Scaffold a base Vue app and a template (optional) Then, if you want you can quicky scaffold a Vue app with two components to test Inertia behaviour by running the publish command :
python craft publish InertiaProvider --tag app
Usage
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 InertiaResponse. And that's it !
We can quickly create this demo (routes & controller) with the publish command :
$ python craft publish InertiaProvider --tag demo
or you can create it manually:
$ craft controller InertiaController
This will create a controller InertiaController 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 = [
Get('/', 'InertiaController@index'),
Get('/helloworld', 'InertiaController@helloworld')
]
And finally create the controller methods. We just need to use the new InertiaResponse to render our controller.
# app/controllers/InertiaController.py
from masonite.inertia import InertiaResponse
## ..
def inertia(self, view: InertiaResponse):
return view.render('Index')
def helloworld(self, view: InertiaResponse):
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:
$ 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 !!!!
Congratulations! You have now setup Inertia in our project! For more information on how to use Inertia.js got to its documentation.
Contributing
Please read the Contributing Documentation here.
License
Masonite Inertia is open-sourced software licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file masonite-inertia-0.1.0.tar.gz.
File metadata
- Download URL: masonite-inertia-0.1.0.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97ac8f9862ebfb931cade66b8b27f1deaa33329bbbca01b14f6694dfedddaafb
|
|
| MD5 |
8edab12c52de38af7b99a67f99daef36
|
|
| BLAKE2b-256 |
c859b5b394f56c245bafee759d5d64584031e97e81937abebde28181e5a4c25b
|
File details
Details for the file masonite_inertia-0.1.0-py3-none-any.whl.
File metadata
- Download URL: masonite_inertia-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23646a1792d073e53c5d1868ad3bf7ebea25eaf73603ffb0f638f438df14eaf0
|
|
| MD5 |
0c60c7caa9fb40050633492eaa5946eb
|
|
| BLAKE2b-256 |
1b1d74263cc5a2ee850aa5737b46f5ecaccd2290be95e136a7f5b131d8ee81ca
|