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 Gist contains the adapter for the Masonite framework.
To get started you will need the following:
- Masonite 2.3+
- Laravel Mix installed (new Masonite 2.3 projects come with this installed already)
- NPM
First we'll need to install some NPM packages:
$ npm install vue @inertiajs/inertia @inertiajs/inertia-vue
First we need to move the contents of some files in this Gist into our Masonite app.
Some files I put in app/inertia
directory and the provider should go in the app/providers
directory.
So we will then have a directory that looks like this:
app/
http/
inertia/
InertiaAssetVersion.py
InertiaMiddleware.py
InertiaResponse.py
providers/
InertiaProvider.py
bootstrap/
This Inertia adapter comes with a middleware that will control some of the flow of data. We can add the middleware to our config/middleware.py
file like so:
# config/middleware.py
# ...
from app.inertia.InertiaMiddleware import InertiaMiddleware
# ...
HTTP_MIDDLEWARE = [
LoadUserMiddleware,
CsrfMiddleware,
#...
InertiaMiddleware,
]
At this point, Masonite is pretty much setup now. What we need to do is create a template file which will work as the
bases of our SPA. See the app.html
file in this gist.
See this structure for an example of where to put it:
app/
bootstrap/
resources/
templates/
app.html
Next we need to add the app.js
file you find here in this Gist. Simply copy it from here into your storage/static/js/app.js
file:
As well as copy the mix-manifest.json
file to your storage/static
directory.
app/
storage/
static/
js/
app.js
mix-manifest.json
We also need to add a new configuration file to our project as well. You can find the contents in this resources.py
file in this Gist:
app/
config/
resources.py
Finally we just need to use the new InertiaResponse
to render our controller. So let's make a new route and controller:
We will call our controller the 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.
$ craft controller Welcome
Then create a route to that controller if you don't have one already:
ROUTES = [
Get('/', 'WelcomeController@inertia')
]
And finally create the inertia
method:
from app.inertia.InertiaResponse import InertiaResponse
def inertia(self, view: InertiaResponse):
return view.render('Index')
Ok now we need to do 2 more commands. The first thing is to run npm run dev
to compile all of this:
$ 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:
Home Page
Congratulations! We have now setup Inertia in our project! Refer to the InertiaJS Documentation
To get the SPA part of our application working we can use a special link.
We'll go a little bit further and show how to setup our second link:
ROUTES = [
# ..
Get('/helloworld', 'WelcomeController@helloworld'),
]
Go back to our WelcomeController and add a new helloworld
method. Remember we made we have a new page in our storage/static/js/Pages/HelloWorld.vue
so that is another component. Instead of specifying a jinja template like we normally do we can just specify a page here. So since we have ../Pages/HelloWorld.vue
we specify to render HelloWorld
here.
def helloworld(self, view: InertiaResponse):
return view.render('HelloWorld')
Now we can modify our Index.vue
page to go to our new page by using the <inertia-link>
component.
<template>
<div>
Home Page.
<inertia-link href="/helloworld">
Hello World
</inertia-link>
</div>
</template>
<script>
export default {
name: "Index",
}
</script>
Notice instead of an a
tag we have an inertia-link
tag instead. At this point you may want to run
$ npm run watch
This command will recompile when you change your Vue Pages.
Now refresh your browser (if you don't see changes you may need to hard refresh).
You should now see something like:
Home Page. Hello World
When you click on the Hello World link you will be directed to your new hello world page all without a page load.
You now have the full power of an SPA.