-
Notifications
You must be signed in to change notification settings - Fork 151
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
Added Django Middleware that Delays Traversal of Static Root until Requested #275
base: main
Are you sure you want to change the base?
Conversation
Question: Would this make sense as the default when the configured storage is a subclass of |
@evansd thoughts on merging? it has been working well for me |
@@ -168,3 +189,20 @@ def get_static_url(self, name): | |||
return decode_if_byte_string(staticfiles_storage.url(name)) | |||
except ValueError: | |||
return None | |||
|
|||
|
|||
class LazyWhiteNoiseMiddleware(WhiteNoiseMiddleware): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally recommend implementing this feature directly into the original middleware class, and then have it toggleable by a settings.py:WHITENOISE_LAZY
value.
I'd also like to point out I agree with your idea on defaulting this to on with ManifestStaticFilesStorage
.
Added an additional middleware for Django users that avoids the problems described #263 by constructing the file cache for the static root as files are requested.
WHITENOISE_ROOT
is not delayed as it is documented asdon’t use this for the bulk of your static files
. Files from finders are also not delayed as this is documented mainly for development and I've assumed its use in production would be fast orcollectstatic
would be used during the build phase.If the configured static storage is a subclass of
ManifestStaticFilesStorage
the middleware makes use ofstaticfiles_storage.hashed_files
to know which files are supposed to exist (avoids accessing the disk to retrieve unknown files) and uses theWHITENOISE_KEEP_ONLY_HASHED_FILES
setting to determine if it should serve unhashed files.