question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use turbolinks:before-cache event instead of turbolinks:visit

See original GitHub issue

Hi!

Have you tried this already? If there’s no known issues, I think before-cache is better because components would be alive and not removed from current page until new page is loaded.

Using turbolinks:visit with slow internet connection, components disappear from page before new page is loaded.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
connorsheacommented, Jan 5, 2019

So I had this problem and it was caused by the same thing as @nicklozon, where I had template code inside my .html.erb file.

<div id="game-library">
  <library
    v-bind:purchased-releases="<%= @purchased_releases.to_json(except: [:created_at, :updated_at]) %>"
  ></library>
</div>

And then in library.js, which I imported into my application.js:

import TurbolinksAdapter from 'vue-turbolinks';
import Vue from 'vue/dist/vue.esm';
import Library from './components/library.vue';

Vue.use(TurbolinksAdapter)

document.addEventListener('turbolinks:load', () => {
  let gameLibrary = document.getElementById("game-library")
  if (gameLibrary != null) {
    const library = new Vue({
      el: '#game-library',
      components: { Library }
    })
  }
});

^^^ the above is what it looks like when the problem exists. ^^^

@printercu’s script (this) worked for me, though it does seem like - based on nick’s comment previously - you can also avoid this problem by simply not including the Vue.js custom elements in the .html.erb files, e.g. by plugging it into a data- attribute and then mounting it on that, or something?

1reaction
printercucommented, Feb 28, 2018

@nicklozon can you please give a try for https://gist.github.com/printercu/e202851c370ffc38bcdedeb8300d417e It should work fine both for .vue files and <script type='x-template'>. I don’t use div templates because they have some limitations/edgecases and can show template source on page while page load, so have not tested for them.

Compiled coffee => js
// Inspired by
// https://github.com/jeffreyguenther/vue-turbolinks/blob/master/index.js
// but changed to support going back to cached page with vue instance.
// Original version will keep instance created from cache along with new one.
var plugin;

plugin = {
  instances: [],
  bind: function() {
    // Destroy instances on current page when moving to another.
    document.addEventListener('turbolinks:before-cache', function() {
      return plugin.cleanupInstances();
    });
    // Destroy left instances when previous page has disabled caching.
    document.addEventListener('turbolinks:before-render', function() {
      return plugin.cleanupInstances();
    });
    // Clear instances on curent page which are not present anymore.
    return document.addEventListener('turbolinks:load', function() {
      return plugin.cleanupInstances(function(x) {
        return document.contains(x.$el);
      });
    });
  },
  cleanupInstances: function(keep_if) {
    var i, instance, len, ref, result;
    result = [];
    ref = plugin.instances;
    for (i = 0, len = ref.length; i < len; i++) {
      instance = ref[i];
      if (typeof keep_if === "function" ? keep_if(instance) : void 0) {
        result.push(instance);
      } else {
        instance.$destroy();
      }
    }
    return plugin.instances = result;
  },
  Mixin: {
    beforeMount: function() {
      // If this is the root component,
      // we want to cache the original element contents to replace later.
      // We don't care about sub-instances, just the root
      if (this === this.$root) {
        plugin.instances.push(this);
        return this.$originalEl = this.$el.outerHTML;
      }
    },
    destroyed: function() {
      if (this === this.$root) {
        // We only need to revert the html for the root component.
        return this.$el.outerHTML = this.$originalEl;
      }
    }
  },
  install: function(Vue, _options) {
    plugin.bind();
    return Vue.mixin(plugin.Mixin);
  }
};


// export default plugin
// or:
// Vue.TurbolinksAdapter = plugin
// plugin.bind()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Turbolinks makes navigating your web application faster
Listen for the turbolinks:before-cache event if you need to prepare the document before Turbolinks caches it. You can use this event to reset...
Read more >
Turbolinks Cheatsheet - Key Shift in Cmd
Copies the page to cache using cloneNode(true), so when it's returned from the cache, attached event listeners and associated data are discarded ...
Read more >
How We Migrated To Turbolinks Without Breaking Javascript
Turbolinks caches pages immediately before navigating away from them. When the user clicks the "Back" button, Turbolinks fetches the previous ...
Read more >
Javascript DOM events before and after Turbolinks cache
I think the problem here is that you are binding to two events: turbolinks:load and $(document).ready() . Turbolinks does not discard event ......
Read more >
Turbolinks for faster web navigation - LogRocket Blog
Turbolinks stores every page you visit within an internal cache, so that when you need to access the same page, Turbolinks can retrieve...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found