This project is not covered by Drupal’s security advisory policy.
Problem/Motivation
Many sites are following single-style-css approach when all the styles for the site are written (or compiled) into a single style.css file which is added to every single page on the site. That file contains all the styles available.
Needless to say, that's just loading of useless kilobytes of data and slowing down the browser by parsing the extra content. Same applies to JS with the scripts.js file
You could use libraries like UnCSS but there's another way to do it - this module.
How does it work?
The module is the development tool that allows you to load only the assets that are relevant for the elements rendered on the page. Following the Drupal theme overview it is easy to see that there's a sort of components' (elements, templates) hierarchy.
We stick to the below layers (starting from the most common one). The higher layers typically include the lower ones.
- HTML
- Page
- Regions
- Entities
- Blocks
Step 1. The module scans the directories (that you define) for the existence of CSS/JS files matching the naming convention (described below) and caches that list not to scan file system all the time.
Step 2. On the page load the module registers the elements that are added into the page, constructs the list of the asset name suggestions and loads the existing suggestion assets from the cached list. Files are loaded according to the layers hierarchy, e.g. CSS files for Entities will be loaded later than HTML layer related files.
Naming Convention
The naming convention described in this section is the default behaviour of the module. It is possible to override/replace it. Review Customisations section to learn how to do it.
Basenames
When elements on the page are detected by the module, e.g. node that is rendered on the page, the module saves the asset's "basenames", e.g. node and node--NODETYPE for the example above.
The complete list of predefined basenames:
- HTML
html
- Page
page
- Regions
region--REGION
- Entities
node- if on node pagenode/%node--NODETYPE- if on node pagenode/%term- if on term pagetaxonomy/term/%term--VOCABULARY- if on term pagetaxonomy/term/%
- Blocks
blockblock--MODULEblock--MODULE--DELTAview- if block is from Views moduleview--VIEWNAME- if block is from Views moduleview--VIEWNAME--DISPLAYID- if block is from Views modulemenu- if block is from Menu modulemenu--MENUNAME- if block is from Menu moduleviewmode--VIEWMODE- if node is rendered on a page using custom VIEWMODEviewmode--VIEWMODE--NODETYPE- if node is rendered on a page using custom VIEWMODE
Modifiers
Similarly to the Drupal's template suggestion mechanism there are modifiers for the basenames that allow loading page-specific or context-specific assets. E.g. for the path node/1 the modifies will be node and node-1. Using the basenames from the example above the asset name suggestions are (EXT is either css or js):
node.EXT- no modifier usednode-node.EXT- basenamenodeand modifiernodenode-node-1.EXTnode--NODETYPE.EXTnode--NODETYPE-node.EXTnode--NODETYPE-node-1.EXT
The above files if existing will be loaded from the configured scan directories.
Customisations
TBD
Project information
- Project categories: Developer tools
- Created by skullhole on , updated
This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.