私は、whitenoiseを使用して静的ファイルを提供するアプリケーションに関わっています。 CompressedManifestStaticFilesStorage
を使用して、ManifestStaticFilesStorage
を使用するように構成されています。ManifestStaticFilesStorageでハッシュからパスを除外する方法
私が提供する静的ファイル、および第三者からのleafletなどのライブラリを意味します。
Djangoが提供するManifestStaticFilesStorage
クラスは、ファイル名の名前をハッシュを含む名前に変更します。キャッシュ破損の場合。これは、リソースをフィルタリングし、ハッシュを含めるファイルへのハッシュされていない参照を変更します。順番に
これはリーフレットで関数を破る:
_detectIconPath: function() {
var el = DomUtil.create('div', 'leaflet-default-icon-path', document.body);
var path = DomUtil.getStyle(el, 'background-image') ||
DomUtil.getStyle(el, 'backgroundImage'); // IE8
document.body.removeChild(el);
return path.indexOf('url') === 0 ?
path.replace(/^url\([\"\']?/, '').replace(/marker-icon\.png[\"\']?\)$/, '') : '';
}
path
の値である問題のようなものになります。ファイル名が一致していないとして、第二は、置き換えることはありません
url("https://example.org/static/path/leaflet/dist/images/marker-icon.2273e3d8ad92.png")
を何でもする。これは、順番に返します。
https://example.org/static/path/leaflet/dist/images/marker-icon.2273e3d8ad92.png")
リーフレットは、この「ディレクトリ」にファイル名を追加しようとすると、我々が得る:それは明らかに間違っ
https://example.org/static/path/leaflet/dist/images/marker-icon.2273e3d8ad92.png")marker-icon.png
を。
解決策は何ですか?私は第三者パッケージのファイル名をハッシュしようとしてはならないことを忠告されていますが、他の破損がある可能性があります。しかし、私はManifestStaticFilesStorage
に、特定のディレクトリをハッシュから除外するオプションはありません。
この問題を回避するために、次のクラスを作成しました。 CompressedManifestStaticFilesStorage
ではなく、このクラスを設定で参照しています。
class MyStorage(CompressedManifestStaticFilesStorage):
""" This class overrides the built in class and turns off file name hashing for selected directories. """
def _nonhashed_name_func(self, name, hashed_files=None):
name = posixpath.normpath(name)
cleaned_name = self.clean_name(name)
return cleaned_name
def _url(self, hashed_name_func, name, force=False, hashed_files=None):
split = name.split("/")
if split[0] in ['bower_components', 'node_modules']:
hashed_name_func = self._nonhashed_name_func
return super()._url(hashed_name_func=hashed_name_func, name=name, force=force, hashed_files=hashed_files)
これは機能しますが、やや控えめな解決策のようです。誰もここに良い提案がありますか?