Render a JSON page in Astro

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.

Creating a JSON page in Astro

However…


This content originally appeared on DEV Community and was authored by Chris Bongers

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.

Creating a JSON page in Astro

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.

  • title
  • description
  • slug

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, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter


This content originally appeared on DEV Community and was authored by Chris Bongers


Print Share Comment Cite Upload Translate Updates
APA

Chris Bongers | Sciencx (2021-10-06T05:57:14+00:00) Render a JSON page in Astro. Retrieved from https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/

MLA
" » Render a JSON page in Astro." Chris Bongers | Sciencx - Wednesday October 6, 2021, https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/
HARVARD
Chris Bongers | Sciencx Wednesday October 6, 2021 » Render a JSON page in Astro., viewed ,<https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/>
VANCOUVER
Chris Bongers | Sciencx - » Render a JSON page in Astro. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/
CHICAGO
" » Render a JSON page in Astro." Chris Bongers | Sciencx - Accessed . https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/
IEEE
" » Render a JSON page in Astro." Chris Bongers | Sciencx [Online]. Available: https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/. [Accessed: ]
rf:citation
» Render a JSON page in Astro | Chris Bongers | Sciencx | https://www.scien.cx/2021/10/06/render-a-json-page-in-astro/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.