Intercooler.js – Simple AJAX Using HTML Attributes
intercoolerjs.org
intercoolerjs.org
as usual, a lot of negative comments from HN on the approach (which is admittedly against the grain) but I've used it for a relatively large and successful web app (rails back end) and it has worked out well compared to the SPA architectures I've had to deal with
i think the philosophical roots in REST/HATEOAS are worth considering, if you are open minded:
http://intercoolerjs.org/2016/01/18/rescuing-rest.html
http://intercoolerjs.org/2016/05/08/hatoeas-is-for-humans.ht...
I used it at my previous job, but didn't find it gave me that many benefits, although there wasn't anything particularly wrong with it either. It just seems like a challenging problem to properly solve with any organization.
I was initially really interested in GraphQL and Relay, but when I tried em out I found em lacking for our small engineering team. I feel kinda similar with all these other alternative tools. I think if you have any API stability problems, you need to tackle it at an organizational level. The API team needs to communicate openly with the web frontend and mobile app team (which might include a cross-section of highly-mixed-specialty people) to make sure all changes are fully backwards compatible. I don't think the tools do a great job at helping you tackle complex issues.
I typically split my externally facing JSON API out from the main application (which uses HTML/intercooler) because the use cases are typically so different: the JSON API needs to be general and abstract whereas the web application UI has a lot of fiddly little specific data needs that are better implemented on the back end, where you have full access to the domain model and a proper query language.
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.
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.
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.
https://developer.mozilla.org/en-US/docs/Web/Events/beforeun...
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).
I also encountered some bugs when mixing the 2 during a transition phase that broke my requests. If I had to start over again I would only use Vue starting with the drop in JS library (no webpack/preprocessing)
I agree that Vue does much more than Intercooler and in theory, it could be seen as an overkill. But in reality, a developer will probably use it in conjunction with jQuery and then he might be better off replacing these two with Vue.js
The Progressive
JavaScript Framework
I mean, regardless of anecdotal specific usage, vue is a framework. And a nice one too! Just entirely too much for something that can be fulfilled by a simple pjax toolBack when I used PHP 5-6 years ago, I would use an "AJAX" helper built into the CakePHP MVC framework which would generate JavaScript for really basic AJAX requests. It was great for rapid prototyping and even production for admin panels and such. It's cool to see a similar, client-sided, framework-agnostic library like this with similar functionality.
Which hilariously begins with the statement:
>> This has been reposted so many times by the author and by others that I can't help but finally ask. What's the point?
To add -- it depends on jQuery? Yeah, I'm gonna have to pass. What this thing does is so small that I can't fathom why they would need to import all of jQuery just to provide a new way to do AJAX requests.
I'll just keep using React, it probably already solves whatever problem is trying to be solved here.
Then it is even more antithetical to import jQuery. I might as well use an SPA framework instead.
It isn't right for everything, but it works well for me w/ a rails back end and there are some good theoretical reasons for using HTML if you like REST/HATEOAS. I linked a few articles up thread you can read if you are interested.
I wouldn't call Intercooler heavy, au contraire.
<a href="http://example.com" onclick="fetch(this.href, {method: 'POST'}).then(r => r.text()).then(t => this.innerText = t); return false;">Click me!</a>I've used pjax with Laravel before, and it's trivial. Kinda does the same thing.
There is a Laracasts video explaining it here: https://laracasts.com/lessons/faster-page-loads-with-pjax
Demo page here: https://pjax.herokuapp.com/
you can click on the code tabs and see what's going on, and it should be easy enough to map to whatever backend technology you are using
You would maintain most of your HTML, routing logic, etc. server side.
as w/ pjax or turbolinks, barbajs is a fine library, but it isn't trying to create HTML++ and there isn't much REST-ful theory behind it
i'm sure it's good for a lot of stuff though