Skip to content

JSAsset

JSAsset

djadmin.dataclasses.JSAsset

JavaScript asset configuration for plugin-provided scripts.

This dataclass specifies how JavaScript assets should be loaded, supporting modern module scripts, deferred loading, and async loading.

Class Attributes

Attribute Type Description
src - Static file path (relative to STATIC_URL).
module - If True, adds type="module" attribute for ES modules.
defer - If True, adds defer attribute (script executes after document is parsed).
async_ - If True, adds async attribute (script executes asynchronously).
nomodule - If True, adds nomodule attribute (script only for non-module browsers).
blocking - If True, script loads in <head> before rendering (prevents layout shift). Use for critical scripts like web components. Default: False.
integrity - Optional SRI hash for security.
crossorigin - Optional CORS attribute ('anonymous' or 'use-credentials').

Examples

Render-blocking script (for web components to prevent layout shift)

JSAsset(src='formset/js/django-formset.js', module=True, blocking=True)

Traditional script with defer

JSAsset(src='djadmin/theme/js/admin.js', defer=True)

Async loading

JSAsset(src='analytics/tracking.js', async_=True)

Fallback for browsers without module support

JSAsset(src='legacy/polyfills.js', nomodule=True)

With Subresource Integrity

JSAsset( src='https://cdn.example.com/library.js', integrity='sha384-...', crossorigin='anonymous' )

Method Resolution Order

  1. djadmin.dataclasses.JSAsset

Attributes

Attribute Value Defined in
__annotations__ {'src': 'str', 'module': 'bool', 'defer': 'bool', 'async_... djadmin.dataclasses.JSAsset
__dataclass_fields__ {'src': Field(name='src',type='str',default=<dataclasses.... djadmin.dataclasses.JSAsset
__dataclass_params__ _DataclassParams(init=True,repr=True,eq=True,order=False,... djadmin.dataclasses.JSAsset
__hash__ None djadmin.dataclasses.JSAsset
__match_args__ ('src', 'module', 'defer', 'async_', 'nomodule', 'blockin... djadmin.dataclasses.JSAsset
async_ False djadmin.dataclasses.JSAsset
blocking False djadmin.dataclasses.JSAsset
crossorigin None djadmin.dataclasses.JSAsset
defer False djadmin.dataclasses.JSAsset
integrity None djadmin.dataclasses.JSAsset
module False djadmin.dataclasses.JSAsset
nomodule False djadmin.dataclasses.JSAsset

Methods

__eq__(self, other)

Defined in: <class 'djadmin.dataclasses.JSAsset'>

Return self==value.

__init__(self, src: 'str', module: 'bool' = False, defer: 'bool' = False, async_: 'bool' = False, nomodule: 'bool' = False, blocking: 'bool' = False, integrity: 'str | None' = None, crossorigin: 'str | None' = None) -> None

Defined in: <class 'djadmin.dataclasses.JSAsset'>

Initialize self. See help(type(self)) for accurate signature.

__repr__(self)

Defined in: <class 'djadmin.dataclasses.JSAsset'>

Return repr(self).

get_attributes(self) -> 'dict[str, str | bool]'

Defined in: <class 'djadmin.dataclasses.JSAsset'>

Get HTML attributes for the script tag.

Returns: dict: Mapping of attribute names to values. Boolean True means attribute without value.

Source code in dataclasses.py line 469
    def get_attributes(self) -> dict[str, str | bool]:
        """
        Get HTML attributes for the script tag.

        Returns:
            dict: Mapping of attribute names to values.
                  Boolean True means attribute without value.
        """
        attrs = {}

        if self.module:
            attrs['type'] = 'module'
        if self.defer:
            attrs['defer'] = True
        if self.async_:
            attrs['async'] = True
        if self.nomodule:
            attrs['nomodule'] = True
        if self.integrity:
            attrs['integrity'] = self.integrity
        if self.crossorigin:
            attrs['crossorigin'] = self.crossorigin

        return attrs

Fields

Field Type Related To
__dict__ getset_descriptor -
__weakref__ getset_descriptor -