jonasmerlin / astro-seo

Makes it easy to add information that is relevant for SEO to your Astro app.
MIT License
908 stars 50 forks source link

Add titleDefault and titleTemplate #56

Closed reaganchisholm closed 1 year ago

reaganchisholm commented 1 year ago

Hi there πŸ‘‹πŸ»

This PR adds titleDefault and titleTemplate fields that mostly match the functionality of its inspiration, next-seo. These are two fields that I found myself trying to reach for when using astro-seo but realized they weren't available. I saw the line about "feature-parity with Next SEO" mentioned in the README.md, so I figured I'd open a PR for these two fields.

PR Includes:

An example use-case for these fields would be using the <SEO /> component within a <Layout> while getting the page title via props, like so:

<!-- contact.astro -- titleTemplate example, becomes "Contact - Website Name" -->
<Layout title="Contact">
    <!--  [...] page content -->
</Layout>

<!-- about.astro - titleDefault example, forgot to provide a title, fallback to "Website Name - Cool Tagline" -->
<Layout>
   <!-- [...] page content -->
</Layout>

<!-- layout.astro -->
<html lang="en">
   <head>
      <SEO
         charset="UTF-8"
         title={title}
         titleTemplate="%s - Website Name"
         titleDefault="Website Name - Cool Tagline"
      />
   </head>
<!-- [...] -->

One minor side note: I went with titleDefault instead of defaultTitle to keep it consistent with the other title prop names. Another option could be titleFallback, or defaultTitle would be fine as well.

jonasmerlin commented 1 year ago

@reaganchisholm Oh wow, I love this! Thank you for adding this and sorry for the delay in getting this merged. Will build a new version with this in it today. πŸ‘