Home

Awesome

<div align="center"> <img alt="Astro Cactus logo" src="https://github.com/chrismwilliams/astro-theme-cactus/assets/12715988/85aa0d3c-ef6a-44e2-954d-ef035b4f4315" width="70" /> </div> <h1 align="center"> Astro Cactus </h1>

Astro Cactus is a simple opinionated starter built with the Astro framework. Use it to create an easy-to-use blog or website.

Table Of Contents

  1. Key Features
  2. Demo
  3. Quick start
  4. Preview
  5. Commands
  6. Configure
  7. Updating
  8. Adding Posts
  9. Pagefind search
  10. Analytics
  11. Deploy
  12. Acknowledgment

Key Features

Demo 💻

Check out the Demo, hosted on Netlify

Quick start

Create a new repo from this template.

# npm 7+
npm create astro@latest -- --template chrismwilliams/astro-theme-cactus

# pnpm
pnpm dlx create-astro --template chrismwilliams/astro-theme-cactus

Deploy with Netlify Deploy with Vercel

Preview

Astro Theme Cactus in a light theme mode

Astro Theme Cactus in a dark theme mode

Commands

Replace pnpm with your choice of npm / yarn

CommandAction
pnpm installInstalls dependencies
pnpm devStarts local dev server at localhost:3000
pnpm buildBuild your production site to ./dist/
pnpm postbuildPagefind script to build the static search of your blog posts
pnpm previewPreview your build locally, before deploying
pnpm syncGenerate types based on your config in src/content/config.ts

Configure

Updating

If you've forked the template, you can sync the fork with your own project, remembering to not click Discard Changes as you will lose your own.

If you have a template repository, you can add this template as a remote, as discussed here.

Adding posts

This theme utilises Content Collections to organise Markdown and/or MDX files, as well as type-checking frontmatter with a schema -> src/content/config.ts.

Adding a post is as simple as adding your .md(x) files to the src/content/post folder, the filename of which will be used as the slug/url. The posts included with this template are there as an example of how to structure your frontmatter. Additionally, the Astro docs has a detailed section on markdown pages.

Frontmatter

Property (* required)Description
title *Self explanatory. Used as the text link to the post, the h1 on the posts' page, and the pages title property. Has a max length of 60 chars, set in src/content/config.ts
description *Similar to above, used as the seo description property. Has a min length of 50 and a max length of 160 chars, set in the post schema.
publishDate *Again pretty simple. To change the date format/locale, currently en-GB, update the date option in src/site.config.ts. Note you can also pass additional options to the component <FormattedDate> if required.
updatedDateThis is an optional date representing when a post has been updated, in the same format as the publishDate. Note that by providing this field, the sorting function, found in src/utils/post.ts, sortMDByDate will order by this field rather than its published date.
tagsTags are optional with any created post. Any new tag(s) will be shown in yourdomain.com/posts & yourdomain.com/tags, and generate the page(s) yourdomain.com/tags/[yourTag]
coverImageThis is an optional object that will add a cover image to the top of a post. Include both a src: "path-to-image" and alt: "image alt". You can view an example in src/content/post/cover-image.md.
ogImageThis is an optional property. An OG Image will be generated automatically for every post where this property isn't provided. If you would like to create your own for a specific post, include this property and a link to your image, the theme will then skip automatically generating one.
draftThis is an optional property as it is set to false by default in the schema. By adding true, the post will be filtered out of the production build in a number of places, inc. getAllPosts() calls, og-images, rss feeds, and generated page[s]. You can view an example in src/content/post/draft-post.md

Frontmatter snippet

Astro Cactus includes a helpful VSCode snippet which creates a frontmatter 'stub' for a blog post, found here -> .vscode/post.code-snippets. Start typing the word frontmatter on your newly created .md(x) file to trigger it. Visual Studio Code snippets appear in IntelliSense via (⌃Space) on mac, (Ctrl+Space) on windows.

Pagefind search

This integration brings a static search feature for searching blog posts. In its current form, pagefind only works once the site has been built. This theme adds a postbuild script that should be run after Astro has built the site. You can preview locally by running both build && postbuild.

Search results only includes blog posts. If you would like to include other/all your pages, remove/re-locate the attribute data-pagefind-body to the article tag found in src/layouts/BlogPost.astro.

It also allows you to filter posts by tags added in the frontmatter of blog posts. If you would rather remove this, remove the data attribute data-pagefind-filter="tag" from the link in src/components/blog/Masthead.astro.

If you would rather not include this integration, simply remove the component src/components/Search.astro, and uninstall both @pagefind/default-ui & pagefind from package.json. You will also need to remove the postbuild script from here as well.

You can reduce the initial css payload of your css, as demonstrated here, by lazy loading the web components styles.

Analytics

You may want to track the number of visitors you receive to your blog/website in order to understand trends and popular posts/pages you've created. There are a number of providers out there one could use, including web hosts such as vercel, netlify, and cloudflare.

This theme/template doesn't include a specific solution due to there being a number of use cases and/or options which some people may or may not use.

You may be asked to included a snippet inside the HEAD tag of your website when setting it up, which can be found in src/layouts/Base.astro. Alternatively, you can add the snippet in src/components/BaseHead.astro.

Deploy

Astro docs has a great section and breakdown of how to deploy your own Astro site on various platforms and their idiosyncrasies.

By default the site will be built (see Commands section above) to a /dist directory.

Acknowledgment

This theme was inspired by Hexo Theme Cactus

License

MIT