hotfix for DEBUG=True and USE_SRI=True and ManifestStaticFilesStorage #73
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!