Skip to content

Instantly share code, notes, and snippets.

@ahgumasing
Last active April 15, 2020 21:07
Show Gist options
  • Save ahgumasing/506a40a24c59b9fed1412e156965ad28 to your computer and use it in GitHub Desktop.
Save ahgumasing/506a40a24c59b9fed1412e156965ad28 to your computer and use it in GitHub Desktop.
Inertia-Vue-installation
  1. Install Inertia:
# Server side adapter
composer require inertiajs/inertia-laravel

# Install Vue.js && client side adapter for Vue.js
npm install vue @inertiajs/inertia @inertiajs/inertia-vue
  1. Add Inertia's root view (app.blade.php)
touch resources/views/app.blade.php

Put this in the file:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>{{ config('app.name') }}</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Poppins:400,700" rel="stylesheet">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                font-family: 'Poppins', sans-serif;
                margin: 0;
            }
        </style>

        <link href="{{ mix('css/app.css') }}" rel="stylesheet">
        <script src="{{ mix('js/app.js') }}" defer></script>
        @routes
    </head>

    <body>
        @inertia
    </body>
</html>
  1. Boot Inertia from app.js
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)
  1. Create some pages & register some routes

Create the files

mkdir resources/js/{Pages,Shared}
touch resources/js/Shared/Layout.vue
touch resources/js/Pages/Home.vue
touch resources/js/Pages/About.vue

Put this in Layout.vue

<template>
    <div>
        <header>
            <inertia-link href="/">Home</inertia-link>
            <inertia-link href="/about">About</inertia-link>
        </header>
        <slot></slot>
    </div>
</template>

<script>
export default {}
</script>

Put this in Home.vue:

<template>
    <h1>Homepage</h1>
</template>

<script>
import Layout from '../Shared/Layout'

export default {
    layout: Layout
}
</script>

Put this in About.vue:

<template>
    <h1>About</h1>
</template>

<script>
import Layout from '../Shared/Layout'

export default {
    layout: Layout
}
</script>

Register the routes (web.php):

use Inertia\Inertia;

Route::get('/', fn () => Inertia::render('Home'));
Route::get('/about', fn () => Inertia::render('About'));
  1. Install Tailwindcss
# Install the dependencies
npm install --dev tailwindcss postcss-nesting postcss-import

# Generate the config file
npx tailwind init

Setup purgeCSS for smaller styles

npm install --dev @fullhuman/postcss-purgecss

Put this in webpack.mix.js:

const cssImport = require('postcss-import')
const cssNesting = require('postcss-nesting')
const mix = require('laravel-mix')
const path = require('path')
const purgecss = require('@fullhuman/postcss-purgecss')
const tailwindcss = require('tailwindcss')

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css/app.css')
    .options({
        postCss: [
            cssImport(),
            cssNesting(),
            tailwindcss('tailwind.config.js'),
            ...mix.inProduction() ? [
                purgecss({
                    content: ['./resources/views/**/*.blade.php', './resources/js/**/*.vue'],
                    defaultExtractor: content => content.match(/[\w-/:.]+(?<!:)/g) || [],
                    whitelistPatternsChildren: [/nprogress/],
                }),
            ] : [],
        ],
    })
    .webpackConfig({
        output: { chunkFilename: 'js/[name].js?id=[chunkhash]' },
        resolve: {
            alias: {
                'styles': path.resolve('resources/css'),
                '@': path.resolve('resources/js'),
            },
        },
    })
    .version()
    .sourceMaps()

Put this inside resources/css/app.css:

@tailwind base;
@tailwind components;
@tailwind utilities;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment