Dynamically loaded libraries in web development

Every page of every website needs assets such as styles, scripts and even specific back-end code. The idea is to load all that stuff on a page-by-page basis (dynamically). Once a page detects that it needs a certain thing it just loads it. Selective assets loading. Magic.

It allows for having virtually unlimited libraries of optional components and customisation. A web-dev paradise.

Each page of a website have different size and complexity. Yet in a typical frameworks such as WordPress, every single page, even the simplest one, needs to load all of the code and assets for all plugins you might have activated on your website such as registration forms, sliders, photo-galleries, re-captcha and what not – everything. Often to simply say Page not found.

This is okay with very small sites but becomes problematic as a website grows.

Early querying

In WordPress, dynamic asset loading can be achieved by early post access. In other words we can retrieve the post ID before the wp hook. We scan the post content and the metadata before enqueuing assets. It allows each page to be thoroughly customised with additional components and overrides.

Separate admin and front end

Admin panel and front-end of the website need different assets. E. g. in WordPress there is a simple template tag is_admin() that can be used to determine whether an asset is needed or not. It’s actually slightly more complex but just to give an idea.

Settings repository

Additionally it is a great practice to keep all the settings such as colours, spacing, typography and so on separately in e. g. JSON files. It provides access to the common settings from any script including SCSS, CSS, Haml, HTML, CoffeeScript, Javascript, PHP, Ruby… you name it, whatever you use to build the website. Just a common DRY practice ;-)

Always required

There are very few things that are required on every single page. That includes but is not limited to SEO optimisation, cache management, redirections, analytics and… that’s pretty much it.

Add a comment