Search code examples
javascriptruby-on-railscachingcoffeescriptturbolinks

Turbolinks, Rails, back button, footer from other source


I have an issue with turbolinks. When I click a 'back' button in browser there is one more footer appends.

In application.html.slim:

doctype html
html
  head
    title
      - unless content_for(:title).present?
        | Some text...
      - else
        == yield :title
    meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'
    = csrf_meta_tags
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
    - if admin_admin_signed_in?
      = stylesheet_link_tag    'cms/application', media: 'all', 'data-turbolinks-track': 'reload'
      = javascript_include_tag 'cms/application', 'data-turbolinks-track': 'reload'

I load a footer from the other source (application.coffee):

$(document).on 'turbolinks:load', ->
    $('footer').get(0).innerHTML = ''
    for src in ['//other_site.com/goc/us/other_site.com/responsive.js', '//other_site.com/footer-builder.js']
      s = document.createElement('script')
      s.async = true
      s.src = src
      document.getElementsByTagName('head')[0].appendChild s

Thanks for attention in advance!


Solution

  • As @uzaif said you should put Turbolinks.clearCache()into your .js file for particular dom element or even page in similar situations. In my case I added the following peace of code this way:

    application.coffee:

    $(document).on 'turbolinks:load', ->
      $('footer').get(0).innerHTML = ''
      for src in ['//other_site.com/goc/us/other_site.com/responsive.js', '//other_site.com/footer-builder.js']
        s = document.createElement('script')
        s.async = true
        s.src = src
        document.getElementsByTagName('head')[0].appendChild s
      Turbolinks.clearCache()
    

    Keep in mind it can reduce speed of page loading so make application profiling to be sure