From a3c2061451a28fafe8da1b177260ea2481fb69a3 Mon Sep 17 00:00:00 2001 From: Jezen Thomas Date: Mon, 30 Mar 2020 11:55:37 +0700 Subject: [PATCH] Add functions for setting description and OG meta It's common that a website author will want to add a specific description and Open Graph image to a given page in their website. These functions are simple conveniences to add these meta tags to the document head. I decided against simply adding all possible meta tags, because not all of them are useful, and even in the case of Open Graph tags, many of them should be set only once and they should use the same value for the entire website. In those cases, it's probably better for the website author to add those tags in their layout template. Closes https://github.com/yesodweb/yesod/issues/1659 --- yesod-core/ChangeLog.md | 4 ++ yesod-core/src/Yesod/Core/Widget.hs | 77 +++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/yesod-core/ChangeLog.md b/yesod-core/ChangeLog.md index 3b27eb4b6..518e96b3e 100644 --- a/yesod-core/ChangeLog.md +++ b/yesod-core/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog for yesod-core +## 1.6.18 + +* Add functions for setting description and OG meta + ## 1.6.17.3 * Support for `unliftio-core` 0.2 diff --git a/yesod-core/src/Yesod/Core/Widget.hs b/yesod-core/src/Yesod/Core/Widget.hs index 4c37289c9..bba1a277e 100644 --- a/yesod-core/src/Yesod/Core/Widget.hs +++ b/yesod-core/src/Yesod/Core/Widget.hs @@ -29,6 +29,10 @@ module Yesod.Core.Widget -- ** Head of page , setTitle , setTitleI + , setDescription + , setDescriptionI + , setOGType + , setOGImage -- ** CSS , addStylesheet , addStylesheetAttrs @@ -160,18 +164,83 @@ instance ToWidgetHead site Javascript where instance ToWidgetHead site Html where toWidgetHead = toWidgetHead . const --- | Set the page title. Calling 'setTitle' multiple times overrides previously --- set values. +-- | Set the page title. +-- +-- Calling @setTitle@ or @setTitleI@ multiple times overrides previously set +-- values. +-- +-- SEO Notes: +-- +-- * Title tags are the second most important on-page factor for SEO, after +-- content +-- * Every page should have a unique title tag +-- * Start your title tag with your main targeted keyword +-- * Don't stuff your keywords +-- * Google typically shows 55-64 characters, so aim to keep your title +-- length under 60 characters setTitle :: MonadWidget m => Html -> m () setTitle x = tell $ GWData mempty (Last $ Just $ Title x) mempty mempty mempty mempty mempty --- | Set the page title. Calling 'setTitle' multiple times overrides previously --- set values. +-- | Set the localised page title. +-- +-- n.b. See comments for @setTitle@ setTitleI :: (MonadWidget m, RenderMessage (HandlerSite m) msg) => msg -> m () setTitleI msg = do mr <- getMessageRender setTitle $ toHtml $ mr msg +-- | Add description meta tag to the head of the page +-- +-- Google does not use the description tag as a ranking signal, but the +-- contents of this tag will likely affect your click-through rate since it +-- shows up in search results. +-- +-- The average length of the description shown in Google's search results is +-- about 160 characters on desktop, and about 130 characters on mobile, at time +-- of writing. +-- +-- Source: https://www.advancedwebranking.com/blog/meta-tags-important-in-seo/ +-- +-- @since 1.6.18 +setDescription :: MonadWidget m => Text -> m () +setDescription description = + toWidgetHead $ [hamlet||] + +-- | Add translated description meta tag to the head of the page +-- +-- n.b. See comments for @setDescription@. +-- +-- @since 1.6.18 +setDescriptionI + :: (MonadWidget m, RenderMessage (HandlerSite m) msg) + => msg -> m () +setDescriptionI msg = do + mr <- getMessageRender + toWidgetHead $ [hamlet||] + +-- | Add OpenGraph type meta tag to the head of the page +-- +-- See all available OG types here: https://ogp.me/#types +-- +-- @since 1.6.18 +setOGType :: MonadWidget m => Text -> m () +setOGType a = toWidgetHead $ [hamlet||] + +-- | Add OpenGraph image meta tag to the head of the page +-- +-- Best practices: +-- +-- * Use custom images for shareable pages, e.g., homepage, articles, etc. +-- * Use your logo or any other branded image for the rest of your pages. +-- * Use images with a 1.91:1 ratio and minimum recommended dimensions of +-- 1200x630 for optimal clarity across all devices. +-- +-- Source: https://ahrefs.com/blog/open-graph-meta-tags/ +-- +-- @since 1.6.18 +setOGImage :: MonadWidget m => Text -> m () +setOGImage a = toWidgetHead $ [hamlet||] + -- | Link to the specified local stylesheet. addStylesheet :: MonadWidget m => Route (HandlerSite m) -> m () addStylesheet = flip addStylesheetAttrs []