satnaing / astro-paper

A minimal, accessible and SEO-friendly Astro blog theme
https://astro-paper.pages.dev/
MIT License
2.12k stars 440 forks source link

Type Error when there are no published posts #266

Closed sherwinski closed 4 months ago

sherwinski commented 4 months ago

Hello and thanks for providing this great theme 👋

Issue

I noticed that the site breaks when there are no published posts under the blog/ directory. Even though this isn't typically the case, I at least expected the rendering logic fallback to some empty state.

Steps to Reproduce

Clone down and run the project:

git clone git@github.com:satnaing/astro-paper.git
cd astro-paper/
npm i && npm run dev

End the local server, delete all posts under blog/ directory, and run the project again:

rm src/content/blog/*
npm run dev

The home page gives the following error and stacktrace:

Screenshot 2024-02-14 at 12 27 51 PM
Stack Trace
TypeError: Cannot read properties of undefined (reading 'filter')
    at Module.getSortedPosts [as default] (/home/astro-paper/src/utils/getSortedPosts.ts:6:6)
    at /home/astro-paper/src/pages/index.astro:15:21
    at async callComponentAsTemplateResultOrResponse (file:///home/astro-paper/node_modules/astro/dist/runtime/server/render/astro/render.js:85:25)
    at async renderToReadableStream (file:///home/astro-paper/node_modules/astro/dist/runtime/server/render/astro/render.js:35:26)
    at async renderPage (file:///home/astro-paper/node_modules/astro/dist/runtime/server/render/page.js:29:12)
    at async renderPage (file:///home/astro-paper/node_modules/astro/dist/core/render/core.js:50:20)
    at async #tryRenderRoute (file:///home/astro-paper/node_modules/astro/dist/core/pipeline.js:110:18)
    at async DevPipeline.renderRoute (file:///home/astro-paper/node_modules/astro/dist/core/pipeline.js:58:20)
    at async handleRoute (file:///home/astro-paper/node_modules/astro/dist/vite-plugin-astro-server/route.js:248:18)
    at async run (file:///home/astro-paper/node_modules/astro/dist/vite-plugin-astro-server/request.js:50:14)



If you're open to it, I'd be happy to open a PR to add some fallback logic to catch this. Thanks and let me know what you think.

jtara1 commented 4 months ago

Even if you fix that, you'll get an error from npx astro build --verbose

I'm just deleting the default contents in the src/content/blog/ then adding placeholder.md to it with contents

---
author: jtara1
pubDatetime: 2022-09-23T15:22:00Z
modDatetime: 2023-12-21T09:12:47.400Z
title: placeholder
slug: placeholder
featured: true
draft: false
tags:
  - docs
description:
  placeholder desc
---
sherwinski commented 4 months ago

Yeah good point. It would be convenient if getCollection("blog") could return an empty array of blogSchema, rather than unknown.

Gonna go ahead and close this as @jtara1's suggestion is the best way forward from what I can tell.