š§ New Site, Who This?
2nd Jan - 2020Iāve done it again. Created the blog again. From scratch. Again? Kind of.
I didnāt like my first site. It was made from a template. Iām better than that. No disrespect to the author, but I like to write my own code and thatās okay.
In early 2020 I tried to rebuild it, but didnāt like it. I was also in a bit of a slump, because I didnāt like my job. I donāt like it now, but Iām getting back in to coding because thatās what I want to do. Thatās a different story. A different post.
The old site, currently hosted at hjf.io, was from a template: Lumen. I liked the template, plenty of white with some black and an accent colour. Sound familiar?
Iāve also been toying with Tailwind and MDX. Iāve also tried Next - didnāt like it. I wanted to play with all the tech that I liked and do away with those that I didnāt.
The stack for this website is:
- Gatsby
- TailwindCSS
- MDX (and frontmatter ā¤ļø)
- Netlify for hosting
- Netlify functions
I donāt have a pretty architecture diagram for you. Else, Iād have spent my time making a powerpoint and not doing anything productive.
āļø Gatsby Config
To start I picked up the default Gatsby starter. This is the aptly named gatsby-starter-default
.
yarn global add gatsby-cli
gatsby new . https://github.com/gatsbyjs/gatsby-starter-default
gatsby develop
Now that there is a default Gatsby skeleton running, weāre got to add plugins. A lot of plugins.
This is the way.
Iām in two minds about plugins. On the one hand, I love them: You pull in everything that you need do to your task and it just (mostly) works. Itās a bit of lego-programming.
On the other hand, I think it pushes a mindset of āget a library to do it, donāt program itā.
I donāt like the extreme part of this, where everythingās just libraries linked. You donāt get a lot of control, or understanding of your code. I donāt htink you grow much as a developer, either.
I think this is why, in part, I didnāt want to rewrite the original site. I donāt have as much control over the code running in the background.
Thereās an argument to be made for using libraries and abstraction though. Letās not reinvent the wheel. Unless we can get 4 of them and slap a turbo on it.
Anyway, the Gatsby way is great. Use Markdown to format text, use GraphQL to fetch data and use plugins to enhance your site. Itās nice to get a āwayā to do something in React.
Iāve kept with the some base plugins this time. I wanted to keep it simple enough to maintain, but give a slick user experience for when I show the site to my mum.
Plugins in use are:
- mdx
- react-helmet
- sass
- sharp
- prismjs
- smartypants
- filesystem
Note: at this point, if you intend on using MDX, you need to make a few config changes. It took me a while to figure this out. Donāt be like me, steal code from me.
Add the correct MDX dependencies:
yarn add gatsby-plugin-mdx gatsby-plugin-filesystem @mdx-js/mdx @mdx-js/react
Configure gatsby-plugin-filesystem
and point it to your mdx files!
Once Iād configured my plugins, I set out to create one template (for now), as well as a page for home, blog posts and quick hacks (these are cool). My about and /uses both use mdx.
š MDX Skel
Finally, I wanted to make pages easy to modify. Ease of editing with Markdown. Custom configuration with Frontmatter. Added functionality with React. MDX provides all of these (Frontmatter is afforded via gatsby-plugin-mdx
).
For the uninitiated, Frontmatter adds some bonus YAML (ew) to the header of any Markdown/MDX page. This is metadata that we can query. My pages use:
- Title: what to show on the page
- Date: Some pretty metadata
- Type: Blog/Hack/Other - This lets me filter for my hacks/blog pages. Remember that /uses and about arenāt a blog or a hack
- Path: Where the page should live
- Description: Something to entice the reader
- Feature Image: Ooh shiney!
š¦ TailwindCSS
At the time of writing, Tailwind has just hit 2.0. This is great, but gatsby-plugin-sass
uses PostCSS 7, which isnāt compatible. I had to pull in a compabitility build
npm uninstall tailwindcss postcss autoprefixer
npm install tailwindcss@npm:@tailwindcss/postcss7-compat @tailwindcss/postcss7-compat postcss@^7 autoprefixer@^9
š” Homepage
Iām pretty proud of the homepage. As well as the 4 most recent blog posts, it shows my most recent (public) commits on Github and my most recent Tweets. Each of these modules work differently.
Thereās some basic logic to the blog posts: do it the gatsby way, fetch the posts via graphql. Iāll chat more about frontmatter later.
query PageContentQuery {
allMdx(limit: 4, filter: {frontmatter: {type: {eq: "blog"}}}, sort: {fields: frontmatter___date, order: DESC}) {
edges {
node {
id
timeToRead
frontmatter {
path
title
date
description
featureImg {
childImageSharp {
fluid(maxWidth: 400) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
}
}
From the beginning, Twithub and Gitter work the same: When the homepage is built, use the onCreatePage
hook in gatsby-node.js
to fetch the data fro the respective websites and add them to the page context. Iāll talk more about this in another post though.
This is fine - but Iām likely to write code after this is published. On page load, the modules simply use a custom hook to refresh the data.
The Github functionality calls the same function that gets called in the page building process. Twitter is slightly different thoughā¦ Their API is a little contrived and I need a key to call read my own tweets. Iāve abstracted this in to a Netlify function, which Iāll detail in another postā¦ Their documentation is somewhat lackluster.
The other pages, Blog and Hacks just pull in all mdx pages where their type === the page type. Thereās not much interesting there.
š Wrap-up
And thatās it! I just started coding from there, threw a few deployment errors, struggled with DNS. We got this thing hosted.
Iām already really enjoying working on this site. Itās looking better than I thought it would. I can add lots of functionalty via MDX, too. Weāre in the early days and Iām excited to see how the thing evolved.
There were a few posts that I gave reference to -
- Working with Netlify functions
- Injecting data in to a Gatsby page without GraphQL (To write)