django-cloudflare-push icon indicating copy to clipboard operation
django-cloudflare-push copied to clipboard

Add a settings variable for static files

Open kamilgregorczyk opened this issue 7 years ago • 11 comments

Hi, I'm using your middleware along with django admin and it works very well for css files but it doesn't seem to work with fonts and fonts.css. Could you maybe add a settings variable for a list of static files which would be always added to the Link header?

https://www.dropbox.com/s/hyoke5oe6q4tolh/Zrzut%20ekranu%202018-07-05%2015.10.00.png?dl=0

kamilgregorczyk avatar Jul 05 '18 13:07 kamilgregorczyk

Hmm, those seem to work fine for me... Can you look into this a bit more and maybe issue a PR?

skorokithakis avatar Jul 05 '18 13:07 skorokithakis

It probably works for fonts in your case because you load them in block in html, django admin seems to prepare fonts.css on its own. I think that's why it skipps these files. A walkaround is fairly simple, just load fonts.css and fonts before django does in html.

kamilgregorczyk avatar Jul 05 '18 13:07 kamilgregorczyk

Ah, yes, the middleware scans for files in static tags, it won't find files that aren't wrapped in those tags. I'm not sure how the workaround works, since not every page includes a fonts.css.

skorokithakis avatar Jul 05 '18 13:07 skorokithakis

My workaround of course has to be done on the "developer side". But it could be useful to maybe have a const variable defined in settings file like

CLOUDFLARE_PUSH_ALWAYS_PUSHED_FILES [
    'static/fonts/bla.woff',
    'static/fonts/bla2.woff',
    'static/fonts/fonts.css',
]

kamilgregorczyk avatar Jul 05 '18 13:07 kamilgregorczyk

Ahh, I see what you mean now. Yes, that could work, although it feels a bit too specific. Maybe there can be a callback function that receives the complete list and can change it however it wants before pushing.

skorokithakis avatar Jul 05 '18 13:07 skorokithakis

Why does it sound too specific?

  • It would be empty as a default
  • If devs want to have something always pushed then they could define it in their settings.file

kamilgregorczyk avatar Jul 05 '18 13:07 kamilgregorczyk

And if they need something never pushed, we'd need a CLOUDFLARE_PUSH_NEVER_PUSHED_FILES setting, etc. The function is more general.

skorokithakis avatar Jul 05 '18 13:07 skorokithakis

Ah I get it, you want to have something like a preprocessor for a list of files, ok that is better.

something like :

CLOUDFLARE_PUSH_FILES_PREPROCESSOR = lambda files: files

But, with such method, is the existing filter method needed anymore?

kamilgregorczyk avatar Jul 05 '18 13:07 kamilgregorczyk

Yeah, the filter method would be a bit redundant, however it would stay because of backwards-compatibility. But yes, it's as you describe, a function that takes a list of all files to push and edits it however the user wants. The default is already there, it's something like lambda files: set(files)[:10].

skorokithakis avatar Jul 05 '18 13:07 skorokithakis

Ok, should I or should you do it?

kamilgregorczyk avatar Jul 05 '18 13:07 kamilgregorczyk

I can do it, but it would probably go faster if you did. I don't currently have the testing setup, but I can review immediately if you want to submit a PR.

The function should run after urls = sort_urls(urls), and the default for it should be lambda urls: urls[10:]. The [10:] part should then be removed from line 70 (create_header_content), as we want to give users as much freedom as we can, while still not making them do lots of standard work (i.e. we should keep the deduplication/sorting lines).

skorokithakis avatar Jul 05 '18 14:07 skorokithakis