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

feat(blog): add options.createFeedItems to filter/limit/transform feed items #8378

Merged
merged 19 commits into from Dec 29, 2022

Conversation

johnnyreilly
Copy link
Contributor

@johnnyreilly johnnyreilly commented Nov 26, 2022

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

THIS IS AN EXAMPLE IMPLEMENTATION TO ILLUSTRATE #8376

Motivation

My blog turns out to be quite large. It was recently brought to my attention that the RSS feed wasn't working well for readers as a consequence.

At present, Docusaurus includes all blog entries in the Atom / RSS feeds: https://docusaurus.io/docs/blog#feed

The proposal is to add another option to support filtering the responses in the feed. This would allow people to reduce the size of their blogs when they become large.

If this existed, then this would not be necessary: johnnyreilly/blog.johnnyreilly.com#352

Test Plan

See automated tests and observe the atom.xml below - 2 entries only due to filtering:

image

or go here: https://deploy-preview-8378--docusaurus-2.netlify.app/blog/rss.xml

Test links

Deploy preview: https://deploy-preview-8378--docusaurus-2.netlify.app/blog/rss.xml

Docs:

Related issues/PRs

fix #8376

@netlify
Copy link

netlify bot commented Nov 26, 2022

[V2]

Name Link
🔨 Latest commit f5bb167
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/63ad7c49ee1147000849fd83
😎 Deploy Preview https://deploy-preview-8378--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Nov 26, 2022

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 61 🟢 97 🟢 100 🟢 100 🟢 90 Report
/docs/installation 🟠 81 🟢 100 🟢 100 🟢 100 🟢 90 Report

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment

packages/docusaurus-plugin-content-blog/src/feed.ts Outdated Show resolved Hide resolved
@slorber slorber changed the title example blog filter implementation feat(blog): add more control over blog feed items (filter/limit/transform) Dec 9, 2022
@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Dec 9, 2022
@slorber slorber marked this pull request as draft December 9, 2022 10:03
@johnnyreilly johnnyreilly marked this pull request as ready for review December 27, 2022 10:50
@johnnyreilly
Copy link
Contributor Author

johnnyreilly commented Dec 27, 2022

Reworked this based on feedback to replace the filter API with a createFeedItems API based on @slorber's suggestion:

    createFeedItems?: (options: {
      blogPosts: BlogPost[];
      siteConfig: DocusaurusConfig;
      outDir: string;
      defaultCreateFeedItems: (params: {
        blogPosts: BlogPost[];
        siteConfig: DocusaurusConfig;
        outDir: string;
      }) => Promise<BlogFeedItem[]>;
    }) => Promise<BlogFeedItem[]>;

Usage looks like this:

            createFeedItems: async (options) => {
              const { blogPosts, defaultCreateFeedItems, ...others } = options;
              const blogPostsFiltered = blogPosts.filter((item, index) => index < 2);
              return defaultCreateFeedItems({ blogPosts: blogPostsFiltered, ...others });
            }

The above implements the a filter; merely one of many things that could be done.

Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks almost good 👍

website/docs/blog.mdx Show resolved Hide resolved
@@ -54,11 +55,35 @@ async function generateBlogFeed({
copyright: feedOptions.copyright,
});

const feedItems = await (feedOptions.createFeedItems
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe simplify with:

const createFeedItems = feedOptions.createFeedItems ?? defaultCreateFeedItems

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love to but the signatures are subtly different. Did ponder other simplifications but didn't come up with anything that seemed worthwhile. Happy to implement any other ideas?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a big problem if the 2 functions had the exact same signature? It shouldn't harm anyone if defaultCreateFeedItems would "receive itself" as a param, although it wouldn't be used in practice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gave it a try and apparently TS is fine with that thanks to duck typing 👍 updated the PR

@slorber
Copy link
Collaborator

slorber commented Dec 28, 2022

LGTM

Need a bit of docs in https://deploy-preview-8378--docusaurus-2.netlify.app/docs/api/plugins/@docusaurus/plugin-content-blog/#configuration

Also remove current test before merge ;)

@johnnyreilly
Copy link
Contributor Author

Awesome! I'll add some docs shortly

@johnnyreilly
Copy link
Contributor Author

@slorber slorber added the to backport This PR is planned to be backported to a stable version of Docusaurus label Dec 29, 2022
@johnnyreilly
Copy link
Contributor Author

Nice - params is more accurate than options

@slorber slorber changed the title feat(blog): add more control over blog feed items (filter/limit/transform) feat(blog): add options.createFeedItems to filter/limit/transform feed items Dec 29, 2022
@slorber slorber merged commit 022e005 into facebook:main Dec 29, 2022
@johnnyreilly johnnyreilly deleted the feat/blog-filter branch December 29, 2022 14:29
slorber added a commit that referenced this pull request Jan 26, 2023
…d items (#8378)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
@slorber slorber added backported This PR has been backported to a stable version of Docusaurus and removed to backport This PR is planned to be backported to a stable version of Docusaurus labels Jan 27, 2023
@slorber
Copy link
Collaborator

slorber commented Jul 26, 2023

Glad you submitted that PR @johnnyreilly 😄

That will allow me to stay under Vercel fair use free plan for a little bit more. My 4MO feed is exploding the plan otherwise.

CleanShot 2023-07-26 at 10 55 06@2x

Wonder if we shouldn't limit by default. Considering we output blog content in the feed and it's not rare to see blogs with 100+ blog posts. Wonder what would be a good default limit (20?) and what would be the API to change it or disable it in case you really want all blog posts in your feed.

@johnnyreilly
Copy link
Contributor Author

Haha - glad it's of use to you! Yeah I did wonder about this. I think the other thing to factor in is that it's the size of the feed that's the issue for consumers generally, and that's not just a factor of the number of blog entries. But also the length of the underlying posts. It'd be nice if it could limit by size of feed, including the max number of entries that keeps the feed under a reasonable size. Though maybe this is too complicated to expose to users.

The idea of a default of a max number of entries of 20 seems reasonable. Again, being able to opt out is probably wise.

@slorber
Copy link
Collaborator

slorber commented Jul 27, 2023

It'd be nice if it could limit by size of feed, including the max number of entries that keeps the feed under a reasonable size.

That seems hard to achieve with our current system and a bit overkill. We write the XML at once from in-memory data and don't stream to it with the ability to stop at a given time.

Limiting the number of entries is much simpler and probably good enough.

I guess we can have a feed.limit: number | false | null = 20

Let me know if you want to submit a PR

@johnnyreilly
Copy link
Contributor Author

Yeah I'd be happy to submit a PR; I'm on holiday right now but maybe sometime in August. Feel free to ping me if I forget

@slorber
Copy link
Collaborator

slorber commented Jul 27, 2023

👍 Here's an issue: #9179

For some reason I can't assign it to you

@johnnyreilly
Copy link
Contributor Author

That's funny - I can't assign it to myself either. Maybe I have to be added as a member to the Docusaurus project to be assigned something? Not sure

@slorber
Copy link
Collaborator

slorber commented Jul 27, 2023

I can assign the issue to many john... persons but not you, you don't appear in the autocomplete 😅 maybe it's a profile setting? 🤔

@johnnyreilly
Copy link
Contributor Author

Maybe - mysterious!

@Josh-Cena
Copy link
Collaborator

I think you can only assign to people who have participated and people affiliated with the organization?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backported This PR has been backported to a stable version of Docusaurus CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide ability to filter RSS / Atom feed
4 participants