github twitter linkedin
Sitemap with Hakyll

Its fairly trivial to configure Hakyll to generate sitemaps. Sitemaps helps search engines websites. Similar to any typical html page, create a template - templates/sitemap.xml:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="">

Then, using the templates, create a rule which uses all the pages from the site as entries. Here is what I do:

  create ["sitemap.xml"] $ do
         route   idRoute
         compile $ do
           posts <- recentFirst =<< loadAll "posts/*/*"
           pages <- loadAll "pages/*"
           let allPosts = (return (pages ++ posts))
           let sitemapCtx = mconcat
                            [ listField "entries" pageCtx allPosts
                            , constField "host" host
                            , defaultContext
           makeItem ""
            >>= loadAndApplyTemplate "templates/sitemap.xml" sitemapCtx
            >>= cleanIndexHtmls

This generates /sitemap.xml which can be submitted to search engines for indexing.