Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avif, webp custom content works incorrectly: it still loads original jpg #2045

Open
theusualberliner opened this issue Jun 27, 2023 · 2 comments

Comments

@theusualberliner
Copy link

theusualberliner commented Jun 27, 2023

Hello!

I'm creating a gallery of responsive images using picture, source and img and I'd like to use the same in photoswipe. The issue which I have is that photoswipe preloads original jpg files when moving to the next/previous slide, or in case of this example - when you open first picture: https://photoswipe.com/custom-content. Also if you open a picture and then close it, webp image is loaded on open, and on slide closing jpg image is loaded.

In my example it looks like this: when I navigate to 3rd image, it loads first image in jpg and then, as I proceed further, it keeps loading avif and jpg.

Screenshot

Code snippets:

dataSource: images.map((image, index) => {
        return {
          src: image.url,
          sources: image.sources,
          thumbnailUrl: image.thumbnailUrl,
          width: image.width,
          height: image.height,
          alt: "",
        };
      }),

lightbox.on("contentLoad", (e) => {
      const { content, isLazy } = e;
      console.log(content);

      if (content.data.sources) {
        e.preventDefault();

        content.pictureElement = document.createElement("picture");

        for (const source of content.data.sources) {
          const sourceElement = document.createElement("source");
          sourceElement.srcset = source.srcset;
          sourceElement.type = source.type;
          content.pictureElement.appendChild(sourceElement);
        }

        content.element = document.createElement("img");
        content.element.src = content.data.src;
        content.element.setAttribute("loading", "lazy");
        content.element.setAttribute("alt", "");
        content.element.className = "pswp__img";
        content.pictureElement.appendChild(content.element);

        content.state = "loading";

        if (content.element.complete) {
          content.onLoaded();
        } else {
          content.element.onload = () => {
            content.onLoaded();
          };

          content.element.onerror = () => {
            content.onError();
          };
        }
      }
    });

    lightbox.on("contentAppend", (e) => {
      const { content } = e;
      if (content.pictureElement && !content.pictureElement.parentNode) {
        e.preventDefault();
        content.slide.container.appendChild(content.pictureElement);
      }
    });

    lightbox.on("contentRemove", (e) => {
      const { content } = e;
      if (content.pictureElement && content.pictureElement.parentNode) {
        e.preventDefault();
        content.pictureElement.remove();
      }
    });
@Dominic-Marcelino
Copy link

Dominic-Marcelino commented Aug 18, 2023

Running into a similar issue. After further debugging it seems that photoswipe still requests the jpeg while the contentRemove event 🤔 Did you found any solution?

@semaf
Copy link

semaf commented May 8, 2024

Seems that the issue has not been solved. According to the code it should also create the element which does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants