Lazy-load Rails partials via CableReady
BREAKING CHANGE: With v1.0, futurism has been transferred to the stimulusreflex organization. Please update your npm package to @stimulus_reflex/futurism accordingly
Caniuse
with a helper in your template
custom
With that method, you could lazy load every class that has to_partial_path defined (ActiveModel has by default).
You can pass the placeholder as a block:
You can also omit the placeholder, which falls back to eager loading.
Currently there are two ways to call futurize, designed to wrap render's behavior:
You can pass a single ActiveRecord or an ActiveRecord::Relation to futurize, just as you would call render:
Remember that you can override the partial path in you models, like so:
That way you get maximal flexibility when just specifying a single resource.
Call futurize with a partial keyword:
You can also use the shorthand syntax:
Collection rendering is also possible:
You can also pass in the controller that will be used to render the partial.
By default (i.e. not passing in a value), futurize will use ApplicationController, but you may override by setting the Futurism default controller in an initializer, for example config/initializers/futurism.rb.
You can pass a hash of attribute/value pairs which will be mixed into the HTML markup for the placeholder element. This is important for layouts that require elements to have dimensionality. For example, many scripts calculate size based on element height and width. This option ensures that your elements have integrity, even if they are gone before you see them.
This will output the following:
It may sound surprising to support eager loading in a lazy loading library , but there's a quite simple use case:
Suppose you have some hidden interactive portion of your page, like a tab or dropdown. You don't want its content to block the initial page load, but once that is done, you occasionally don't want to wait for the element to become visible and trigger the IntersectionObserver, you want to lazy load its contents right after it's added to the DOM.
Futurism makes that dead simple:
Futurism's default behavior is to broadcast partials as they are generated in batches:
On the client side, IntersectionObserver events are triggered in a debounced fashion, so several renders are performed on the server for each of those events. By default, futurism will group those to a single broadcast call (to save server CPU time).
For collections, however, you can opt into individual broadcasts by specifying broadcast_each: true in your helper usage:
For individual models or arbitrary collections, you can pass record and index to the placeholder block as arguments:
Once your futurize element has been rendered, the futurize:appeared custom event will be called.
Add this line to your application's Gemfile:
And then execute:
To copy over the javascript files to your application, run
! Note that the installer will run yarn add @stimulus_reflex/futurism for you !
After bundle, install the Javascript library:
In your app/javascript/channels/index.js, add the following
For authentication, you can rely on ActionCable identifiers, for example, if you use Devise:
The Stimulus Reflex Docs have an excellent section about all sorts of authentication.
In Rails system tests there is a chance that flaky errors will occur due to Capybara not waiting for the placeholder elements to be replaced. To overcome this, add the flag
to an initializer, for example config/initializers/futurism.rb.
Out of the box, Rails will prefix generated urls with http://example.org rather than http://localhost, much like ActionMailer. To amend this, add
to your environments.
Below are a set of instructions that may help you get a local development environment working
The gem is available as open source under the terms of the MIT License.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Read more from the original source:
GitHub - stimulusreflex/futurism: Lazy-load Rails partials ...
- Alzheimer's May Spread Through Blood Transfusions, Scientists Find - Futurism - April 2nd, 2024 [April 2nd, 2024]
- So Much Polar Ice Is Melting That It's Slowing Down the Earth's Rotation - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Scientists Alarmed to Discover Microplastics in Ancient Archaeological Dig - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Crazed CEOs Adding AI to Home Appliances - Futurism - April 2nd, 2024 [April 2nd, 2024]
- NASA's First Artemis Moon Astronauts Will Bring Small Greenhouse to Lunar Surface - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Twitter Caught Selling Data to Government Spies While Complaining About Surveillance - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Scientist Who Gene-Edited Human Babies Back in the Lab Again After Prison Release - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Investigation Finds What Really Happens If You Actually Click Those PUSSYINBIO Links - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Reddit's Stock Is Already Collapsing - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Official NYC Chatbot Encouraging Small Businesses to Break the Law - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Marineland Under Investigation Amid 17 Beluga Deaths - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Experts Say Bridge Collapsed Because It Was Struck by Giant Ship - Futurism - April 2nd, 2024 [April 2nd, 2024]
- The James Webb's Beautiful Images Actually Arrive in Black and White - Futurism - April 2nd, 2024 [April 2nd, 2024]
- ChatGPT Use Linked to Memory Loss, Procrastination in Students - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Rich People Hiking Mount Everest Now Forced to Bring Their Poop Back With Them - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Cargo Giant That Took Out Bridge Had Silenced Whistleblower Who Warned About Safety - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Sam Bankman-Fried Sentenced to 25 Years - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Against All Odds, Japan's Toppled Moon Lander Is Still Alive - Futurism - April 2nd, 2024 [April 2nd, 2024]
- OpenAI Let Directors Make Short Films With Sora and the Results Are Wild - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Google Pushing Its Unsafe Search AI on Users Who Didn't Opt In - Futurism - April 2nd, 2024 [April 2nd, 2024]
- Microsoft's Super Bowl AI Ad Will Get Dumbest People You Know Absolutely Hyped - Futurism - February 13th, 2024 [February 13th, 2024]
- Genes Aren't Actually the Blueprint of Life, Experts Say - Futurism - February 13th, 2024 [February 13th, 2024]
- Tesla Driver Says He's Not Sure If He Killed a Pedestrian Because He Was on Autopilot - Futurism - February 13th, 2024 [February 13th, 2024]
- Google Renames Its Forgettable AI in Fight to Stay Relevant - Futurism - February 13th, 2024 [February 13th, 2024]
- Sierra Space Reveals "Star Wars"-Looking New Spaceship - Futurism - February 13th, 2024 [February 13th, 2024]
- Sample From Distant Asteroid Shows Signs of Originating on Ocean World - Futurism - February 13th, 2024 [February 13th, 2024]
- Facebook Exec Warned Zuckerberg Might Have Panic Attack and Faint - Futurism - February 13th, 2024 [February 13th, 2024]
- Mounjaro Patients Show Hugely Improved Blood Pressure - Futurism - February 13th, 2024 [February 13th, 2024]
- Wait, Did Elon Musk Just Hint He's Going to Buy Disney? - Futurism - February 13th, 2024 [February 13th, 2024]
- Tesla Is Officially the Worst-Performing S&P 500 Stock of the Year - Futurism - February 13th, 2024 [February 13th, 2024]
- Massive European Satellite to Plummet Back Down to Earth Uncontrollably - Futurism - February 13th, 2024 [February 13th, 2024]
- "Pro Life Spider Man" Tries to Climb the Sphere in Las Vegas, Gets Immediately Arrested - Futurism - February 13th, 2024 [February 13th, 2024]
- Fusion Reactor Produces Record Amount of Energy in Final Experiment - Futurism - February 13th, 2024 [February 13th, 2024]
- If You Have Signs of Dementia, Doctors Found a Way to Help - Futurism - February 13th, 2024 [February 13th, 2024]
- Fusion Facility Generates Twice the Power Put Into It - Futurism - February 13th, 2024 [February 13th, 2024]
- Nicki Minaj, Who's Used AI, Decries Use of AI - Futurism - February 13th, 2024 [February 13th, 2024]
- NASA Spacecraft Swoops So Close to Io That It Can See Volcanic Eruptions - Futurism - February 13th, 2024 [February 13th, 2024]
- Harvard Scientist Presents New Evidence That Samples Are Alien Spacecraft - Futurism - February 13th, 2024 [February 13th, 2024]
- Transcript: The Futurist: The Rise of AI - The Washington Post - February 13th, 2024 [February 13th, 2024]
- Operator of AI-Powered Joe Biden Unmasked, Criminal Investigation Opened - Futurism - February 13th, 2024 [February 13th, 2024]
- Twitter Suddenly Suspends Journalists Critical of Elon Musk - Futurism - January 12th, 2024 [January 12th, 2024]
- Experts Warn Against Strip Mining the Moon - Futurism - January 12th, 2024 [January 12th, 2024]
- Satellites Show the East Coast Is Sinking - Futurism - January 12th, 2024 [January 12th, 2024]
- Millions of Plastic Pellets Wash Up on Shore - Futurism - January 12th, 2024 [January 12th, 2024]
- We'd Like to See the Chain of Custody on Elon Musk's Pee After the Drug Allegations - Futurism - January 12th, 2024 [January 12th, 2024]
- Scientists Working to Explain "Superstructures" on Ocean Floor - Futurism - January 12th, 2024 [January 12th, 2024]
- Gaping Hole in Boeing 737 Linked to Stuffing More Passengers Into Flights - Futurism - January 12th, 2024 [January 12th, 2024]
- Rejoice! Device That Sprays Your Butthole Now Connects to Alexa for Voice Control - Futurism - January 12th, 2024 [January 12th, 2024]
- Wizards of the Coast Denies "Magic: The Gathering" Art Was AI-Generated, Then Admits It Was - Futurism - January 12th, 2024 [January 12th, 2024]
- Zuckerberg Brags About Feeding Cows Macadamia Nuts and Beer at His Alleged Doomsday Bunker - Futurism - January 12th, 2024 [January 12th, 2024]
- People Close to Elon Musk Concerned About Alleged Drug Use, Slurring Words - Futurism - January 12th, 2024 [January 12th, 2024]
- SpaceX Sues Government Over Claim It Fired Employees for Criticizing Elon Musk - Futurism - January 12th, 2024 [January 12th, 2024]
- Tesla Admits Its Cars Have Shorter Driving Range Than It Claimed - Futurism - January 12th, 2024 [January 12th, 2024]
- Remember That Uber-Hyped "AI Pin" Startup? It's Already Doing Layoffs - Futurism - January 12th, 2024 [January 12th, 2024]
- The Mighty Cybertruck Keeps Getting Stuck in the Snow - Futurism - January 12th, 2024 [January 12th, 2024]
- Robotics Expert Says AI Hype Is Due for a Brutal Reality Check - Futurism - January 12th, 2024 [January 12th, 2024]
- Schoolteacher Finds Door Plug That Fell Off Boeing 737 in His Backyard - Futurism - January 12th, 2024 [January 12th, 2024]
- Experts Alarmed After Large Piece Blows Off Boeing 737 Mid-Flight - Futurism - January 12th, 2024 [January 12th, 2024]
- SpaceX Releases Bizarre Video in Attempt to Prove Elon Wasn't on Drugs - Futurism - January 12th, 2024 [January 12th, 2024]
- Scientists Have Been Studying Your Pee and They Finally Have Answers - Futurism - January 12th, 2024 [January 12th, 2024]
- What should we expect in 2024? We asked three futurists - ISRAEL21c - January 4th, 2024 [January 4th, 2024]
- Partiers Attempt to Celebrate New Year's Twice With Flight Between Time Zones, Accidentally Land in Wrong Year - Futurism - January 4th, 2024 [January 4th, 2024]
- Adopted Child Accused of Secretly Being an Adult Takes DNA Test to Prove Age - Futurism - January 4th, 2024 [January 4th, 2024]
- Congress Receiving Mysterious Classified Briefing About UFOs - Futurism - January 4th, 2024 [January 4th, 2024]
- Driver Injured in First Cybertruck Crash - Futurism - January 4th, 2024 [January 4th, 2024]
- Kiki Goti STIRred 2023 with striking visions of maximalism and neo-futurism - STIRpad - January 4th, 2024 [January 4th, 2024]
- Scary AI Can Look at Photos and Figure Out Exactly Where They Were Taken - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Scientists Simulated Runaway Greenhouse Effect and It's Horrifying - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Jeff Bezos Discusses Plans for a Trillion People to Live in Huge Cylindrical Space Stations - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Car Dealership Disturbed When Its AI Is Caught Offering Chevys for $1 Each - Futurism - December 22nd, 2023 [December 22nd, 2023]
- DNA Tests Are a Fun Holiday Gift... Unless They Reveal a Horrifying Secret - Futurism - December 22nd, 2023 [December 22nd, 2023]
- NASA Proudly Shows Off Desiccated Tomatos Lost in Space Station Crevice - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Man Horrified When Someone Uses AI to Reword and Republish All His Content, Complete With New Errors - Futurism - December 22nd, 2023 [December 22nd, 2023]
- DeepMind Says Its AI Solved a Math Problem That Humans Were Stumped By - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Booze Affects Sperm More than a Month After Cessation in Mouse Research - Futurism - December 22nd, 2023 [December 22nd, 2023]
- There May Be Some Big Tech Names on the Unsealed Epstein List - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Teslas Crash More Than Any Other Brand, Analysis Finds - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Microsoft's Stuffing Talking Generative AI Into Your Car - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Those Scam Texts You're Getting Are Sent By Victims of Human Trafficking - Futurism - December 22nd, 2023 [December 22nd, 2023]
- Tesla Driver Forced to Pay After Killing Two People on Autopilot - Futurism - December 22nd, 2023 [December 22nd, 2023]