Replies: 3 comments 13 replies
-
The global data is not just "saved as a JSON file". It is loaded globally in your site via We most definitely do not want to save literally everything about blog posts globally. Nevertheless I think |
Beta Was this translation helpful? Give feedback.
-
This is because you don't understand yet what React hydration is. Learn that and it will make sense. Global data need to be loaded on every single page of your site before React could hydrate, because hydration assumes the data is loaded, because we use that data to display things such as the global site layout features (version dropdowns, i18n dropdowns etc). If this data is very heavy, this delays the React hydration. Users on a bad network will encounter a bad UX where the site/page is visible, but it is not possible to interact with it for a few seconds. If users click on buttons, and the buttons do nothing, this is a really bad UX. Increasing global data size increase the time a site is presented as non-interactive, and should be avoided. Now if you don't mind, you can extend the blog plugin const blogPluginExports = require("docusaurus-plugin-content-blog");
const defaultBlogPlugin = blogPluginExports.default;
async function blogPluginExtended(...pluginArgs) {
const blogPluginInstance = await defaultBlogPlugin(...pluginArgs);
const pluginOptions = pluginArgs[1];
return {
...blogPluginInstance,
contentLoaded: async function (params) {
// Register yourself the extra global data you want here
const { content, actions } = params;
actions.setGlobalData(content);
return blogPluginInstance.contentLoaded(params);
},
};
}
module.exports = {
...blogPluginExports,
default: blogPluginExtended,
}; Another possibility is to have the blog plugin create additional pages, I've explained this setup here: This is more efficient (no global data usage) but will not work in all situations (particularly if you need data from 2 distinct plugins on the same page). I doubt The solution we want to this "problem" (being able to inject any plugin data to anywhere) is React Server Components running at build time. But it's complicated to implement and we haven't started working on this yet. Until we have this, I'll try to figure out if we can provide a temporary good enough workaround. |
Beta Was this translation helpful? Give feedback.
-
You do not even have to extend the blog plugin to add your own
It has impact only on the pages you inject that data into. If your homepage receives a lot of data, your homepage will be heave. Unlike global data this size will not impact ALL your pages, but just this one.
That is related to the DX optimizations I'm working on as part of #9903. If
Agree, here's a full example that I'll also add to the PR: export default async function MyHomePagePlugin(context, options) {
return {
name: 'my-home-page-plugin',
async allContentLoaded({ allContent, actions }) {
const allBlogs = allContent['docusaurus-plugin-content-blog'];
const blog1 = allBlogs['blog1'];
const blog2 = allBlogs['blog2'];
const allBlogPosts = [...blog1.blogPosts, ...blog2.blogPosts];
actions.addRoute({
path: '/',
exact: true,
component: '@site/MyHomePage/MyHomePage.js',
modules: {
allBlogPosts: await actions.createData(
'allBlogPosts.json', // The file name doesn't matter, we'll simplify this API soon
JSON.stringify(allBlogPosts)
),
},
});
},
};
} export default function Home(props) {
const { allBlogPosts } = props;
return (
<main>
<h2>All blog posts</h2>
<ul>
{allBlogPosts.map((blogPost, i) => (
<li key={i}>{blogPost.metadata.title}</li>
))}
</ul>
</main>
);
} |
Beta Was this translation helpful? Give feedback.
-
With the current release code, I could not find a way to access a selection of the blog posts in standalone or docs pages.
Would it be possible for the blog plugin to save the blog posts globally (with
setGlobalData()
) and make the content available outside the blog plugin? Similar question for tags, authors, and possibly other data.I saw a notice in the documentation (Global data is... global: its size affects the loading time of all pages of your site, so try to keep it small.), but I don't understand how the loading time of a static site can be affected by some JSON files saved during build time.
If saving the blogPosts in the blog plugin is not possible, is it reasonable to do this in the new
processBlogPosts()
hook? What solutions would be possible for the other data (tags, authors, etc)?Beta Was this translation helpful? Give feedback.
All reactions