如何在NUXT中正确地延迟负载图像?

发布于 2025-01-28 14:43:32 字数 282 浏览 3 评论 0原文

是否可以同时将-SSR和Lazy -Loading用于客户端的图像。这是我的情况。我的标记与img [src]一起渲染,在我的chrome浏览器中,我按Ctrl+u,可以看到所有图像都具有SRC属性(SEO机器人会识别它们,我很高兴),但是在客户端,我需要拥有IMG [DATA-SRC]用于延迟加载。如果我在服务器端上有一个data-src属性,则SEO机器人找不到任何SRC(按Ctrl + U将证明这一点),但是所有图像都将带有懒惰加载...如果我过滤标记在接收客户端之前(实际上我确定如何在NUXT中实现它)。请提出任何解决问题的想法或指示?

Is it possible to use both - ssr and lazy-loading for images on the client side. Here is my situation. My markup is rendered with img[src], in my chrome browser I press ctrl+U and can see all images have an src attribute (seo robots will recognize them and I'm happy), but on the client side I need to have img[data-src] for lazy-loading. If I have a data-src attribute on the server-side, the seo robots won't find any src (pressing ctrl + U will prove it), but all images will be displayed with lazy loading... Should I filter the markup before receiving on the client side (actually I'm sure how to implement it in nuxt). Please suggest any ideas or directions to solve the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜还是长夜 2025-02-04 14:43:32

这是实现以下方法的方法:

  • 在移动视口
  • (< 1000px)上以红线为代表的移动视口(< 1000px),
  • 如果您的视口更宽,则您有4个图像(桌面viewport)
  • 在这里进行繁重的举重,在不旧的浏览器
  • 从移动设备开始,打开您的网络选项卡,按名为6的图像过滤,您会发现,如果增加视口&gt,您将获得一些网络请求。 1000px(滚动滚动时,懒惰加载)
<template>
  <div>
    <pre>all the interesting images: {{ pokemon.sprites.other }}</pre>
    <div class="reference-breakpoint"></div>

    <p>Down below are all the remaining images ⬇️</p>
    <img :src="pokemon.sprites.other.dream_world.front_default" />
    <img
      class="hide-when-mobile"
      loading="lazy"
      :src="pokemon.sprites.other.home.front_default"
    />
    <img
      class="hide-when-mobile"
      loading="lazy"
      :src="pokemon.sprites.other.home.front_shiny"
    />
    <img
      class="hide-when-mobile"
      loading="lazy"
      :src="pokemon.sprites.other['official-artwork'].front_default"
    />
  </div>
</template>

<script>
export default {
  async asyncData({ $axios }) {
    const pokemon = await $axios.$get(
      'https://pokeapi.co/api/v2/pokemon/charizard'
    )
    return { pokemon }
  },
}
</script>

<style scoped>
img {
  display: block;
  height: 100vh;
  width: auto;
}
.reference-breakpoint {
  width: 1000px;
  height: 1rem;
  background-color: red;
}
@media (max-width: 1000px) {
  .hide-when-mobile {
    display: none;
  }
}
</style>

对此不满意吗?这是一个 InterSectionObserver 示例,示例更为复杂,但也更加灵活。

Here is how to achieve the following:

  • fetching some images from an API
  • display 1 image when on a mobile viewport (< 1000px) represented by the red line
  • if your viewport is wider, you have 4 images (desktop viewport)
  • loading="lazy" is doing the heavy lifting here, it's working fine on not-so old browsers
  • if you start on mobile, open your network tab, filter by images named 6, you will see that you'll get some network requests going out if increasing the viewport > 1000px (lazy-loaded when scrolling still)
<template>
  <div>
    <pre>all the interesting images: {{ pokemon.sprites.other }}</pre>
    <div class="reference-breakpoint"></div>

    <p>Down below are all the remaining images ⬇️</p>
    <img :src="pokemon.sprites.other.dream_world.front_default" />
    <img
      class="hide-when-mobile"
      loading="lazy"
      :src="pokemon.sprites.other.home.front_default"
    />
    <img
      class="hide-when-mobile"
      loading="lazy"
      :src="pokemon.sprites.other.home.front_shiny"
    />
    <img
      class="hide-when-mobile"
      loading="lazy"
      :src="pokemon.sprites.other['official-artwork'].front_default"
    />
  </div>
</template>

<script>
export default {
  async asyncData({ $axios }) {
    const pokemon = await $axios.$get(
      'https://pokeapi.co/api/v2/pokemon/charizard'
    )
    return { pokemon }
  },
}
</script>

<style scoped>
img {
  display: block;
  height: 100vh;
  width: auto;
}
.reference-breakpoint {
  width: 1000px;
  height: 1rem;
  background-color: red;
}
@media (max-width: 1000px) {
  .hide-when-mobile {
    display: none;
  }
}
</style>

Not satisfied with this one? Here is an intersectionObserver example, a bit more complex but also more powerful/flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文