lumeland / lume

🔥 Static site generator for Deno 🦕
https://lume.land
MIT License
1.75k stars 74 forks source link

Use local version of Decap CMS script instead of unpkg.com #580

Closed hpfast closed 4 months ago

hpfast commented 4 months ago

Enter your suggestions in details:

Hi, The wait time for unpkg.com, for decap_cms.js, can be long (a minute or more). This is annoying especially in local/dev mode, but it also sometimes affects my production deployment.

Would it be possible to make this configurable, so that the user can specify a local copy of the script if they want?

I'm having a hard time deciding how to do this, though. First I tried doing it just for my site by writing a processor that modifies the rendered /admin/ page, and rewrites the src of the script to a local url. This could be encaspulated in a plugin wrapping (or coming after) the DecapCMS plugin, and it could have the desired url as an option. But this seems like a non-starter because it seems like deno-dom doesn't know about document.scripts?

So the next thing I thought of was to make the DecapCMS plugin configurable: add an option localscript or some such which the page builder would check here: https://github.com/lumeland/lume/blob/d5ac50063d60979124ef732121a7426fc9fb436e/plugins/decap_cms.ts#L88 falling back to unpkg if undefined.

It feels a bit weird to me to make this an option, but I don't see how else to make something like this configurable since the url is hard-coded.

Would you:

a. be open to a pull request adding this option b. have a suggestion for how else I could modify this?

oscarotero commented 4 months ago

@hpfast Hmmm, it shouldn't take one minute to load the script. unpkg.com use to be very fast. Maybe there's a problem somewhere.

I think you can change the url of the script with a processor. For example:

site.process([".html"], (pages) => {
  const admin = pages.find((page) => page.data.url === "/admin/");
  const src = admin.document.querySelector('script[src^="https://unpkg.com/decap-cms"]');
  src.setAttribute("src", newUrl);
});
hpfast commented 4 months ago

@oscarotero thanks! I was trying pretty much this, except I was doing document.scripts. Should have looked for more documentation first.

This works great. No changes to DecapCMS plugin needed :)

Yeah, I was confused with the slow load times from unpkg.com too. Maybe it was just wifi issue ...

Anyways a good illustration of what you can do with processors, I hope this issue helps someone else in the future.