aerni / statamic-advanced-seo

Comprehensive SEO addon for Statamic with flexibility in mind
https://statamic.com/addons/aerni/advanced-seo
11 stars 6 forks source link

Add GraphQL support for sitemaps #96

Closed aerni closed 1 year ago

aerni commented 1 year ago

This PR adds GraphQL support for sitemaps.

A basic query looks like this:

query MyQuery {
  seoSitemap {
    alternates {
      href
      hreflang
    }
    changefreq
    lastmod
    loc
    priority
  }
}

You can also filter by site:

query MyQuery {
  seoSitemap(site: "default") {
    loc
  }
}

It's also possible to filter by type and handle. Possible values for type are collection, taxonomy, and custom.

query MyQuery {
  seoSitemap(type: "collection", handle: "pages") {
    loc
  }
}

You may also change the baseUrl like this:

query MyQuery {
  seoSitemap(baseUrl: "https://foo.bar") {
    loc
  }
}
el-schneider commented 1 year ago

I only had very little time to check but it already looks great! 😍

One thing I noticed though is that it seems, that there is no option to alter the base URL. This would be a very welcome addition still.

maartenvanhunsel commented 1 year ago

@aerni approved, looking good. @el-schneider you can add your frontend_url by the code below in sites.php.

 'sites' => [
        'default' => [
            'name' => config('app.name'),
            'locale' => 'nl_NL',
            'url' => env('FRONTEND_URL', '/'),
        ],
    ],
aerni commented 1 year ago

@maartenvanhunsel Awesome! Thanks for taking the time. @el-schneider Can you confirm that adding the frontend URL to the sites config does what you need?

el-schneider commented 1 year ago

Not really, because this also affects asset urls and a plugin I'm using to generate srcsets. But it's fine. I'll just transform the urls myself.

aerni commented 1 year ago

Oh ok. Will look into adding base_url.

aerni commented 1 year ago

I added the option to change the baseUrl. Do you mind giving it a try?

maartenvanhunsel commented 1 year ago

@aerni tested and works like expected. Would you mind to merge :D

aerni commented 1 year ago

Thanks for testing! After looking at it again, I think I might want to separate the collection, taxonomy, and custom sitemaps and structure the query similarly to how the seoDefaults query is structured.

It would look like this:

query MyQuery {
  seoSitemap {
    collection(handle: "pages") {
      loc
    }
    taxonomy(handle: "tags") {
      loc
    }
    custom {
      loc
    }
  }
}

This way, we could get rid of the type argument. And it's always clear which type of sitemap URLs you get. Also, it would more closely resemble the way Advanced SEO's XML sitemaps are structured.