GithubHelp home page GithubHelp logo

macagoraga / vue-pjax-adapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from riverskies/vue-pjax-adapter

0.0 2.0 0.0 448 KB

A Vue plugin to enhance your website performance with PJAX without relying on jQuery.

JavaScript 86.54% Vue 13.46%

vue-pjax-adapter's Introduction

PJAX adapter for Vue

This Vue plugin was inspired by defunkt/jquery-pjax and depends on spatie/laravel-pjax.

Those people who use VueJS extensively in their (non-SPA) projects, PJAX could be lifesaving when optimising the user experience. Simply put, PJAX uses AJAX requests to manipulate the DOM at time of page changes thus reducing the load over the network and therefore improving response times.

Dependencies

The plugin itself depends on the vue and axios libraries and has been built to consume the PJAX server responses provided by the spatie/laravel-pjax package.

Installation

First things first, open up your terminal and pull in the plugin.

$ npm install riverskies/vue-pjax-adapter

Next, you will have to specify the target container in your blade template. In the example below we called it #pjax-container (which is the default by the way).

<div id="app">
    @include('partials.header')

    <div>
        @include('partials.navigation')

        <main id="pjax-container">
            @yield('content')
        </main>
    </div>
</div>

Now go to your app.js file where you set up Vue and initialise the plugin.

require('./bootstrap');
window.Vue = require('vue');

import pjaxAdapter from 'vue-pjax-adapter';
window.Vue.use(pjaxAdapter);

// or if you don't like the default target, you can override it
// window.Vue.use(pjaxAdapter, {
//     targetSelector: '#my-custom-target',
// });

const app = new Vue({
    el: '#app'
});

As the final step, you will have to install Spatie's PHP library to play ball on the server side.

Once done, head to your browser and see the magic happen!

Options

You can override the default target container if you need to at time of initialisation.

window.Vue.use(pjaxAdapter, {
    targetSelector: '#my-custom-target',
});

Should you have any links you want to disable the PJAX behaviour for, you can do so in two ways: by adding the data-no-pjax attribute or by adding the no-pjax class.

<a href="/my-non-pjax-link-1" data-no-pjax>External Link 1</a>
<a href="/my-non-pjax-link-2" class="no-pjax">External Link 2</a>

If you have many of those under a common parent, you can do so by disabling the PJAX behaviour on the parent itself using either of the methods above.

<div data-no-pjax>
    <div>
        <h1>Some external links</h1>
        <ul>
            <li><a href="/my-non-pjax-link-1">External Link 1</a></li>
            <li><a href="/my-non-pjax-link-2">External Link 2</a></li>
            <li><a href="/my-non-pjax-link-3">External Link 3</a></li>
        </ul>
    </div>
    <div>
        <h1>Even more external links</h1>
        <ul>
            <li><a href="/my-non-pjax-link-4">External Link 4</a></li>
            <li><a href="/my-non-pjax-link-5">External Link 5</a></li>
            <li><a href="/my-non-pjax-link-6">External Link 6</a></li>
        </ul>
    </div>
</div> 

How it works

The plugin attaches an event listener to all the a[href] tags on your page at time of initialisation. When you click the link and you haven't disabled the PJAX behaviour, it takes over by preventing the default action and works with Spatie's PHP library to bring only the relevant content from the backend. Otherwise, it will let the default click event fall through and go on its merry way.

Performance

Configuration:

  • fresh install of Laravel with VueJS and Axios (nothing else);
  • a three link navigation: ul > li > a, nothing more complex;
  • three pages with only an h1 tag on each;
  • all run by Laravel Valet on localhost.

Test results without using the plugin: Test results without using the plugin

Test results with the plugin: Test results with the plugin

Bugs

Please report bugs using the issue tracker.

Contributing

You are more than welcome to help me maintain and extend this plugin.

About us

Riverskies Development Limited is a small software development company based in Surrey, UK.

vue-pjax-adapter's People

Contributors

barnabaskecskes avatar

Watchers

James Cloos avatar Damir Grgic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.