Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

thenewguy
Copy link

@thenewguy thenewguy commented Jan 26, 2021

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 as don’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 or collectstatic would be used during the build phase.

If the configured static storage is a subclass of ManifestStaticFilesStorage the middleware makes use of staticfiles_storage.hashed_files to know which files are supposed to exist (avoids accessing the disk to retrieve unknown files) and uses the WHITENOISE_KEEP_ONLY_HASHED_FILES setting to determine if it should serve unhashed files.

@thenewguy
Copy link
Author

Question: Would this make sense as the default when the configured storage is a subclass of ManifestStaticFilesStorage? It could be moved into the WhiteNoiseMiddleware and add_static_root_files could be a calculated property based on isinstance(staticfiles_storage, ManifestStaticFilesStorage)

@thenewguy thenewguy marked this pull request as ready for review May 26, 2021 21:08
@thenewguy
Copy link
Author

@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):
Copy link
Contributor

@Archmonger Archmonger Nov 20, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants