An Inertia.js adapter for Statamic.

Overview

Inertia.js adapter for Statamic

Statamic server side adapter for Inertia.js to build single-page apps, without building an API.

Latest Stable Version

Installation

You can install the package through Composer.

composer require hotmeteor/inertia-statamic

Usage

Setup

The Inertia adapter works for any page or entry content available through Statamic Collections.

By default, all Inertia-enabled pages will be expecting an app template, which should be located at resources/views/app.blade.php. This is the base page that any Inertia app is looking for, and should contain the @inertia directive. The template can be defined either at the collection or page level, but it must be app.

<!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>

In your app.js file you must set up your Inertia app and reference where your Vue pages will live:

// app.js

import { createApp, h } from 'vue'
import { App, plugin } from '@inertiajs/inertia-vue3'

const el = document.getElementById('app')

createApp({
  render: () => h(App, {
    initialPage: JSON.parse(el.dataset.page),
    resolveComponent: name => require(`./Pages/${name}`).default,
  })
}).use(plugin).mount(el)

Finally, you need to create a Pages folder in resources/js. This is where your app will be looking for Vue components that match the resolved naming of your Statamic pages.

|_ resources
    |_ js
        |_ Pages
            |_ About
                |_ Team.vue
            |_ Home.vue             

Both server-side setup and client-side setup full instructions are available on Inertia's website.

Component Naming

As you can see in the folder structure above, your Vue component naming and location must match the Statamic collection hierarchy + page slug combo for any Inertia-enabled pages. The adapter will automatically build these paths based on the page's URL and slug.

Here are some examples of what this looks like:

Statamic Collection Statamic Page Slug URL Component Name
Home Home home / Home.vue
Marketing Overview overview /marketing/ Marketing/Overview.vue
Marketing Logos and Colors logos-and-colors /marketing/logos Marketing/LogosAndColors.vue

Props

All the typical data passed to a Statamic page as objects will now be available to your page as props. The props will contain all of the expected attributes and data. For example, the Inertia response's props object could look like:

Inertia\Response {#2587 ▼
  #component: "Marketing/Overview"
  #props: array:22 [▼
    "amp_url" => null
    "api_url" => null
    "collection" => array:3 [▶]
    "content" => array:4 [▶]
    "date" => Illuminate\Support\Carbon @1617827556 {#2478 ▶}
    "edit_url" => "http://mysite.test/cp/collections/marketing/entries/f854a1cf-0dcf-404b-8418-a74662ba77e7/overview"
    "id" => "f854a1cf-0dcf-404b-8418-a74662ba77e7"
    "is_entry" => true
    "last_modified" => Illuminate\Support\Carbon @1617827556 {#2477 ▶}
    "mount" => null
    "order" => null
    "parent" => null
    "permalink" => "http://mysite.test/marketing"
    "private" => false
    "published" => true
    "slug" => "overview"
    "template" => "app"
    "title" => "Overview"
    "updated_at" => Illuminate\Support\Carbon @1617827556 {#2523 ▶}
    "updated_by" => array:4 [▶]
    "uri" => "/marketing"
    "url" => "/marketing"
  ]
  #rootView: "app"
  #version: ""
  #viewData: []
}

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
  • Use actual lookup so we don’t 404

    Use actual lookup so we don’t 404

    Closes #7

    Calling FrontController throws a 404 if the data doesn't exist. Calling the actual lookup is better.

    @hotmeteor sorry I didn't write a test, but the tests don't run (blueprint is missing). I can help get those going again, if you like.

    opened by edalzell 4
  • Update requirements

    Update requirements

    Please update inertiajs/inertia-laravel support:

    $ composer require hotmeteor/inertia-statamic
    Using version ^0.1.2 for hotmeteor/inertia-statamic
    ./composer.json has been updated
    Running composer update hotmeteor/inertia-statamic
    > Statamic\Console\Composer\Scripts::preUpdateCmd
    Loading composer repositories with package information
    Updating dependencies
    Your requirements could not be resolved to an installable set of packages.
    
      Problem 1
        - Root composer.json requires hotmeteor/inertia-statamic ^0.1.2 -> satisfiable by hotmeteor/inertia-statamic[v0.1.2].
        - hotmeteor/inertia-statamic v0.1.2 requires inertiajs/inertia-laravel ^0.4.1 -> found inertiajs/inertia-laravel[v0.4.1, ..., v0.4.5] but it conflicts with your root composer.json require (^0.6.3).
    
    Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
    You can also try re-running composer require with an explicit version constraint, e.g. "composer require hotmeteor/inertia-statamic:*" to figure out if any version is installable, or "composer require hotmeteor/inertia-statamic:^2.1" if you know which you need.
    
    Installation failed, reverting ./composer.json and ./composer.lock to their original content.
    
    opened by benjivm 2
  • Fix Tests

    Fix Tests

    Got the Statamic error sorted.

    However @hotmeteor, I had to change the expected view results as there's an extra <p> in what's returned and I don't know why.

    Also, the last test still fails cuz there's an extra newline in the results, which doesn't make sense to me.

    opened by edalzell 1
  • Non-Statamic routes throw a 404

    Non-Statamic routes throw a 404

    If you have custom, non-statamic routes, a 404 is thrown from the adapter.

    We found this when enabling OAuth for Statamic. Statamic then adds the /oauth/{provider} route, that throws a 404.

    PR incoming.

    opened by edalzell 0
  • Use template method

    Use template method

    Disclaimer - I did this all in the GitHub editor. I didn't actually test it, so you'll probably want to check it out.

    While I was doing https://github.com/statamic/cms/pull/5302 for Statamic 3.3, I went looking on GitHub for usages of augmentValue() and noticed this.

    1. On Statamic 3.2, if you had a field in your blueprint named template, $this->augmentedValue('template') would have been a Value instance, so this condition would always be false.
    2. On Statamic 3.3, even if you don't have a field named template, $this->augmentedValue('template') will be a Value instance, again making this condition always false.
    3. You can just use the template() method to get the plain template name.
    4. I swapped the instanceof check to first, so you can be sure the template() method will be available to use since you'll know it's a Page or Entry already.
    opened by jasonvarga 0
  • Does not load related data.

    Does not load related data.

    This seems to work pretty ok, but it does not load related data.

    So if I use the Entries field, then the entry is not resolved. That means I have to fetch it via the api_url, but this really fucks with SSR, since this is not loaded then.

    Is there a way to load all data in, not just what is on the given page?

    opened by addgod 0
  • Assets on S3

    Assets on S3

    Hi guys,

    Does anyone know how can I get metadata of images stored on S3?

    The props returned on the page gives me data like url, id, permalink, etc.. but no alt tag, width, height, etc...

    I had the same problem with forms and their fields and I solved it by making an AJAX call to the controller and pulling all the data I need by passing form handle and returning the data aj JSON.

    Do I need to do something similar with an image? Blade and antlers can somehow pull it out just by doing $image->alt or $image->height but that's not an option with Vue as the prop is just not there.

    Any ideas are welcome.

    Thank you

    opened by DenyEs 0
  • app.js name variable undefined

    app.js name variable undefined

    Problem description:

    const app = createApp({
        render: () =>
            h(App, {
                initialPage: JSON.parse(el.dataset.page),
                resolveComponent: name => pages[`./Pages/${name}.vue`].default
            }),
    });
    

    Error:

    Cannot read properties of undefined (reading 'default')

    I did some digging and found that the issue comes from InertiaStatamic.php

    public function handle(Request $request, Closure $next)
        {
            $page = app(FrontendController::class)->index($request);
          
            if ($page->augmentedValue('template') === 'app' && ($page instanceof Page || $page instanceof Entry)) {
                return Inertia::render(
                    $this->buildComponentPath($page),
                    $this->buildProps($page)
                );
            }
    
            return $next($request);
        }
    
    

    $page->augmentedValue('template') === 'app' => Always returns false

    Possible fix

    Got it working by adding raw()

    $page->augmentedValue('template')->raw() === 'app' => Returns true if app template exists.

    Thanks!

    opened by RalfHei 0
  • How to use with Vue 2?

    How to use with Vue 2?

    Hey,

    I've tried making this work with Vue 2 using the following code as per the Inertia documentation:

    import Vue from 'vue'
    import { createInertiaApp } from '@inertiajs/inertia-vue'
    
    createInertiaApp({
      resolve: name => require(`./Pages/${name}`),
      setup({ el, App, props }) {
        new Vue({
          render: h => h(App, props),
        }).$mount(el)
      },
    })
    

    But I'm seeing the following console error:

    Uncaught (in promise) Error: Cannot find module './undefined'
    

    I'm assuming this could be an issue where the statamic/inertia adapter isn't communicating the path for the components?

    Thanks!

    opened by luke-parallax 4
  • Undefined variable $page

    Undefined variable $page

    I'm trying to implement this connector (v0.1.2) in my Statamic v3.1.23 site and I'm getting this undefined variable $page error.

    When troubleshooting, I've discovered it's happening due to $page->augmentedValue('template') === 'app' being false (in InertiaStatamic.php), so the Inertia render isn't triggered. If I note out this conditional statement, then the page seems to load!

    From what I can tell now, there's no template value being augmented anymore that indicates which template is rendering the requested path?

    This is definitely confusing...

    Help?

    Thanks so much!

    opened by skylennard 8
Releases(v0.2.1)
  • v0.2.1(Oct 19, 2022)

    What's Changed

    • Use actual lookup so we don’t 404 by @edalzell in https://github.com/hotmeteor/inertia-statamic/pull/8

    New Contributors

    • @edalzell made their first contribution in https://github.com/hotmeteor/inertia-statamic/pull/8

    Full Changelog: https://github.com/hotmeteor/inertia-statamic/compare/v0.2...v0.2.1

    Source code(tar.gz)
    Source code(zip)
  • v0.2(Oct 15, 2022)

    What's Changed

    • Use template method by @jasonvarga in https://github.com/hotmeteor/inertia-statamic/pull/4
    • Update dependencies by @hotmeteor in https://github.com/hotmeteor/inertia-statamic/pull/6

    New Contributors

    • @jasonvarga made their first contribution in https://github.com/hotmeteor/inertia-statamic/pull/4
    • @hotmeteor made their first contribution in https://github.com/hotmeteor/inertia-statamic/pull/6

    Full Changelog: https://github.com/hotmeteor/inertia-statamic/compare/v0.1.2...v0.2

    Source code(tar.gz)
    Source code(zip)
Owner
Adam Campbell
Adam Campbell
A widget that displays a Bible verse every day in the Statamic dashboard

Statamic Widget: Verse of the Day What is it A widget that displays a Bible verse every day in the Statamic dashboard. How to install it Install via c

Michael 2 Jan 27, 2022
Spin up a working Statamic instance quickly & easily with Docker

Spin Up Statamic Allows you to create your own self-contained Statamic project complete site config, Antlers/Blade/Twig template files, assets, and de

nystudio107 11 Jun 2, 2023
A Phalcon paginator adapter for Phalcon Collections

Phalcon Collection Paginator A Phalcon paginator adapter for Phalcon Collections Why create this? Being familiar with the various Pagination data adap

Angel S. Moreno 2 Oct 7, 2022
Doctrine adapter for SlmQueue module

SlmQueueDoctrine Created by Stefan Kleff Requirements SlmQueue Doctrine 2 ORM Module or roave/psr-container-doctrine Note: it's necessary require the

Webador 32 May 13, 2022
A simple twitter-feed-style RSS aggregator written in PHP, Laravel, Inertia.js, Tailwind and Vue.js

RSS A simple, opinionated, RSS feed aggregator. Features The following features are built into the application: Supports RSS and ATOM formats. Regular

Dan Brown 257 Dec 24, 2022
A package that adds view-composer like feature to Inertia.js Laravel adapter

Kinetic A package that adds view-composer like feature to Inertia.js Laravel adapter. Use to be able to share props based on the Inertia component nam

Marvin Quezon 76 Dec 12, 2022
Statamic 3 is the flat-first, Laravel + Git powered CMS designed for building beautiful, easy to manage websites.

About Statamic 3 Statamic 3 is the flat-first, Laravel + Git powered CMS designed for building beautiful, easy to manage websites. Note: This reposito

Statamic 2.4k Jan 5, 2023
This Statamic addon allows you to modify the tags rendered by the Bard fieldtype, giving you full control over the final HTML.

Bard Mutator This Statamic addon allows you to modify the tags rendered by the Bard fieldtype, giving you full control over the final HTML. You can ad

Jack Sleight 10 Sep 26, 2022
A Statamic Pro addon that provides alternative GraphQL queries for collections, entries and global sets.

Statamic Enhanced GraphQL A Statamic CMS GraphQL Addon that provides alternative GraphQL queries for collections, entries and global sets. ⚠️ This is

Grischa Erbe 2 Dec 7, 2021
The official Statamic 3 static site generator package

Statamic Static Site Generator Generate static sites with Statamic 3. Installation Install the package using Composer: composer require statamic/ssg

Statamic 187 Dec 25, 2022
A widget that displays a Bible verse every day in the Statamic dashboard

Statamic Widget: Verse of the Day What is it A widget that displays a Bible verse every day in the Statamic dashboard. How to install it Install via c

Michael 2 Jan 27, 2022
Statamic 3 - the flat-first, Laravel + Git powered CMS designed for building beautiful, easy to manage websites

Statamic 3 - the flat-first, Laravel + Git powered CMS designed for building beautiful, easy to manage websites

Statamic 600 Jan 4, 2023
Spin up a working Statamic instance quickly & easily with Docker

Spin Up Statamic Allows you to create your own self-contained Statamic project complete site config, Antlers/Blade/Twig template files, assets, and de

nystudio107 11 Jun 2, 2023
⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

Erik C. Forés 31 Dec 18, 2022
PSR-15 Adapter for InertiaJS

inertia-psr15 Before using this library, is important to know what is Inertia.js, what is it for and how it works, in the official Inertia.js website

Mohamed Cherif Bouchelaghem 28 Oct 26, 2022
Flysystem adapter for Google Cloud Storage

Flysystem adapter for Google Cloud Storage This package contains a Google Cloud Storage driver for Flysystem. Notice This package is a fork from super

Spatie 21 May 1, 2022
Database adapter for data interfaces

Yii _____ The package ... Requirements PHP 7.4 or higher. Installation The package could be installed with composer: composer require yiisoft/_____ --

Yii Software 8 Oct 15, 2022
Flysystem V2 adapter for the webman

Flysystem V2 adapter for the webman

null 0 Nov 3, 2021
Microsoft OneDrive adapter for Flysystem 2+

Lexik Flysystem OneDrive Adapter This is a Flysystem 2+ adapter to interact with Microsoft OneDrive API. Setup In your Microsoft Azure portal create a

Choosit 3 Nov 19, 2021
💾 Flysystem adapter for the GitHub storage.

Flysystem Github Requirement PHP >= 7.2 CDN List 'github' => "https://raw.githubusercontent.com/:username/:repository/:branch/:fullfilename", 'fastg

wangzhiqiang 2 Mar 18, 2022