General
- Celebrating Eleventy 2.0 🎉 with Zach Leatherman • JS Party 🎙️
Why Eleventy?
- Transitioning to Eleventy from other environments (Gatsby, NextJS, WordPress, Jekyll) 📺 • The Eleventy Meetup
Config
- How to keep your Eleventy config file organized • Lene Saile
WebC
Filters
Wrap variables in a function call rather than piping variables to them.
Example:
<time datetime="${htmlDateString(post.date)}">${readableDate(post.date)}</time>
VS Code support
WebC syntax highlighting is available in VS Code by associating the .webc
extension with the HTML language:
{
"files.associations": {
"*.webc": "html"
}
}
Prettier support
Prettier doesn’t support WebC out of the box, but you can add support by adding a Prettier config file to your project and setting the parser
option to html
for files with the .webc
extension:
# see: https://prettier.io/docs/en/configuration.html#setting-the-parserdocsenoptionshtmlparser-option
overrides:
- files: ['*.webc']
options:
parser: 'html'
Links
- Eleventy 2.0 & WebC • Martin Hicks
- Understanding WebC Features and Concepts | 11ty Rocks!
- Adding Components to Eleventy with WebC—zachleat.com
- WebC Layouts with Layout Chaining « 11ty ♥ WebC
- WebC Inside Your Head « 11ty ♥ WebC
- Recipes « 11ty ♥ WebC
- Betting on WebC • David Luhr
- WebC • Eleventy docs
Drafts and Scheduled Posts in Eleventy
const removeDrafts = collection =>
process.env.NODE_ENV === 'production' ? collection.filter(item => item.data.published) : collection;
const removeScheduled = collection =>
process.env.NODE_ENV === 'production' ? collection.filter(item => item.date <= Date.now()) : collection;
Make sure the environment that builds for production sets NODE_ENV=production.
Netlify does that automatically, but if you build via a GitHub action, you’ll need to set it yourself.
Links
- https://remysharp.com/2019/06/26/scheduled-and-draft-11ty-posts - Scheduled and draft 11ty posts
- Quick Tip—Draft Posts using Computed Data • Eleventy
- extend with logic to only publish blog posts if they have a past date (so no “draft” or “published” property is needed in post frontmatter)?
- Permalinks • Eleventy docs
- use fileSlug to remove folder from path using “page.fileSlug” in “permalink” front matter setting
- Hiding posts with future dates in Eleventy • Saneef’s website
- alternate approach that updates the permalink and collection exclusion in
eleventyComputed.js
- alternate approach that updates the permalink and collection exclusion in
Add sitemap.xml
- Add a
sitemap.webc
page to your site withpermalink: /sitemap.xml
to output the file at the root of your site - Add the static outer markup
- Use
webc:type="js"
to output the dynamic markup (i.e. the list of pages) - Use
collections.all
to get all pages - Optionally sort by
page.url
to get a consistent order (I found this useful for debugging) - Check if
page.url
exists (i.e. isn’tfalse
because the page setspermalink: false
oreleventyExcludeFromCollections: true
) - Add logic to exclude pages that shouldn’t be in the sitemap (e.g. page.url
includes
.css`) - Add full URL to
<loc>
element - Add last modified date to
<lastmod>
element- Use “git Last Modified” for this?
- sitemaps.org - Protocol
- see for more info on the sitemap protocol and other elements you can add
- Take a look and make sure it includes all pages you expect and excludes pages you don’t
- sitemap.xml
- example component (less logic than mine)
- Another example that uses WebC attributes instead of the script tag to output the list:
- https://github.com/Gyanreyer/jess/blob/0456d40fe528f4cec79f4e7e98acf499a347105d/src/sitemap.webc
- [show example of this approach as well]
- the
path
example may be a little safer because it doesn’t make assumptions about where/
s have been added or not added to the URL components, so it’s less likely to break if you change those path strings later
Add robots.txt (separate post?)
Once you have a sitemap, you can add a robots.txt file to tell search engines where to find it.
- Add a
robots.webc
page to your site withpermalink: /robots.txt
to output the file at the root of your site - Exclude it from collections with
eleventyExcludeFromCollections: true
- Use
webc:type="js"
to output the dynamic markup (i.e. the path to the page)- You could also just hard code the path to make the file simpler if you don’t expect it to change
- [show static verson of page as a contrast]
- robots.txt
- example component (less content than mine)
- My PR adding both: https://github.com/ooloth/michaeluloth.com/pull/1
Using Tailwind CSS with Eleventy
To add postcss support to eleventy, follow the steps in the eleventy-plugin-postcss README:
- …
To add TailwindCSS support to postcss, follow the steps in the Tailwind postcss docs:
- …
Missing any other basic tailwind setup (like the config file in the root folder and entry point css)
Test by starting server and adding classes to a template.
If you use postcss-import, place your imported partials in the _includes folder so they’ll be watched for changes but not copied to the build folder like your main css file:
[Show example of importing from _includes/css in main file.]
I found this works better than solutions that rely on updating npm scripts and using the tailwind CLI. This solution is simpler and results in browser updates that don’t require a hard refresh.
Add Favicons and a Web Manifest to Eleventy
- Generate favicon markup and manifest.json from a single image
- use https://realfavicongenerator.net/
- or https://favicomatic.com
- choose desired options
- can preview color choices
- optionally add
/favicons
as subfolder to keep things tidy
- Download icons folder and add to your site’s
/public
folder- Ensure you are copying the contents of
/public
to your output directory- [show
.eleventy.js
config lines for this]
- [show
- Ensure you are copying the contents of
- Copy markup and add to your site’s head
- update paths as needed (e.g.
site.webmanifest
path, subfolder if missing)- the
.webmanifest
extension is special and preferred over.json
for manifest files
- the
- [show example of this in my
root.webc
layout component]
- update paths as needed (e.g.
- Create
manifest.webc
component withpermalink: manifest.json
to output at root- Optionally pull values from your global site data file to make it easier to update those values later
- [show example of this in my
site.js
component] - Example
manifest.webc
component: https://github.com/solution-loisir/webc-starter-kit/blob/efc5170a336e65425ec9a32a81277d1bfaeca69f/src/manifest.webc
- How to test when done?
- In Chrome/Brave, open DevTools > Application > Manifest
- Address any warnings or issues
- In Chrome/Brave, open DevTools > Lighthouse and run an audit with “Progressive Web App” checked
- This will check for any issues with your manifest.json and favicon markup
- [show example of this]
- In Chrome/Brave, open DevTools > Application > Manifest
- https://developer.mozilla.org/en-US/docs/Web/Manifest
- details about manifest file
TypeScript Support
- How to use TypeScript with Eleventy? · Issue · 11ty/eleventy
- ⭐️ JSX, Typescript, and TSX in 11ty.js
- pauleveritt/eleventy-vite-preact • Illustration of 11ty with Vite+TS and TSX via Preact
- pauleveritt/g3-tsx • Prototype for Guide 3 with Eleventy, TSX, and Vite.
- jahilldev/11tyby • Simple 11ty setup using TypeScript, SASS, Preact with partial hydration, and other useful things. Aims to provide the DX of Gatsby, but using 11ty!
- pdehaan/11ty-ts
- Template Language—Custom — Eleventy
- antoBit/antodev
Inbox
- General:
- https://davidea.st/articles/11ty-tips-i-wish-i-knew-from-the-start/
- Is 11ty actually a JavaScript framework? - https://www.youtube.com/watch?v=0av40V5cwcQ - David east showing how to create components in 11ty with nunjucks shortcodes and web components (sync for passing props, async for fetching data)
- Shortcodes:
- https://github.com/adamduncan/eleventy-shortcomps - examples of 11ty components created via shortcodes
- page variable - Eleventy Supplied Data — Eleventy