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

hotfix for DEBUG=True and USE_SRI=True and ManifestStaticFilesStorage #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

abedimhosein
Copy link

Hello,

I use Django version 3.2.16 and I use the ManifestStaticFilesStorage method for cache busting. Today I ran into a problem in the development environment (DEBUG=True).
The hash value calculated for some static files using {% sri_static %} was not equal to their actual hash value, and this caused the following problem:
Failed to find a valid digest in the 'integrity' attribute for resource!

By debugging the library source code line by line, I reached the get_static_path() function. In this function, it is checked that if staticfiles_storage has stored_name attribute then the path of the file is obtained from the staticfiles_storage object, but because we are in the development environment, in fact, the wrong file is selected and as a result, the hash calculated for the file is different from the hash of the served file, And this will cause problems. To solve this problem, I added a condition to check DEBUG Mode to the get_static_path() function and the problem is solved!

Of course, another thing that can be done to solve this problem is to use ManifestStaticFilesStorage only for the deployment environment. But keep in mind that many people use ManifestStaticFilesStorage to prevent unwanted errors during deployment and collectstatic because by using it they can find out if they have a missing static file or not!

Copy link
Owner

@RealOrangeOne RealOrangeOne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks fine. I'll admit I don't use manifest storage locally, but I agree there's no reason not to support it.

Given that, it'd be great to see a test to ensure this functionality sticks!

@@ -15,7 +16,7 @@ def get_static_path(path: str) -> Path:
Resolves a path commonly passed to `{% static %}` into a filesystem path
"""

if hasattr(staticfiles_storage, "stored_name"):
if (not settings.DEBUG) and hasattr(staticfiles_storage, "stored_name"):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: What about DEBUG is it that changes the behaviour here? Is there a better setting it should be derived from?

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