harlan-zw / nuxt-seo

The complete SEO solution for Nuxt.
https://nuxtseo.com
MIT License
1.09k stars 70 forks source link

Option to ignore images or videos with discovery #318

Closed nirazul closed 2 months ago

nirazul commented 2 months ago

Clear and concise description of the problem

I have some videos in the main element that I don't want to include in the sitemap. However, I have not found a way to exclude them from the sitemap without disabling discovery altogether.

Suggested solution

Add an attribute, that marks the videos or images for exclusion, for example data-sitemap-ignore.

Alternative

No response

Additional context

No response

harlan-zw commented 2 months ago

Hi, thanks for the issue. This option actually does already exist but it missing documentation.

I've pushed some improvements to the docs.

export default defineNuxtConfig({
  sitemap: {
    discoverVideos: false,
  }
})
nirazul commented 2 months ago

@harlan-zw Thanks for your response and the time to improve the documentation!

However, I'd like to keep Video (and Image) Discovery. But I'd like to exclude only certain images. I'm interested in a solution with a data-attribute, for example:

<main>
  <!-- This video is included -->
  <video>
    <source src="/media/cc0-videos/flower.webm" type="video/webm" />
  </video>

  <!-- This video is excluded -->
  <video data-sitemap-ignore>
    <source src="/media/cc0-videos/flower.webm" type="video/webm" />
  </video>
</main>
harlan-zw commented 2 months ago

Hi, while this would be fairly straight forward it's not a type of customization I'd like to maintain.

A better solution for something like this is to use the Nitro hook which will let you customize the output however you like.

See https://nuxtseo.com/sitemap/nitro-api/nitro-hooks#sitemapresolved

Here's an example of how you'd do it

import { defineNitroPlugin } from 'nitropack/runtime'

export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('sitemap:resolved', (ctx) => {
    ctx.urls = ctx.urls.map((url) => {
      if (url.videos?.length) {
        url.videos = url.videos.filter((video) => {
          if (video.content_loc) {
            const url = new URL(video.content_loc)
            return url.host.startsWith('www.youtube.com') // only include youtube videos
          }
          return false
        })
      }
      return url
    })
  })
})
nirazul commented 1 month ago

@harlan-zw Ah, that's perfect. I wasn't aware that such a thing is configurable with a Nitro Hook! Awesome! Thanks for your help!

harlan-zw commented 1 month ago

No problem, just did a small update to that code block, old version wouldn't work :+1: