Skip to main content

HashedFilesMixin

This class provides functionality to rename files by appending a content-based hash for cache-busting and updates internal references within those files to point to the new hashed filenames. It supports processing for CSS and JavaScript files, including handling for source maps and modern JS module imports. The mixin manages multiple processing passes to resolve nested dependencies and ensures that file URLs are correctly mapped in both development and production environments.

Attributes

AttributeTypeDescription
default_templatestring = """url("%(url)s")"""The default string template used to wrap processed URLs when no specific template is provided in the patterns.
max_post_process_passesint = 5The maximum number of times the post-processing loop will run to resolve circular or nested file references.
support_js_module_import_aggregationboolean = falseA boolean flag that determines whether JavaScript module import and export statements should be included in the URL replacement process.
patternstupleA collection of file extensions and associated regex patterns used to find and replace file references with their hashed equivalents.
keep_intermediate_filesboolean = trueA boolean flag that controls whether intermediate versions of files created during multi-pass processing are saved to storage.

Constructor

Signature

def HashedFilesMixin(
*args: any,
**kwargs: any
) - > null

Parameters

NameTypeDescription
*argsanyVariable length argument list passed to the parent constructor.
**kwargsanyArbitrary keyword arguments passed to the parent constructor.

Methods


file_hash()

@classmethod
def file_hash(
name: string,
content: [File](../../../core/files/base/file.md?sid=django_core_files_base_file) = null
) - > string

Return a hash of the file with the given name and optional content.

Parameters

NameTypeDescription
namestringThe name of the file to be hashed
content[File](../../../core/files/base/file.md?sid=django_core_files_base_file) = nullThe file-like object containing the data to hash

Returns

TypeDescription
stringA 12-character MD5 hash string of the file content, or None if no content is provided

hashed_name()

@classmethod
def hashed_name(
name: string,
content: [File](../../../core/files/base/file.md?sid=django_core_files_base_file) = null,
filename: string = null
) - > string

Generates a unique filename by appending a content hash to the original file name. This is used for cache-busting by ensuring file names change when their contents do.

Parameters

NameTypeDescription
namestringThe base name or URL path used to construct the new hashed filename
content[File](../../../core/files/base/file.md?sid=django_core_files_base_file) = nullOptional file-like object to hash; if omitted, the method attempts to open the file from storage
filenamestring = nullThe actual disk filename to use for hashing if it differs from the provided name

Returns

TypeDescription
stringThe file path with the content hash inserted before the file extension

url()

@classmethod
def url(
name: string,
force: boolean = false
) - > string

Return the non-hashed URL in DEBUG mode.

Parameters

NameTypeDescription
namestringThe file path for which to generate a URL
forceboolean = falseWhether to bypass DEBUG settings and return a hashed URL

Returns

TypeDescription
stringThe URL for the given file name, hashed unless DEBUG is True

get_comment_blocks()

@classmethod
def get_comment_blocks(
content: string,
include_line_comments: boolean = false
) - > array

Return a list of (start, end) tuples for each comment block.

Parameters

NameTypeDescription
contentstringThe text content to scan for comment blocks
include_line_commentsboolean = falseWhether to include single-line comments in the results

Returns

TypeDescription
arrayA list of tuples containing the start and end character indices of comments in the content

is_in_comment()

@classmethod
def is_in_comment(
pos: integer,
comments: array
) - > boolean

Determines if a specific character position falls within any of the provided comment blocks. This helps avoid processing URLs found inside code comments.

Parameters

NameTypeDescription
posintegerThe character index to check
commentsarrayA list of (start, end) tuples representing comment ranges

Returns

TypeDescription
booleanTrue if the position is inside a comment block, False otherwise

url_converter()

@classmethod
def url_converter(
name: string,
hashed_files: dict,
template: string = null,
comment_blocks: array = null
) - > callable

Return the custom URL converter for the given file name.

Parameters

NameTypeDescription
namestringThe name of the file currently being processed
hashed_filesdictA dictionary mapping original file paths to their hashed versions
templatestring = nullThe string template used to format the converted URL
comment_blocksarray = nullA list of character ranges to ignore during conversion

Returns

TypeDescription
callableA function that takes a regex match object and returns a string with the hashed URL

post_process()

@classmethod
def post_process(
paths: dict,
dry_run: boolean = false
) - > generator

Post process the given dictionary of files (called from collectstatic).

Parameters

NameTypeDescription
pathsdictA dictionary of file paths to be processed
dry_runboolean = falseIf true, the method returns early without performing any file operations

Returns

TypeDescription
generatorYields tuples of (original_path, hashed_path, processed_status)

clean_name()

@classmethod
def clean_name(
name: string
) - > string

Normalizes file paths by replacing backslashes with forward slashes. This ensures consistent path formatting across different operating systems.

Parameters

NameTypeDescription
namestringThe file path to normalize

Returns

TypeDescription
stringThe normalized path string

hash_key()

@classmethod
def hash_key(
name: string
) - > string

Generates a lookup key for the hashed files cache. In this implementation, it returns the cleaned name directly.

Parameters

NameTypeDescription
namestringThe cleaned file name

Returns

TypeDescription
stringThe key used to identify the file in the hashed_files dictionary

stored_name()

@classmethod
def stored_name(
name: string
) - > string

Retrieves the hashed name of a file from the internal cache or calculates it if missing. It will attempt multiple passes to resolve dependencies in adjustable files.

Parameters

NameTypeDescription
namestringThe original file name to look up

Returns

TypeDescription
stringThe final hashed filename