Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use RSS with content folder #5619

Closed
1 task
louiss0 opened this issue Dec 17, 2022 · 13 comments
Closed
1 task

Can't use RSS with content folder #5619

louiss0 opened this issue Dec 17, 2022 · 13 comments

Comments

@louiss0
Copy link
Contributor

louiss0 commented Dec 17, 2022

What version of astro are you using?

1.7.0

Are you using an SSR adapter? If so, which one?

NONE

What package manager are you using?

pnpm

What operating system are you using?

Windows

Describe the Bug

I can't use RSS with Astro when I try to use the content folder to render the rss stuff it will not work.

This is the error that I get when running the code.


 Cannot read properties of undefined (reading 'replace')
  File:
    C:\Users\louis\Desktop\web-projects\ui\test-windows\node_modules\@astrojs\rss\dist\util.js:2:22
  Code:
    1 | function createCanonicalURL(url, base) {
    > 2 |   let pathname = url.replace(/\/index.html$/, "");
        |                      ^
      3 |   pathname = pathname.replace(/\/1\/?$/, "");
      4 |   if (!getUrlExtension(url))
      5 |     pathname = pathname.replace(/(\/+)?$/, "/");
  Stacktrace:
TypeError: Cannot read properties of undefined (reading 'replace')
    at createCanonicalURL (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/@astrojs/rss/dist/util.js:2:22)
    at file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/@astrojs/rss/dist/index.js:82:62
    at Array.map (<anonymous>)
    at generateRSS (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/@astrojs/rss/dist/index.js:80:33)
    at getRSS (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/@astrojs/rss/dist/index.js:38:17)    
    at Module.get (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/dist/entry.mjs?time=1671239724287:2315:3)     
    at renderEndpoint (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/runtime/server/endpoint.js:60:18)
    at call (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/endpoint/index.js:70:26)
    at async generatePath (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/generate.js:271:20)
    at async generatePage (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/generate.js:110:5)
    at async generatePages (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/generate.js:69:7)
    at async staticBuild (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/static-build.js:68:7)
    at async AstroBuilder.build (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/index.js:86:5)
    at async AstroBuilder.run (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/index.js:127:7)
    at async build (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/core/build/index.js:21:3)
    at async runCommand (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/cli/index.js:150:14)
    at async cli (file:///C:/Users/louis/Desktop/web-projects/ui/test-windows/node_modules/astro/dist/cli/index.js:168:5)    

 ELIFECYCLE  Command failed with exit code 1.

This is what my config looks like

rss-config

Link to Minimal Reproducible Example

https://github.com/louiss0/test-windows

Participation

  • I am willing to submit a pull request for this issue.
@fflaten
Copy link
Contributor

fflaten commented Dec 17, 2022

👍 Ideally we'd be able to use getCollection() API directly as this is the type of scenario it's really useful for.

Ugly workaround:

const slugRegex = /^src\/content\/blog\/(.*?)\.mdx?$/i
...

link: new URL(`/posts/${post.url.replace(slugRegex, '$1/')}`, import.meta.env.SITE).toString(),

@andersk
Copy link
Contributor

andersk commented Dec 17, 2022

You can use the getCollection() API directly.

import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
import { SITE_TITLE, SITE_DESCRIPTION } from '../config';

const posts = await getCollection('blog');

export const get = () =>
  rss({
    title: SITE_TITLE,
    description: SITE_DESCRIPTION,
    site: import.meta.env.SITE,
    items: posts.map((post) => ({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.pubDate,
      link: post.slug,
    })),
  });

@louiss0
Copy link
Contributor Author

louiss0 commented Dec 17, 2022

You can use the getCollection() API directly.

import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';
import { SITE_TITLE, SITE_DESCRIPTION } from '../config';

const posts = await getCollection('blog');

export const get = () =>
  rss({
    title: SITE_TITLE,
    description: SITE_DESCRIPTION,
    site: import.meta.env.SITE,
    items: posts.map((post) => ({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.pubDate,
      link: post.slug,
    })),
  });

I can't seem to get a good build from doing that.

@andersk
Copy link
Contributor

andersk commented Dec 17, 2022

I did verify that it works: louiss0/test-windows#1 (open in StackBlitz).

@louiss0
Copy link
Contributor Author

louiss0 commented Dec 17, 2022

I did verify that it works: louiss0/test-windows#1 (open in StackBlitz).

I tried out the example but I did not get a good build out of it. This is what I get when I try to build the site.

image

Using get collection does not allow you to get the build you want. I don't know what is happening but the files i need refuse to come out. Do I need to build while the server is running or something?

@andersk
Copy link
Contributor

andersk commented Dec 17, 2022

It looks like you are running astro preview, which does not build anything; it only serves the build that was previously generated with astro build. Review the CLI reference.

@louiss0
Copy link
Contributor Author

louiss0 commented Dec 18, 2022

It looks like you are running astro preview, which does not build anything; it only serves the build that was previously generated with astro build. Review the CLI reference.

I did that after the build the files that are generated from the build on the left. It's not about the command It's about the files. As you can see no html files are generated at all.

@louiss0
Copy link
Contributor Author

louiss0 commented Dec 18, 2022

I just noticed that I don't get the url from the post when I use import.meta.glob() this is what I get from it

image

@louiss0
Copy link
Contributor Author

louiss0 commented Dec 18, 2022

👍 Ideally we'd be able to use getCollection() API directly as this is the type of scenario it's really useful for.

Ugly workaround:

const slugRegex = /^src\/content\/blog\/(.*?)\.mdx?$/i
...

link: new URL(`/posts/${post.url.replace(slugRegex, '$1/')}`, import.meta.env.SITE).toString(),

How am I supposed to write the url. I see no url out put any more

@andersk
Copy link
Contributor

andersk commented Dec 18, 2022

As you can see no html files are generated at all.

You seem to have run into a separate async-related bug that caused astro build to exit early without an error message. I’ve filed that as #5628. Try again with this (also updated at louiss0/test-windows#1).

import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { SITE_TITLE, SITE_DESCRIPTION } from "../config";

export const get = async () => {
  const posts = await getCollection("blog");

  return rss({
    title: SITE_TITLE,
    description: SITE_DESCRIPTION,
    site: import.meta.env.SITE,
    items: posts.map((post) => ({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.pubDate,
      link: post.slug,
    })),
  });
};

@louiss0
Copy link
Contributor Author

louiss0 commented Dec 18, 2022

As you can see no html files are generated at all.

You seem to have run into a separate async-related bug that caused astro build to exit early without an error message. Try again with this (also updated at louiss0/test-windows#1).

import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { SITE_TITLE, SITE_DESCRIPTION } from "../config";

export const get = async () => {
  const posts = await getCollection("blog");

  return rss({
    title: SITE_TITLE,
    description: SITE_DESCRIPTION,
    site: import.meta.env.SITE,
    items: posts.map((post) => ({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.pubDate,
      link: post.slug,
    })),
  });
};

You got it the astro team needs to be told that this is how to use getCollection() from now on. We have a new problem now.

I'll have to write lots of boilerplate code to be able to get multiple collections from multiple files. I want the content folder just to be the base.
image

@louiss0 louiss0 closed this as completed Dec 18, 2022
@louiss0
Copy link
Contributor Author

louiss0 commented Dec 18, 2022

This is how you use astro/rss with content collections. This is fantastic

image

@fflaten
Copy link
Contributor

fflaten commented Dec 18, 2022

As you can see no html files are generated at all.

You seem to have run into a separate async-related bug that caused astro build to exit early without an error message. I’ve filed that as #5628. Try again with this (also updated at louiss0/test-windows#1).

import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { SITE_TITLE, SITE_DESCRIPTION } from "../config";

export const get = async () => {
  const posts = await getCollection("blog");

  return rss({
    title: SITE_TITLE,
    description: SITE_DESCRIPTION,
    site: import.meta.env.SITE,
    items: posts.map((post) => ({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.pubDate,
      link: post.slug,
    })),
  });
};

Thanks! 🚀 I got the early exit issue using astro build multiple in my attempts, so though this was limited to *.astro files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants