rdilweb / docusaurus-plugin-remote-content

A Docusaurus plugin to download content from remote sources when it is needed.
MIT License
91 stars 12 forks source link

Fast Refresh infinite loop #51

Open alebanzas opened 1 year ago

alebanzas commented 1 year ago

localhost keeps hot reloading even though no changes are made

image

docusaurus.config.js

[
    "docusaurus-plugin-remote-content",
    {
        name: "some-name-readme",
        sourceBaseUrl: "https://someurl.com/file.md",
        outDir: "docs/xyz/",
        documents: ["README.md"],
        modifyContent(filename, content) {
          if (filename.includes("README")) {
              return { content: "# some title \n\n #" + content }
          }
          return undefined
      },
    },
 ],
RDIL commented 1 year ago

You need to update your @docusaurus/core version, you're on an out-of-date version which isn't supported by the plugin.

alebanzas commented 1 year ago

Updated it to 2.2.0 and still looping

RDIL commented 1 year ago

@alebanzas did you update all your @docusaurus packages to the same version? If not, you may have old versions lingering.

doroncy commented 1 year ago

I also have the same behavior of looping while on docusaurus 2.2.0

RDIL commented 1 year ago

@doroncy same project?

thomasheartman commented 1 year ago

Hey πŸ™‹πŸΌ I think I'm seeing the same issue. All docusaurus packages are version 2.2.0. It runs once on startup, but then it seems to create an infinite loop whenever I modify or remove a file. I've primarily been working with the sidebar and docusaurus.config files, but saw the same when deleting other files picked up by docusaurus.

I'd be happy to provide any more details if you need them. And for the record: I have no affiliation with the other reporters in this thread, so it's a different project.

RDIL commented 1 year ago

Hey all, so to be honest, I have no idea what's causing this. I've asked the maintainers of Docusaurus to take a look, as they probably will know. I'm sorry this is still happening and causing frustration!

thomasheartman commented 1 year ago

Thanks for the update! And that's perfectly fine 😁 It's pretty benign, just a little annoying πŸ’πŸΌ

I worked with it a bit today and found that some things seem to trigger it, while others don't:

Changing docusaurus.config (as you might do when you're first setting up the plug-in) definitely does. Deleting a file created by the plugin also seems to cause it.

On the other hand, changing other, unrelated files did not trigger it for me, I think. I also think that changing a file that was require'd by the Docusaurus config was okay (but I would need to double check that to be 100%).

So once it's set up, I think it should be mostly fine from there. Still, it'd be nice to get to the bottom of it, but I don't think anyone should be losing sleep over it ☺️

FilipPyrek commented 1 year ago

Same issue here 🀚 Looking forward to the resolution. Thanks for the plugin btw. πŸ™

RDIL commented 1 year ago
Screenshot 2023-01-27 at 17 02 59

I haven't forgotten about this issue, this is what the maintainers of Docusaurus had to say. I'm going to look for a workaround.

dtbuchholz commented 1 year ago

note: if you set noRuntimeDownloads: true in your docusaurus config, this should resolve the issue

colececil commented 8 months ago

Hey, I happened to run into this same issue yesterday trying to write a Docusaurus plugin with similar functionality. (I was looking at this plugin's code as a reference, which is how I ended up here.)

I figured out a way to work around the issue, so I thought I'd share the solution here. The problem is that anytime a file is updated in a directory that's watched by any plugin, a reload is triggered and the loadContent function is called on all plugins. So if this plugin is configured to write to /docs and a reload gets triggered, this plugin rewrites the files to /docs. But since /docs is watched by @docusaurus/plugin-content-docs, each of the files this plugin updates in /docs triggers another reload, which causes this plugin's loadContent function to get called again... which results in an infinite loop.

The solution I came up with is to only write to /docs when at least one of the following conditions are met. (This way, if no changes to /docs are necessary, I avoid writing to it, which prevents a reload from getting triggered.)

  1. The content of an existing file in /docs needs to change.
  2. A new file needs to be added to /docs.
  3. An old file needs to be deleted from /docs.

For scenario 1 above, I had to write some code to check if the file I'm about to write to /docs already exists there, and to check if it already contains the same content I'm about to write. If those conditions are both true, then I skip writing the file, since there's nothing to change.

For scenario 3 above, I had to move my "cleanup" code to the end of my loadContent function. Because of the sequence change, I also had to modify that code so instead of deleting all files from the destination directory, it only deletes any "extra" ones that aren't expected to be there.

Even with these changes, there will still be at least one unnecessary reload because of the write to /docs. But it's much better than an infinite loop! πŸ˜…

I hope this helps!

colececil commented 8 months ago

Also, see https://github.com/facebook/docusaurus/issues/4138 for a proposed "middleware" feature to official Docusaurus plugins that I think would solve this problem.

RDIL commented 8 months ago

Interesting discovery, thanks for letting me know. I'll keep a watch on the middleware proposal, and in the meantime, I'm happy to review any PRs that anyone sends to fix the bug - I don't currently have time to fix it myself sadly.