withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.65k stars 2.48k forks source link

`pagesGlobToRssItems` throwing unexpectedly with apparently valid items #7039

Closed Lauro235 closed 1 year ago

Lauro235 commented 1 year ago

I have tried reaching out to the community multiple times, and haven't had any support so I decided to open an issue.

Problem - import.meta.glob doesn't appear to work inside rss.xml.js file (as part of blog tutorial). I really don't know why the path wouldn't be searching through the inner folders of pages (I assume that is what the ./*/ part does?) I've tried getting specific with `"./posts/.md"` but still not having any luck. Does any configuration need to happen to use the vite import.meta.glob() code?

I have

I've made the file rss.xml.js in the pages folder

[13:47] User ~/.../astro/blog_example/src/pages (main *%=) $ ls about.astro blog.astro index.astro posts/ rss.xml.js tags/

I copy in the following

import rss, { pagesGlobToRssItems } from '@astrojs/rss';

export async function get() {
  return rss({
    title: 'Astro Learner | Blog',
    description: 'My journey learning Astro',
    site: 'https://my-blog-site.netlify.app',
    items: await pagesGlobToRssItems(import.meta.glob('./**/*.md')),
    customData: `<language>en-us</language>`,
  });
}

I get the build error attached. image

When I change the path to "./*.md"

I get the simple site map

<rss version="2.0">
  <channel>
    <title>Astro Learner | Blog</title>
    <description>My journey learning Astro</description>
    <link>https://blog-example-js.netlify.app/</link>
    <language>en-uk</language>
  </channel>
</rss>

Find reproducible example here https://stackblitz.com/edit/github-tstu91?file=src%2Fpages%2Frss.xml.js

The build command breaks...

Thanks for your help.

delucis commented 1 year ago

Thanks again for this report @Lauro235 and sorry you had trouble with the tutorial. I can reproduce the error in the Stackblitz you provided but can’t figure out why pagesGlobToRssItems is throwing that error — I don’t think it should be.

I’ll transfer this issue to the main astro repo for someone to debug.

For anyone reading this, there’s also a linked Discord thread for this issue although not much more to add other than that I tried to debug this but couldn’t figure out why pagesGlobToRssItems is throwing — inspecting items manually shows they all have a valid url.

TheOtterlord commented 1 year ago

Thanks for reporting this! After debugging, it looks like the cause was an invalid pubDate value in post-2.md.

-pubDate: 23-05-01
+pubDate: 2023-05-01

Even though it's an invalid date, it parses the Zod schema as it's a valid string. Since the error happens in the transformation, it's not caught. I'll make a PR to fix this, but hopefully this resolves your issue for now.

Lauro235 commented 1 year ago

Wow, thanks for getting back to me. Massively appreciate that!

If it's not to much trouble for you could you give me some tips on how you debugged this? Any good resources relating to debugging that you would recommend. If that's too much trouble, then no problem, but thanks for your time!

On Thu, May 11, 2023 at 3:19 PM Reuben Tier @.***> wrote:

Thanks for reporting this! After debugging, it looks like the cause was an invalid pubDate value in post-2.md https://stackblitz.com/edit/github-tstu91-guvfq9?file=src%2Fpages%2Frss.xml.js,src%2Fpages%2Fposts%2Fpost-2.md .

-pubDate: 23-05-01+pubDate: 2023-05-01

Even though it's an invalid date, it parses the Zod schema as it's a valid string. Since the error happens in the transformation, it's not caught. I'll make a PR to fix this, but hopefully this resolves your issue for now.

— Reply to this email directly, view it on GitHub https://github.com/withastro/astro/issues/7039#issuecomment-1544072128, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVX54QI7ZUSKOCZEUGJSU2LXFTYPXANCNFSM6AAAAAAX2B6EQA . You are receiving this because you authored the thread.Message ID: @.***>

-- Lorentz Bloom He / Him

56 Queen Adelaide Road SE20 7DX 07491644437

TheOtterlord commented 1 year ago

Honestly, it was just a series of console.log statements :sweat_smile:

I logged out the result and after copying it in to check for any weird promise issues (removing promises entirely by hardcoding the result) I noticed one of them was an invalid date. After that I dug through the schema in the source code for @astrojs/rss and found the field for pubDate had a conversion, which in this case caused the date to be "validated" even though it was an invalid date.

Lauro235 commented 1 year ago

Amazing man! 👏👏

Do you mind sharing me the console.log() code you used to log out the result? I'm not sure what you mean by the result. I understand the part about hardcoding in the result and removing promises from the problem, but not sure what you meant by copying in the result? Do you mean you looked at it and compared it to what should have been written from the docs? Looking through the astrojs/rss source code was the right move for sure though. 🙏

I'm still very new to blogs/rss/front matter etc so I wouldn't have known where to start in terms of debugging. Hopefully with your tips, I'll be able to continue getting better at debugging these kinds of problems.

I'm hungry to learn, but I'm completely fine if you need to do other things with your day than email me back with tips and such. Your help so far has been awesome.

On Thu, May 11, 2023 at 3:33 PM Reuben Tier @.***> wrote:

Honestly, it was just a series of console.log statements 😅

I logged out the result and after copying it in to check for any weird promise issues (removing promises entirely by hardcoding the result) I noticed one of them was an invalid date. After that I dug through the schema in the source code for @astrojs/rss and found the field for pubDate https://github.com/withastro/astro/blob/main/packages/astro-rss/src/schema.ts#LL5C3 had a conversion, which in this case caused the date to be "validated" even though it was an invalid date.

— Reply to this email directly, view it on GitHub https://github.com/withastro/astro/issues/7039#issuecomment-1544097707, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVX54QPX3ZY4A64VO3MFSMTXFT2B7ANCNFSM6AAAAAAX2B6EQA . You are receiving this because you authored the thread.Message ID: @.***>

-- Lorentz Bloom He / Him

56 Queen Adelaide Road SE20 7DX 07491644437

TheOtterlord commented 1 year ago

Yeah, I logged the result of pagesGlobToRSSItems to get the array of results, and check it looked to be the shape expected by the schema defined in the source code.

console.log(await pagesGlobToRssItems(import.meta.glob('./**/*.md')));

The output was the array of posts, but while copying it over to the items of the rss function, I found the second post had Invalid Date.

 [
   {
     title: 'My First Blog Post',
     pubDate: 2023-05-01T00:00:00.000Z,
     description: 'This is my first blog.',
     author: 'Ldog',
     link: '/posts/post-1'
   },
   {
     title: 'My Second Blog Post',
+    pubDate: Invalid Date,
     description: "After learning some astro, I couldn't stop!",
     author: 'Astro Learner',
     link: '/posts/post-2'
   },
   {
     title: 'My Third Blog Post',
     pubDate: 2023-05-01T00:00:00.000Z,
     description: 'I had some challenges, but asking in the community really helped!',
     author: 'Astro Learner',
     link: '/posts/post-3'
   },
   {
     title: 'My Fourth Blog Post',
     pubDate: 2022-08-08T00:00:00.000Z,
     description: 'This post will show up on its own!',
     author: 'Astro Learner',
     link: '/posts/post-4'
   }
 ]
bluwy commented 1 year ago

Fixed in https://github.com/withastro/astro/pull/7066

Lauro235 commented 1 year ago

Thanks so much for the explanation Reuben. Really enjoying Astro. Take care.

On Thu, 11 May 2023, 16:47 Reuben Tier, @.***> wrote:

Yeah, I logged the result of pagesGlobToRSSItems to get the array of results, and check it looked to be the shape expected by the schema defined in the source code.

console.log(await pagesGlobToRssItems(import.meta.glob('./*/.md')));

The output was the array of posts, but while copying it over to the items of the rss function, I found the second post had Invalid Date.

[ { title: 'My First Blog Post', pubDate: 2023-05-01T00:00:00.000Z, description: 'This is my first blog.', author: 'Ldog', link: '/posts/post-1' }, { title: 'My Second Blog Post',+ pubDate: Invalid Date, description: "After learning some astro, I couldn't stop!", author: 'Astro Learner', link: '/posts/post-2' }, { title: 'My Third Blog Post', pubDate: 2023-05-01T00:00:00.000Z, description: 'I had some challenges, but asking in the community really helped!', author: 'Astro Learner', link: '/posts/post-3' }, { title: 'My Fourth Blog Post', pubDate: 2022-08-08T00:00:00.000Z, description: 'This post will show up on its own!', author: 'Astro Learner', link: '/posts/post-4' } ]

— Reply to this email directly, view it on GitHub https://github.com/withastro/astro/issues/7039#issuecomment-1544237656, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVX54QL6DZGS7VN6FDQURS3XFUC2FANCNFSM6AAAAAAX2B6EQA . You are receiving this because you authored the thread.Message ID: @.***>