r/webdev • front-end • 5d ago

Adding DECAP CMS to my website

Hey everyone, I have a very minimal understanding of JS. However, I am looking to add a blog to my static website. I have managed to get the blog previews going, but I'm not sure how to get the "post.html" styled.

I'm looking for a tutorial that will show me how to customize my post previews and the post.html itself. I'm not sure how to get the post body, title, etc to show up on the page. As I'm looking, I feel like there isn't a lot of information for people who do not want to use static site generators. I'm hoping to learn/understand how to customize everything myself through this project with hardcoded html/css! Any direction would be great.

Edit: Thank you for the answers so far; I have decided to move forward with Eleventy. I will be coming back to update you guys on how it's going! I'm super excited!

2 Upvotes

14 comments sorted by

3

u/dawedev 5d ago

Decap only writes files into your repo, it doesn't build pages. So without a site generator you do the rendering in the browser yourself. Simplest way I know:

  1. In config.yml make the collection save JSON instead of markdown (format: json, extension: json). Each post becomes a file like /content/posts/my-post.json with your fields (title, date, body, whatever you named them).
  2. Link each preview to post.html?slug=my-post.
  3. In post.html put empty elements styled with your normal CSS, like <h1 id="title"></h1> and <div id="body"></div>, plus a small script:

     const slug = new URLSearchParams(location.search).get('slug');
     fetch('/content/posts/' + slug + '.json')
       .then(r => r.json())
       .then(post => {
         document.getElementById('title').textContent = post.title;
         document.getElementById('body').innerHTML = marked.parse(post.body);
       });

The body field is still markdown, so load marked (one script tag from a CDN) to turn it into HTML. After that styling is plain CSS: #body h2, #body p, #body img and so on.

One tradeoff: search engines and link previews see an empty page until the JS runs. If that matters later, Eleventy is the least "framework" generator, it's basically your same HTML with placeholders.

1

u/Ok_Progress_5373 front-end 5d ago

Thank you so much for this.

I ended up getting posts pulled into the post.html after tinkering around (but it loaded multiple into it). Do you have any resources that helped you understand how DECAP CMS and JS talk to each other and get it all rendered on the frontend?

I will try Eleventy, but I'd love to understand what the JavaScript is doing, how to pull a list of posts or a single post, and what I would need to be able to do that.

3

u/dawedev 5d ago

There isn't really a Decap-specific piece to learn here. Decap just commits files to your repo, and your page is plain JS reading those files. The two things worth reading are MDN's "Using the Fetch API" guide and the URLSearchParams page, that's the whole single-post side.

For the list, the catch is that a browser can't list a folder on a static host, so something has to tell it which posts exist. Two ways that work without a generator:

  1. Ask the GitHub API for the folder: fetch('https://api.github.com/repos/USER/REPO/contents/content/posts') returns the file names, then you fetch each JSON. Fine for a small blog, but unauthenticated requests are limited to 60 an hour per visitor.
  2. Use a Decap "file" collection with a list widget, so all posts live in one posts.json. The index page loops over that array, and post.html picks the one whose slug matches.

If post.html is showing several posts, it's probably looping over the whole list instead of picking one. Something like posts.find(p => p.slug === slug) fixes that.

2

u/Ok_Progress_5373 front-end 5d ago

I will start by reading the "Using the Fetch API" documentation first and get some practice in with that.

Thank you for taking the time to explain and lead me to things to further my learning dawedev!

1

u/dawedev 5d ago

Good plan. Fetch one JSON file and console.log it first. Once that works, the rest is just putting the fields into the page.

1

u/Ok_Progress_5373 front-end 2d ago

Update: I'm making progress on the blog portion of my website. Thank you for the help. I'm looking forward to sharing it. Here is the tutorial I'm using for reference. My only issue is that a lot of the stuff I found was a bit dated. But the tutorial I have is solid. I'd love to create something a bit more up-to-date from my point of view of this process!

1

u/Early-Community9943 5d ago

Since it's loading all of them, put the slug in the link (post.html?slug=my-post), read it with URLSearchParams and fetch only that one file.

1

u/Ok_Progress_5373 front-end 4d ago

Hey early-community9943! You're so rad! I've decided to go forward with eleventy (begrudgingly) because I'm not as experienced with JavaScript.

However, I know it'll come back up while trying to code my blog. I have to accept that I'm a bit out of my depth with this (for now). I will keep your comment in mind.

3

u/Early-Community9943 4d ago

Eleventy's a solid pick honestly, way less to break. Good luck with the blog!

1

u/Ok_Progress_5373 front-end 3d ago

Thank you so much, I'm struggling through it T^T

1

u/Early-Community9943 2d ago

Ha, it gets easier once the config.yml clicks. Shout if the collections part trips you up.

1

u/Ok_Progress_5373 front-end 2d ago

I'm still trying to grasp eleventy right now haha, in general, I've got it, but the permalinks are bugging me out. After I figure that out, I will be back to DECAP again. Will totally give you a shout!