question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Safari: `<picture>` in asynchronously loaded pages get fallback image

See original GitHub issue

Turbo version: 7.0.0-rc.1.

This is a weird problem only faced in Safari (tested in 14.1.2).

I moved to Turbo from Swup. In my website, I use <picture> tag to load responsive images (showing sample code below).

In Safari, when the page is navigated to, through Turbo the fallback image (file specified in the <img>) is loaded.

In other browsers, the correct image matching the width and pixel density is loaded.

Example:

<picture
  ><source
    type="image/webp"
    srcset="
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-100w.webp   100w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-300w.webp   300w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-500w.webp   500w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-700w.webp   700w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-900w.webp   900w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-1100w.webp 1100w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-1300w.webp 1300w
    "
    sizes="100vw" />
  <source
    type="image/jpeg"
    srcset="
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-100w.jpeg   100w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-300w.jpeg   300w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-500w.jpeg   500w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-700w.jpeg   700w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-900w.jpeg   900w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-1100w.jpeg 1100w,
      /images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-1300w.jpeg 1300w
    "
    sizes="100vw" />
  <img
    src="/images/anh-tuan-to-F2FIM5HJogk-unsplash-110cbaf3-100w.jpeg"
    width="1300"
    height="866"
    alt="Photo by Anh Tuan To on Unsplash"
    sizes="100vw"
    loading="lazy"
    decoding="async"
/></picture>

To demonstrate the problem, you can visit https://sleepy-hawking-71d88c.netlify.app in Safari and click through the links. When navigated through links, you’ll see a blurred image (with filename ending with -100w.jpeg). If you refresh the page, the correct image is loaded.

You can also find the code the sample page in this repo.

This problem is not there in the latest versions of Turbolinks, Barba.js, or Swup.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
boboldehampsinkcommented, Nov 1, 2021

Same here. For now I am using this (ugly) snippet to work around the Safari problem:

// Image fix for Safari
document.addEventListener('turbo:render', () => {
  if (navigator.userAgent.match(/Version\/[\d.]+.*Safari/)) {
    document.querySelectorAll('picture').forEach((img) => {
      img.outerHTML = img.outerHTML; // eslint-disable-line no-param-reassign, no-self-assign
    });
  }
});
3reactions
rikcommented, Sep 6, 2022

I believe this is related to Turbo’s use of new DOMParser().parseFromString(). Other frameworks probably don’t use this and don’t run into this issue.

I’ve opened https://bugs.webkit.org/show_bug.cgi?id=244815 on Safari’s side.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Images sometimes don't load in Safari [#3279316] | Drupal.org
We are experiencing an issue where images don't always appear in safari (almost 75% of the time they're missing). Only occurs when using...
Read more >
Why does Safari only render the top of images sometimes?
When this happens, we can refresh the page and the image will still only partially render. We have only ever seen this happen...
Read more >
Native Lazy Image Loading With JavaScript Fallback
Lazy image loading is a technique to avoid unnecessary downloads by delaying below-the-fold images until each image appears on screen. Similar to other ......
Read more >
Lazy Loading Images – The Complete Guide - ImageKit.io
Lazy loading images that are not in the viewport improves initial page load performance and user experience. This is an in-depth guide to ......
Read more >
Smarter, Lighter, Better Images: A Guide to Optimization
decoding="async" tells your browser it can try to parallelize loading your image. When your page is loading, it tries to decode both text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found