Render a JSON page in Astro

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I've been working on a search solution for my Astro blog, and building search on top of static site generators is always tricky.
My general idea would be to do it almost the same as my Eleventy search.
However, I quickly realized Astro doesn't have a neat permalink structure by default.
Trying out some things, I learned that we could create pages like search.json.astro.
These will render as http://yoursite.com/search.json
But let's see how we can render a JSON response of all our blog posts in there.
Astro has the cool built-in fetch method for internal pages so let's use that first.
const allPosts = Astro.fetchContent('./posts/*.md');
In the next step, I'd like to map these to the output that I can use, which only needs the following three elements.
Let's see how that would look:
allPosts.map((p) => {
return {
title: p.title,
description: p.description,
slug: p.url,
};
});
However, let's create a variable from this, and JSON stringify the output.
const json = JSON.stringify(
allPosts.map((p) => {
return {
title: p.title,
description: p.description,
slug: p.url,
};
})
);
Now all that's left is to render the JSON output on the page.
// All our code
---
{json}
And that's it. We can now leverage a simple JSON file for our search to use.
You can find my example code on the following file. Or see the end JSON file here.
Note: This process might change if Astro will support this eventually, but for now, this seems like an excellent approach.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter