doublesecretagency / craft-cpcss

Control Panel CSS plugin for Craft CMS
MIT License
117 stars 8 forks source link

Plugin causes very slow CP load times #5

Closed siebird closed 3 years ago

siebird commented 9 years ago

Hi Lindsey,

First, thanks for sharing this plugin, it's been easy to use for adding styles to the redactor formatting menu. Unfortunately, I have been having issues developing this site locally because the CP takes forever to load (5+ sec.) and I wasn't sure what the issue was until today as I started disabling plugins. I put together a short video [removed] to demonstrate what's going on. Essentially loading an external style sheet with a environment variable {baseUrl} causes the slowest load times. If I move the styles into the database this speeds things up, but is still not desirable.

lindseydiloreto commented 9 years ago

Thanks for letting me know, and thanks for the video. Couple of quick questions...

I hate to say "works on my machine", but we have this installed on several different sites across several different servers (local, staging, and production), and none of them are experiencing any slowdown whatsoever.

lindseydiloreto commented 9 years ago

Ultimately, the plugin is pretty simple. There are only a few places where I could envision a point of failure...

All of this code is in cpcss/CpCssPlugin.php:

If you're comfortable tinkering with PHP, you might want to try making adjustments to any of those lines in order to pinpoint the problem. Please let me know what you find out... if there's any instability (even minor) to be addressed, I'll gladly work it into a future release.

siebird commented 9 years ago

Thanks for the follow up. This was only happening on my local machine (all browsers) so I thought it was something specific to MAMP. I checked out 'Network' in dev tools and the general request for the admin page was the source (5+ sec). As you thought, line 83 was the culprit. I commented out the conditional and just left the else statement and the CP is snappy again (< 200ms). However I'm not sure what the fix could be for my situation. Any ideas? Thanks

lindseydiloreto commented 9 years ago

Sorry for the delayed response! Can you try something for me really quick?

Put the original lines of code back, and simply remove the @ symbol. Also, please make sure that you're running in dev mode. After that, you should be able to see exactly what error is being triggered when you load any page in the CP. If you could then copy & paste that error message back into this thread, that would be great!

If there's a bug here that can be patched, it would be great to get it fixed permanently... That way, you wouldn't need a hacked version of the plugin, and could easily update if needed. And we could guarantee that no one would ever bump into the same issue again.

siebird commented 9 years ago

This ended up being an issue with IPv6 host names in my etc/host file. MAMP was only generating the IPv4 host names. Adding the IPv6 host names (::1 domain.dev) made a huge difference in resolve times.

I upgraded my modem about 2 months ago which supports IPv6. In hindsight, this is when I started noticing the slow resolve times locally. At this point, MAMP does not support IPv6 host name so I unfortunately need to manually update the host file every time I add a new host.

lindseydiloreto commented 9 years ago

Huh, pretty weird. Thanks for the full explanation!

If you remove the "@", does a specific error message get thrown?

I'd suspect that the sha1_file line is having trouble ensuring that the file actually exists before trying to encode it. I don't know why it would stall for so long before realizing the file is unavailable... I might try to break that up into a couple lines of code to handle it step-by-step.

  1. Identify filename
  2. Get file contents
  3. Encode file contents
siebird commented 9 years ago

When I removed the @ symbol, I got no errors. Would this error be thrown on every CP page?

lindseydiloreto commented 9 years ago

No errors? Yeah, it would be on every page of the CP.

Are you running in dev mode?

siebird commented 9 years ago

Yep!

lindseydiloreto commented 9 years ago

Weird.

Ah well... I'll try to split that code up a little bit to make it easier to pinpoint errors. Not sure exactly when I'll get to that though, so in the meantime feel free to just comment out that conditional again.

Thanks! :)

rungta commented 5 years ago

Hey, just wanted to let you know that this bug still persists in the Craft 2 versions of both CpCss and CpJS. On one of our projects this was slowing down the control panel enough to make it unusable.

We’re currently addressing this by using the @import statement in the Additional CSS field to pull in an external file instead.

lindseydiloreto commented 5 years ago

Oh wow, thanks @rungta! Not sure why I closed this thread before I made any code changes.

I'll re-open this ticket and try to address it in the near future. πŸ‘

OldStarchy commented 3 years ago

Hi, @lindseydiloreto hope you're doing well. I've also run into this problem on our dev server. Any request to /admin causes the request to hang for 2 minutes (consistently)

It took a long time but I managed to find the call to sha1_file. This seems to be at least part of my problem. The cssFile setting was @web/cms.css which expanded to a URL, and I think then sha1_file is trying to hash the file by pulling it over http... I guess; this is causing it to hang due to session blocking itself maybe (this also doesn't seem right to me).

In any case, is it correct to have cssFile point to a URL or should it be @webRoot? The latter makes more sense to me but I didn't set up the config for this project I'm working on.

I've removed the config to disable this module and anything /admin is still hanging elsewhere too, so it seems something else is causing me troubles too. I'll have to time the request again to see if it's still 2 minutes.

lindseydiloreto commented 3 years ago

Hey @aNickzz, sorry you're bumping into this! It's been an unsolved riddle for a long time. The sha1_file function should be failing more gracefully, I have no idea why it stalls out for some people.

I just made some changes in an attempt to improve the process... can you switch to this branch and run craft update?

"doublesecretagency/craft-cpcss": "dev-sha"

I split sha1_file into its comparable components, sha1 and file_get_contents. I also added a bit of logging to make it easier to track down the problem. Hopefully those improvements make it a little less painful for the next person.


To answer your other question... we need to point at the full URL so that people can use remote CSS files.

OldStarchy commented 3 years ago

I forgot to hit comment on this yesterday but a short while after leaving that last comment I found the same thing in the craft-cpjs module. Disabling both has resolved the issue.

I'll try the new branch when I have time, but I'm not sure when that will be, hopefully within a week or so. As it turns out we didn't actually have cms.css or cms.js files to load, the config was in there as a placeholder.

lindseydiloreto commented 3 years ago

Yup, that makes sense... They share 95% of the same DNA. Whatever solution we work out for CP CSS will get ported over to CP JS when we're finished.

Let me know when you have a chance to try that new branch!

lindseydiloreto commented 3 years ago

Ok, I've just released v2.4.0. The new version will (hopefully) squash this issue once and for all. 🀞

It addresses the problem in the following ways...

And as an added bonus, I've copied all of these changes over to the CP JS plugin as well. πŸ‘

Hope that helps!