Introducing Inertia.js

Introducing Inertia.js

Introduction to Inertia.js, we'll look at Inertia's viability in the nearest future, the advantage it will have, and how to use it in a Laravel and Vue project. What is Inertia.js? Inertia is a library that combines the best of both server-side rendering (SSR) and client-side rendering (CSR) by allowing developers to build SPAs using server-side routing and controllers.

Building web applications can be a very daunting process. You have to think about if it will be a traditional server-side rendered app(SSR) or a single page application(SPA) before proceeding to pick from the many frameworks and libraries.

While both server-side and client-side rendering have their pros and cons, Inertia combines the best of both worlds.

What is Inertia.js?

Inertia is a library that combines the best of both server-side rendering (SSR) and client-side rendering (CSR) by allowing developers to build SPAs using server-side routing and controllers.

According to its official documentation:

Inertia is a new approach to building classic server-driven web apps. We call it the modern monolith. 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.

Some might be asking is this another JavaScript framework? The documentation has this to say:

Inertia isn’t a framework, nor is it a replacement to your existing server-side or client-side frameworks. Rather, it’s designed to work with them. Think of Inertia as glue that connects the two.

The problem Inertia.js solves

Inertia solves many problems developers face when building modern applications. Problems like:

  • SPA complexities — building modern SPAs comes with some hassles. Developers have always had problems with effectively managing the state for the app, figuring routing for the app, setting up navigation guards, and authentication
  • Building APIs — With Inertia.js you don’t need to build a REST or GraphQL API, because Inertia was created to work out of the box with your classic server-side frameworks such as Laravel, Ruby on Rails, or Django
  • Browser issues — Inertia has built-in services that solve browser issues faced when building SPAs, issues such as:
    • Browser history management and scroll position — it provides remember , preserveState, and preserveScroll properties to cache local component states
    • Loading indication — because Inertia requests are made via AJAX calls there’s no default browser loading indicator, so Inertia includes NProgress.js, a progress bar library. The loading indicator is only shown if a request takes longer than 250m
    • Assets reloading and versioning — Inertia provides an option to track and store the current version of assets on your website
Why should you use Inertia.js?

GraphQL benefits without using GraphQL

Inertia gives full access to run specific queries on a database to get the data needed for a page while using your server-side ORM as a data source.

Limited AJAX calls

In traditional SPAs, AJAX calls are made on every page visit to fetch data. In Inertia, an AJAX call is made to boot up the app then it maintains a persistent Vue.js instance and every subsequent page visits are made via XHR with a special X-Inertia header set to true. This triggers the server sending an Inertia response as JSON rather than making a full-page visit.

It also creates a fail-safe component that wraps around a standard anchor link, it intercepts click events and prevents full page reloads from occurring.

Security

When building API-powered apps, we have to add CORS support to our app to be able to access resources on other origins.

With Inertia you don’t have to worry about setting up CORS since your data is provided via your controllers and housed on the same domain as your JavaScript components.

You can set up authorization on the server-side and perform authorization checks bypassing tokens as props to your page components, this helps reduce the risk of exposing important information because handling authorizations on the client can put one at the risk of an XSS attack (cross-site scripting).

Framework agnostic

Inertia is both server-side and client-side framework agnostic. You can use Inertia with any server-side framework as well as any client-side framework that supports dynamic components.

Inertia adapters are services(packages) that help make Inertia work well with specific frameworks, Official adapter support is currently limited to Rails, Laravel on the backend, and React, Vue.js, Svelte on the frontend.

There are unofficial adapters for some other frameworks such as Symfony, Django, CakePHP, and Adonis.

Is there a future for Inertia?

The web is forever evolving and we’ve seen a transition from traditional server-side built monolith apps to API-powered apps. With this current trend is there a future for Inertia?

Of course, the answer to the question depends on the use case and preferences.

Inertia is built for people who want to build monolith applications — they generally prefer the tight coupling between their controllers and their views, but also want to build their apps using modern client-side frameworks. A majority of developers still fall into this category but with the rise and industry support for API-powered apps, we might see its usage dwindle.

Of course, there are times when using Inertia might not be the best fit. Situations such as when you need multi-client support, customer-facing/marketing pages, and SEO driven websites. Using Inertia for these is probably not a good idea. But it is perfectly useful to build web apps that power dashboards and the likes.

Is server-side rendering possible with Inertia?

Inertia does not currently support server-side rendering but there are tools to pre-render Inertia websites, they generate and cache static HTML versions of specific routes of your websites, and then serve that content.

Get started with using Inertia.js in your project

Prerequisites

This installation process makes use of Laravel for the server-side and Vue.js for the client-side, the following is required to follow along with this section:

Create a new Laravel project:

laravel new inertia-example

Or create with composer:

composer create-project --prefer-dist laravel/laravel inertia-example

cd into the project:

$ cd inertia-example

Install Inertia’s server-side adapter using composer:

composer require inertiajs/inertia-laravel

Rename the welcome.blade.php file found in your resources/views folder to app.blade.php.

Replace the content of your app.blade.php with this:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <link href="{{ mix('/css/app.css') }}" rel="stylesheet" />
    <script src="{{ mix('/js/app.js') }}" defer></script>
  </head>
  <body>
    @inertia
  </body>
</html>

The @inertia directive is a helper that creates a base div with an id of app that contains the page information, it tells Laravel that the views are generated using Inertia.

Next, set up the client-side adapter by running this command in your terminal:

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

#or, Using Yarn

yarn add @inertiajs/inertia @inertiajs/inertia-vue

Open your app.js file found in resources/js and replace the content of your app.js file with the following:

import { InertiaApp } from '@inertiajs/inertia-vue'
import Vue from 'vue'

Vue.use(InertiaApp)

const app = document.getElementById('app')

new Vue({
  render: h => h(InertiaApp, {
    props: {
      initialPage: JSON.parse(app.dataset.page),
      resolveComponent: name => require(`./Pages/${name}`).default,
    },
  }),
}).$mount(app)

The resolveComponent callback tells Inertia how to load a page component. It receives a string as a page name and returns a page instance.

Dynamic imports

To enable code-splitting we use a babel plugin for dynamic imports.

First, install it by running this command:

npm install @babel/plugin-syntax-dynamic-import
#or, Using Yarn
yarn add install @babel/plugin-syntax-dynamic-import

Next, create a .babelrc file in your projects root directory with the following:

{
  "plugins": ["@babel/plugin-syntax-dynamic-import"]
}

Finally, update the resolveComponent callback in your app initialization to use import instead of require. The callback returns a promise that includes a component instance, like this:

......

new Vue({
  render: h => h(InertiaApp, {
    props: {
      initialPage: JSON.parse(app.dataset.page),
      resolveComponent: name => import(`./Pages/${name}`).then(module => module.default),
    },
  }),
}).$mount(app)
Conclusion

Inertia is a great library for building “hybrid” SPAs. In this article, we’ve looked at its viability in the nearest future, the advantage it has, and how to use it in a Laravel and Vue project.

Checkout Inertia on Github and this article written by Jonathan Reinink to learn more. The official documentation is also well written and is an excellent resource to get started with.

Top Vue.js Developers in USA

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

We, at HireFullStackDeveloperIndia, implement the right strategic approach to offer a wide variety through customized Vue.js development services to suit your requirements at most competitive prices.

Vue.js is an open-source JavaScript framework that is incredibly progressive and adoptive and majorly used to build a breathtaking user interface. Vue.js is efficient to create advanced web page applications.

Vue.js gets its strength from the flexible JavaScript library to build an enthralling user interface. As the core of Vue.js is concentrated which provides a variety of interactive components for the web and gives real-time implementation. It gives freedom to developers by giving fluidity and eases the integration process with existing projects and other libraries that enables to structure of a highly customizable application.

Vue.js is a scalable framework with a robust in-build stack that can extend itself to operate apps of any proportion. Moreover, vue.js is the best framework to seamlessly create astonishing single-page applications.

Our Vue.js developers have gained tremendous expertise by delivering services to clients worldwide over multiple industries in the area of front-end development. Our adept developers are experts in Vue development and can provide the best value-added user interfaces and web apps.

We assure our clients to have a prime user interface that reaches end-users and target the audience with the exceptional user experience across a variety of devices and platforms. Our expert team of developers serves your business to move ahead on the path of success, where your enterprise can have an advantage over others.

Here are some key benefits that you can avail when you decide to hire vue.js developers in USA from HireFullStackDeveloperIndia:

  • A team of Vue.js developers of your choice
  • 100% guaranteed client satisfaction
  • Integrity and Transparency
  • Free no-obligation quote
  • Portal development solutions
  • Interactive Dashboards over a wide array of devices
  • Vue.js music and video streaming apps
  • Flexible engagement model
  • A free project manager with your team
  • 24*7 communication with your preferred means

If you are looking to hire React Native developers in USA, then choosing HireFullStackDeveloperIndia would be the best as we offer some of the best talents when it comes to Vue.js.

Inertia.js for Laravel & Vue - Server Driven Single Page Apps

Inertia.js for Laravel & Vue - Server Driven Single Page Apps

We take a look at the basics of Inertia.js and build out a simple CRUD application in Laravel and Vue. What is Inertia.js? Inertia is a library that combines the best of both server-side rendering (SSR) and client-side rendering (CSR) by allowing developers to build SPAs using server-side routing and controllers.

Inertia.js is a framework created by Jonathan Reinink for creating server-driven single page apps. It combines the best parts of building SPAs, while keeping the conveniences of server-driven apps. For a typical Laravel and Vue app, Inertia replaces all your blade templates with Vue Single File components allowing your application to be more interactive.

We take a look at the basics of Inertia.js and build out a simple CRUD application in Laravel and Vue.

Inertia GitHub: https://github.com/inertiajs
GitHub Repo for this example: https://github.com/drehimself/inertia-example

Top Web Application Developer

Top Web Application Developer

You can also contact a web application development company for your business but then why not to contact the best web application development company that can turn up your business and customer satisfaction ratio to sky touching heights.

Not long-ago internet came into existence and the world has never been the same ever since. The Internet made sure that people and business do evolve at a faster rate than ever and was never merciful to the slow ones. Because of this competition and availability of any business with few clicks made India one of the hubs as IT center. This trend of constantly updating has given rise to smartphones, smart machines, wearable gadgets and a lot more is yet to come in the upcoming years. In such time it is always a good idea to hire Web App Developer from India at your service. They are expert in developing not only websites but web applications as well.

We at HireFullStackDeveloperIndia, have a huge team of experienced developers that have grasped over different domains in front and back end development. You can hire web app developers in India from us with many advantages that you won’t get anywhere else in the industry.

Here is why we are the best option if you are looking forward to hiring web app developers in India:

  • Flexible hiring models available, as per your convenience and requirement
  • Maximum ROI, compared to any other company or team of developers.
  • We provide you with Source code Authorization meaning code written for you belong only to you. It cannot be used or copied anywhere else.
  • All of our developers are sound with agile development methodology, so you will be in a constant loop of suggestions, ideas, trends, and updates about your project.
  • Our developers are good with creating custom web applications as well; it guarantees you a better product without any compromise of non-existing functionalities.
  • You can save huge costing on infrastructure by utilizing our hire web app developer in India program.
  • Your hired developer or team will be easily accessible to your preferred mode of communication.
  • You get to exercise complete control over your team or individual that you hire.
  • We believe in Integrity and Transparency.
  • Our developers are highly creative and motivated to deliver excellence.


HireFullStackDeveloperIndia is a Web Application Development Company in India that is known worldwide for our Innovative guaranteed solutions. You can inquire with us about your project and we will be providing you multiple suitable developers that are the best fit for your requirements. You can evaluate them and select one or multiple whosoever deems fit to you. After this, all you have to do is provide your valuable input to the resource through sprint base project development until you get delivery of your project.

Our engagement model will also allow you to get our development team to your site location and proceed with development from your premises.