whitenoise icon indicating copy to clipboard operation
whitenoise copied to clipboard

Added Django Middleware that Delays Traversal of Static Root until Requested

Open thenewguy opened this issue 4 years ago • 2 comments

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 avatar Jan 26 '21 13:01 thenewguy

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 avatar Jan 26 '21 15:01 thenewguy

@evansd thoughts on merging? it has been working well for me

thenewguy avatar Jul 01 '21 21:07 thenewguy