Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Option to not reexecute scripts in body? #143

Closed
lucas-clemente opened this issue Dec 14, 2012 · 5 comments
Closed

Option to not reexecute scripts in body? #143

lucas-clemente opened this issue Dec 14, 2012 · 5 comments

Comments

@lucas-clemente
Copy link

I usually prefer to add script tags at the bottom of the body, for reasons of loading speed. However as far as I understand, with recent changes to turbolinks, all of those tags will be evaluated every time a new page loads. But that can take a long time (and e.g. scripts like jquery-ujs throw errors in that case), and is the reason I wanted turbolinks in the first place.

Is there a way to put scripts into the body and to not evaluate them every time? I actually would have expected data-turbolinks-track to behave like that. What do you think about changing its behavior?

@jipiboily
Copy link

I'll add my 2 cents here, not only about that issue but in general. I think turbolink should not deal with all edge cases but have conventions, just like Rails. Turbolinks users just needs to adapt their stack to turbolinks' conventions. If it's too hard, maybe this is not the right solution for you? If we deal with all edge cases, we'll end up having reimplemented the whole browser thing, pile of crap code that will be unnecessary complex and less maintainable.

This is just my 2 cents! :)

Disclaimer: I am not a maintainer and haven't even wrote a single line for turbolinks so take that with a grain of salt, please. That said, I am thankful for all the work that was done for turbolinks.

@lucas-clemente
Copy link
Author

I understand your point. For now I just put my script tags into the header. That solves the issue, but at the expense of speed in non-turbolinks environments. For me that's not a problem, but it might be for others.

At the end, the question boils down to "where do I put my script tags". Thinking about it again I see three options:

  • Leave it as it is and put everything into the header (using $(document).on("page:load", ...) for attaching stuff)
  • Leave it as it is and put js libraries (jquery, ...) into the header, stuff that registers handlers on the page into the body
  • Change the behavior of turoblinks to allow putting everything into the body.

Probably you are right and this doesn't justify a change in turbolinks. Maybe these options should be listed and explained in the readme?

@reed
Copy link
Collaborator

reed commented Dec 14, 2012

I don't see the harm in providing an option for exempting scripts from execution after changing the page. It could be implemented with half a line of code. Just have the executeScriptTags function ignore scripts that have a data-turbolinks-run-once attribute.

I can submit a PR if anyone else thinks it's a good idea.

@yasuoza
Copy link
Contributor

yasuoza commented Dec 15, 2012

I agree with @jipiboily. Implementation of data-turbolinks-run-once may be easy like following, but I think turbolinks should be minimum and obey rail's way: static assets should be loaded in header.

executeScriptTags = ->
  for script in document.body.getElementsByTagName 'script' when script.type in ['', 'text/javascript'] and not script.getAttribute?('data-turbolinks-run-once')?
    copy = document.createElement 'script'

@dhh
Copy link
Contributor

dhh commented Dec 15, 2012

Yeah, I don't think this is a good idea. The whole point of Turbolinks is to avoid reloading scripts, so trying to optimize for what happens in the body doesn't gel with that. So no thanks on this for now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants