News for hackers. Stuff that matters.
Re:Intercooler.js – Simple AJAX Using HTML Attributes by cousin_it on Sunday July 08, 2018 @03:43PM (#17485772)
Sometime ago I had an idea that SPAs exist mostly because animated transitions between pages make managers happy, not to serve any actual user need. If that's true, maybe we could add support for animated transitions between server-side rendered pages, and make SPAs mostly go away.

Then I realized we could do that easily with a JS library! We could give some divs a "sticky-id" attribute, so they stay as part of the DOM tree when the page reloads, only their contents get replaced with the contents of divs from the new page having the same "sticky-id". That way there's no flash to white, and CSS transitions on sticky elements get a chance to fire. The whole thing could be handled by a generic onclick handler on each link. And the links would support opening in new tab as well, the JS would only fire when they are clicked in the same tab. I think that would be the most conservative way to have an SPA-like experience with a minimum of JS. Unfortunately I was too lazy to implement it :-/

EDIT: It's interesting that most replies to this comment seem to talk about whole page transitions, like fading out the whole page. But as I envisioned it, the key part of the idea is that we can get element-by-element transitions without writing custom JS, just by adding "sticky-id" attribute to some elements and letting their CSS transitions do the work.

Re:Intercooler.js – Simple AJAX Using HTML Attributes by detaro on Sunday July 08, 2018 @03:44PM (#17485779)
Re:Intercooler.js – Simple AJAX Using HTML Attributes by cyberferret on Sunday July 08, 2018 @08:15PM (#17487078)
Turbolinks looks neat, but our Ruby app selectively includes CSS and Javascript snippets outside the <body> tag depending on what page is loaded (using :content_for declarations in our templates). I am guessing that Turbolinks will not work for us in this case, as the relevant external files will not be called on page load?
Re:Intercooler.js – Simple AJAX Using HTML Attributes by nicostouch on Sunday July 08, 2018 @09:58PM (#17487454)
turbolinks swaps out the <body> and merges the <head> tags of the incoming page with the existing <head> tag.
Re:Intercooler.js – Simple AJAX Using HTML Attributes by cousin_it on Sunday July 08, 2018 @03:48PM (#17485804)
Neat! Though the point of my idea is having animated transitions between pages, by replacing contents of sticky elements and letting their CSS transitions work as usual. As far as I can tell, turbolinks doesn't do that.
Re:Intercooler.js – Simple AJAX Using HTML Attributes by detaro on Sunday July 08, 2018 @04:13PM (#17485938)
I think it always replaces the entire body element, not parts of it, yes
Re:Intercooler.js – Simple AJAX Using HTML Attributes by b_f on Sunday July 08, 2018 @07:00PM (#17486772)
Re:Intercooler.js – Simple AJAX Using HTML Attributes by cousin_it on Sunday July 08, 2018 @07:19PM (#17486864)
Yeah, well... that still requires you to write custom JS. The thing I'm imagining would be based only on markup - which sticky ids exist in both pages. But the idea is really similar, thanks for the link!
Re:Intercooler.js – Simple AJAX Using HTML Attributes by victor106 on Sunday July 08, 2018 @03:58PM (#17485860)
Turbolinks is cool but it seems more tied to only a rails backend. Anyone know how to use it with Java?
Re:Intercooler.js – Simple AJAX Using HTML Attributes by nicostouch on Sunday July 08, 2018 @10:11PM (#17487490)
It's independent of Rails. It simply hooks the all calls to <a href> tags and replaces them with ajax get requests to the url.

It works really nice with Rails because of how easy Rails makes it return a piece of javascript to the page (this is called SJR) so there is some magic included for making form submissions work which is part of the Rails turbolinks gem. The javascript that the server returns to the page essentially calls Turbolinks.visit("/url-form-submission-would-redirect-to-after-completion") so that the expected navigation takes place. It's quite clever. I think you would have to replicate that in the Java world to get the full experience. This SO answer alludes to how you might replicate SJR https://stackoverflow.com/questions/26750738/java-spring-ret...

So I think out of the box you can expect clicking links to work (and that alone provides a great speedup), but ajax form submissions will require some work. If you look at some articles on how form submissions with turbolinks make use of SJR you could likely figure it out and implement it. It's overall not a very complicated mechanism.

I suppose Rails .erb templating engine also has some helpers which the form submission side of turbolinks relies on. Namely it includes a data attribute data-remote and that's the thing turbolinks hooks into to prevent the default behavior of the form submission and and instead fire the ajax request. In theory so long as your forms include that it will work if you have the SJR stuff in place on your back-end.

You could definitely roll full support for it.

Re:Intercooler.js – Simple AJAX Using HTML Attributes by PascalW on Sunday July 08, 2018 @05:03PM (#17486185)
It's not tied to Rails at all. I'm using it with a Phoenix backend. You just include the JS and presto!
Re:Intercooler.js – Simple AJAX Using HTML Attributes by elcritch on Sunday July 08, 2018 @06:07PM (#17486513)
So you don’t you need to configure the Phoenix backend or whatnot? That’s even more awesome!
Re:Intercooler.js – Simple AJAX Using HTML Attributes by schizoidboy on Sunday July 08, 2018 @03:56PM (#17485848)
JQueryMobile does this (automatically applying to all anchor links): https://jquerymobile.com/

Various page transitions can be set using `defaultPageTransition`: https://demos.jquerymobile.com/1.4.5/transitions/

Click the "page" button to the right of a transition for a demo.

Re:Intercooler.js – Simple AJAX Using HTML Attributes by spankalee on Sunday July 08, 2018 @04:30PM (#17486030)
There once was (still is?) a proposal for this: https://github.com/jakearchibald/navigation-transitions
Re:Intercooler.js – Simple AJAX Using HTML Attributes by 52-6F-62 on Sunday July 08, 2018 @04:38PM (#17486063)
I've done something not the same, but a compromise previously using the Window object's `beforeunload` event to fade out everything in the DOM smoothly, and then using the `beforeload` and `load` events to fade the new DOM back in. It's a bit of a halfway between having sliding transitions, etc. You might be able to simulate that even further than I ever tried.

https://developer.mozilla.org/en-US/docs/Web/Events/beforeun...

Re:Intercooler.js – Simple AJAX Using HTML Attributes by johnnypopcorn on Sunday July 08, 2018 @04:44PM (#17486099)
You may be interested in http://barbajs.org/
Re:Intercooler.js – Simple AJAX Using HTML Attributes by dwaltrip on Sunday July 08, 2018 @04:39PM (#17486070)
The primary benefit I see from SPAs is to enable pages that changes in a wide variety of ways without having to reload the whole thing at any point.

Funny thing, you can actually serve each page as a separate, stand-alone JS "app/page" and still get this benefit (e.g. each page in your app becomes separate webpack entry point).

Re:Intercooler.js – Simple AJAX Using HTML Attributes by blattimwind on Sunday July 08, 2018 @03:54PM (#17485837)
history.pushState() aka pjax