GithubHelp home page GithubHelp logo

Comments (4)

jorisnoo avatar jorisnoo commented on June 25, 2024 1

Hi @daun and @hirasso,

thanks a lot for the detailed response and for pointing out the fragment plugin; very much appreciated. I'll happily look into all of this, maybe i'll even wait for the v4 release if it's imminent. Thank you both for your help and the work on the library!

from swup.

daun avatar daun commented on June 25, 2024

Hi there! That's a valid point. I haven't used that option too much myself yet so I'd like to get the input of @hirasso who's been using it extensively in some of his projects.

In the meantime, I'll just point you to the demo site for the Fragment Plugin that will be released along with swup 4 next week or so. Sounds like a good fit for your use case — it's meant for targeting smaller page fragments with custom transitions for filter interfaces, overlays, and such.

swup 4 will also come with a cache:set hook you can use to update any cache items after they're created.

from swup.

hirasso avatar hirasso commented on June 25, 2024

@jorisnoo I've only ever used resolveUrl for URLs that all returned the same data from the server. Basically like a mirror of the canonical link element. I guess you might have tried to clear the cache for your resolved URLs like this?

swup.on("transitionStart", () => {
  const url = swup.getCurrentUrl();
  if (url !== swup.resolveUrl(url)) {
    swup.cache.remove(swup.resolveUrl(url));
  }
});

This might work if navigating using the browsers back/forward buttons, but won't work when clicking on links, due to a limitation in swup@3, that will be fixed in v4.

In swup3, swup.getCurrentUrl() called from inside transitionStart returns the URL before the visit for clicks on links, but the URL after the visit during history visits. This will be more consistent in swup@4. Until then, you could work around this limitation by using two combined handlers:

import Swup, { Location } from 'swup';

const swup = new Swup();

function maybeClearCache(url) {
  if (url !== swup.resolveUrl(url)) { // or some condition that makes sense for your case :)
    console.log(url);
    swup.cache.remove(swup.resolveUrl(url));
  }
}

swup.on("clickLink", (e) => {
    // get the correct URL from the link element instead of the browser for click on links:
    const url = Location.fromElement(e.delegateTarget).url;
    maybeClearCache(url);
});

swup.on("popState", (e) => {
    maybeClearCache(swup.getCurrentUrl());
});

from swup.

hirasso avatar hirasso commented on June 25, 2024

Alternatively, you could brute-force overwrite swup.cache.getCacheUrl to never resolve cache URLs:

import Swup, { Location } from 'swup';
const swup = new Swup();

swup.cache.getCacheUrl = (urlToResolve) => {
  const { url } = Location.fromUrl(urlToResolve);
  return url;
};

Just keep in mind that this function is not part of the public API and therefor might change without notice. It will be renamed to swup.cache.resolve in swup@4, for example.

from swup.

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.