FORGEBOX Enterprise 🚀 - Take your ColdFusion (CFML) Development to Modern Times! Learn More...

cbInertia

v1.2.0 Public

cbInertia

Master Branch Build Status

The ColdBox adapter for Inertia.js

What is Inertia.js?

Inertia.js lets you, in its own words, "quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers." Using Inertia.js is like using your favorite MVC server-side framework (ColdBox, of course) with Vue as the templating language - no need to build a separate API. You can learn all about Inertia.js on their website.

What is this module for?

This module provides the needed lifecycle and ajax responses to integrate Inertia.js into a ColdBox app on the server. It will let you render Inertia responses, both for the initial page visit and subsequent client-side transitions, as well as share data on every Inertia request.

Requirements, Installation, and Configuration

cbInertia requires ColdFusion 2016+ or Lucee 5+ and ColdBox 5.6.0+.

Install cbInertia into your ColdBox project using CommandBox.

box install cbInertia

By default, no configuration is needed. However, you can disable the automatic registering of the InertiaLifecycle interceptor and/or the cbInertia application helpers, if you wish.

// config/ColdBox.cfc
moduleSettings = {
    "cbInertia": {
        "autoRegisterInterceptor": true,
        "autoRegisterHelpers": true
    }
}

Usage

You use cbInertia from your handlers/controllers. You can either inject the Inertia model using the [email protected] dsl or you can use the automatically loaded (by default) helper: inertia(). We'll cover both use cases, but the choice is just a matter of style preference.

Once available in your handler, you render an Inertia route using the render method.

render

NameTypeRequiredDefaultDescription
componentstringtrue The component name to render.
propsstructfalse{}Any props to inject in to the component. Any props passed will be serialized to json.
// handlers/Users.cfc
component {

    property name="inertia" inject="@cbInertia";

    function index( event, rc, prc ) {
        inertia.render( "Users/Index", {
            "users" = [ /* ... */ ]
        } );
    }

}

You can get an instance of Inertia by calling the inertia() helper function.

// handlers/Users.cfc
component {

    function index( event, rc, prc ) {
        inertia().render( "Users/Index", {
            "users" = [ /* ... */ ]
        } );
    }

}

Since render is the most common method called, you can pass the arguments to render directly to the inertia() helper function.

// handlers/Users.cfc
component {

    function index( event, rc, prc ) {
        inertia( "Users/Index", {
            "users" = [ /* ... */ ]
        } );
    }

}

The other Inertia method you will use is the share method:

share

NameTypeRequiredDefaultDescription
keystringtrue The key name for the shared property.
valueanytrue The shared property value. This can also accept a closure. Closures will be evaluated just prior to rendering.

The share method is used for values that should be available on every Inertia render. A common example of this is the authenticated user (or null if there is none). You can set up an interceptor to share your global values, and they will be available on every Inertia render as a prop.

inertia.share( "user", function() {
    return auth.check() ? // auth == cbauth
        auth.user().loadRelationship( "account" ).getMemento() :
        javacast( "null", "" );
} );

You can call share as many times as you need.

Code Splitting and ColdBox Elixir Integration

Using Pages creates a natural code splitting point. You can reduce the javascript your users have to initially download and speed up your site by only delivering the javascript for the page they are requesting. You can do this with ColdBox Elixir.

Here is an example client-side boilerplate for Vue.js

import Vue from "vue";
import VueMeta from "vue-meta";
import PortalVue from "portal-vue";
import { InertiaApp } from "@inertiajs/inertia-vue";

Vue.config.productionTip = false;
Vue.use(InertiaApp);
Vue.use(PortalVue);
Vue.use(VueMeta);

let app = document.getElementById("app");

new Vue({
  metaInfo: {
    title: "Loading...",
    titleTemplate: "%s | My Inertia.js App"
  },
  render: h =>
    h(InertiaApp, {
      props: {
        initialPage: JSON.parse(app.dataset.page),
        resolveComponent: name =>
          import(
            /* webpackChunkName: "includes/js/pages/[request]" */ `@/Pages/${name}`
          ).then(module => module.default)
      }
    })
}).$mount(app);

The webpackChunkName will create each of the pages in a includes/js/pages directory and use the current chunk name in place of [request].

Additionally, you need to add a bit of Webpack config to combine the shared parts of your pages together. For instance, if two pages each import a Button component you have, you want a shared module for those components.

The configuration sample here will pull out all shared components from your Pages directory into one file. You can tweak the configuration if your needs are different:

const elixir = require("coldbox-elixir");

elixir.config.mergeConfig({
  optimization: {
    splitChunks: {
      cacheGroups: {
        shared: {
          chunks: "async",
          minChunks: 2,
          name: "includes/js/pages/shared"
        }
      }
    }
  }
});

module.exports = elixir(mix => {
  mix.css("app.css");
  mix.vue("app.js");
});

v1.2.0

31 Dec 2019 — 17:03: 59 UTC

chore

  • Tests: Re-enable getFullUrl expectations (474ce0b)

feat

  • ModuleConfig: Add toggles for automatic registering of interceptors and helpers (7ccb0f9)

other

  • *: docs: Document cbInertia (384c287)

v1.1.0

24 Dec 2019 — 05:38: 10 UTC

feat

  • Props: Resolve closures in props before rendering (7ed313e)

v1.0.0

16 Aug 2019 — 20:28: 55 UTC

other

  • *: feat: Initial commit (24feb4e)
  • *: build: Enable commandbox-semantic-release (b9cb566)
  • *: Initial commit (1ca8877)

Here are all the versions for this package. Please note that you can leverage CommandBox package versioning to install any package you like. Please refer to our managing package version guide for more information.

Version Created Last Update Published By Stable Actions
Current
1.2.0 Dec 31 2019 11:04 AM Dec 31 2019 11:04 AM
Version History
1.1.0 Dec 23 2019 11:38 PM Dec 23 2019 11:38 PM
1.0.0 Aug 16 2019 03:28 PM Aug 16 2019 03:28 PM

 

$ box install cbInertia

No collaborators yet.
     
  • Aug 16 2019 03:28 PM
  • Dec 31 2019 11:04 AM
  • 195
  • 0
  • 6