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

content in contentLoaded is always undefined #10574

Closed
6 of 7 tasks
ben-qnimble opened this issue Oct 10, 2024 · 3 comments
Closed
6 of 7 tasks

content in contentLoaded is always undefined #10574

ben-qnimble opened this issue Oct 10, 2024 · 3 comments
Labels
closed: question This issue is a user error/misunderstanding.

Comments

@ben-qnimble
Copy link
Contributor

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

I want to use the contentLoaded api to get the content of my site.

I created the following plugin:

module.exports = function getContent(...pluginArgs) {
  return {
      name:"testplugin",
      async contentLoaded({ content, actions }) {
          console.log("contentLoaded's content: ",content);
      },
  };
}

and added it to the plugins configuration in docusaurus.config.js. When I run yarn build, I get in the console:

contentLoaded's content:  undefined

but I'd expect to get the content of the site. Am I not using the async contentLoaded function correctly? And how can I get access to the content of the site for use in a plugin?

Reproducible demo

https://codesandbox.io/p/devbox/green-firefly-37w5c9

Steps to reproduce

Create plugin such as

module.exports = function getContent(...pluginArgs) {
  return {
      name:"testplugin",
      async contentLoaded({ content, actions }) {
          console.log("contentLoaded's content: ",content);
      },
  };
}

try to use content.

Expected behavior

I would expect content to show the data associated with the site so that it can be parsed, etc.

Actual behavior

content is always undefined.

Your environment

Self-service

  • I'd be willing to fix this bug myself.
@ben-qnimble ben-qnimble added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Oct 10, 2024
@Josh-Cena
Copy link
Collaborator

Each plugin can only get its own content. If you didn't define loadContent, then you will receive no content. It's not possible for a plugin to get content from other plugins, other than possibly in postBuild you can inspect the HTML build output etc.

I'm closing this for now since it's not a bug. If you have questions about how to implement a particular use case, please use GH discussions.

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2024
@Josh-Cena Josh-Cena added closed: question This issue is a user error/misunderstanding. and removed bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Oct 10, 2024
@slorber
Copy link
Collaborator

slorber commented Oct 10, 2024

Technically there's a secret allContentLoaded() hook you can use that received the whole site plugins content aggregated. It's used by our debug plugin.

But it's internal and I'm likely to remove it, but if I ever do there will be an alternative option such as accessing the site content from the upcoming Server Components. So for an app I think it's relatively safe to implement this lifecycle until we have a better option.

@ben-qnimble
Copy link
Contributor Author

Yeah, I was using allContentLoaded for my plugin when that was one of the arguments to contentLoaded (maybe from docusaurus 2.x?) and it worked fine for a while. But then it stopped working when I upgraded from 3.1 to 3.5 and I saw what I was doing was not anywhere in the documentation, so clearly that was asking for trouble. I ended up seeing your reference for doing this (discussion example ) and that was super helpful. Using that code as a template, I was able to recreate my plugin but in a supported / working way. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: question This issue is a user error/misunderstanding.
Projects
None yet
Development

No branches or pull requests

3 participants