Code Monkey home page Code Monkey logo

Comments (7)

mdesantis avatar mdesantis commented on August 23, 2024 2

With Turbo 8.0.0.beta.2 it was possible to check event.detail.isPreview to handle no-cache and cache renderings, I wonder if now there is another way

from turbo.

andriytyurnikov avatar andriytyurnikov commented on August 23, 2024 1

@mdesantis thanks a lot for your input.
This property is not there anymore, but data-turbo-preview attribute is still there.

from turbo.

andriytyurnikov avatar andriytyurnikov commented on August 23, 2024 1

@mdesantis - yep, seems to work for now.
But in this particular case (animated transitions) supporting caching adds substantial challenges of it's own, and ignoring it is practically impossible

from turbo.

jdelStrother avatar jdelStrother commented on August 23, 2024 1

Ohhh. Yep, that works - thanks!

from turbo.

mdesantis avatar mdesantis commented on August 23, 2024

I see, so this is the workaround nowadays:

addEventListener("turbo:before-render", (event) => {
  event.detail.render = (currentElement, newElement) => {
    if (document.documentElement.hasAttribute("data-turbo-preview"))
      console.log('cache')
    else
      console.log('no cache')
  }
})

I wish Turbo exposed a stable API for this, so we don't have to cross the fingers every library update :D

from turbo.

jdelStrother avatar jdelStrother commented on August 23, 2024

Looks like the isPreview property got reverted here: bb735bf , although the docs still reference it here: https://turbo.hotwired.dev/reference/events#turbo%3Abefore-render

The alternative seems pretty unwieldy - you need to override event.detail.render? So really you'd need something like this:

document.addEventListener("turbo:before-render", function (e) {
  let ogRender = e.detail.render;
  e.detail.render = (...args: any[]) => {
    if (document.documentElement.hasAttribute("data-turbo-preview")) {
      console.log("preview");
    } else {
      console.log("no preview");
    }
    return ogRender(...args);
  };
});

from turbo.

Ambient-Impact avatar Ambient-Impact commented on August 23, 2024

You don't need to override event.detail.render() at all; this should work at the start of a turbo:before-render handler:

// Don't do anything if this is a cached preview being shown.
if (event.target.hasAttribute('data-turbo-preview') === true) {
  return;
}

from turbo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.