📜 ⬆️ ⬇️

Connecting static resources from templates

Having worked on a number of web projects as a frontend / backend developer / maker-up in different companies, I constantly encountered an inefficient and ugly approach to the task of connecting the necessary static resources (for the time being, we consider this .css and .js files) to display on the page .

The main problem of all the approaches I have encountered is the close connection between the structure of the frontend code, the logic of the deployment and the backend code (mainly templates), as well as the absence of semantics. Further, the term frontend-code will be understood as the entire set of .js , .css and any other files or resources that are given to the browser. As a rule, frontend developers (sick!) Deal with these files.

First, I will give a couple of real-life examples (in pseudocode, since different frameworks and languages ​​were used everywhere, and the real code will only confuse us), consider the shortcomings and problems associated with the approaches used, and at the end I will describe my vision of this problem.

First example

On one extensive project (based on the Zend Framework ), static files were connected in approximately the following way:
')
// someWidget.tpl // PROJECT_STATIC_VERSION —      (  ) ViewHelpers.appendStylesheet("css/some-path/sub-path/some-widget.css?" + PROJECT_STATIC_VERSION); ViewHelpers.appendJsFile ("js/some-path/sub-path/some-widget.js?" + PROJECT_STATIC_VERSION); //    Layout: <div class="some-widget"> <!-- a lot of cool html --> </div> 

We assume that the methods ViewHelpers.appendStylesheet and ViewHelpers.appendJsFile guarantee us the inclusion of the files transferred to them in the corresponding tag on the final page. The PROJECT_STATIC_VERSION line was used to add some key to the url, the update of which would force the browser to download the new version of this file.
In addition to this, files are often connected outside of templates, for example, in the controller code or in the element decorator code ( Zend_Form_Decorator ). Especially frequent was the connection of ExtJS js-framework files in case the js-code connected from the template was based on ExtJS . Unfortunately, in 95% of cases this was done by a copy-paste of the form:

 ViewHelpers.appendJsFile("js/libs/ext-js/ext-js.js?" + PROJECT_STATIC_VERSION); ViewHelpers.appendCss ("css/libs/ext-js/ext-js.css?" + PROJECT_STATIC_VERSION); ViewHelpers.appendJsFile("js/libs/ext-js/locale-ru.js?" + PROJECT_STATIC_VERSION); 

So, the disadvantages of this approach (most certainly is obvious):

The second example from a symfony project

 // SomeConroller.SomeAction If (Config.Env == "Production") { includeCss("styles/feature.min.css"); includeJs("js/feature.min.js"); } Else If(Config.Env == "Dev") { includeCss("styles/feature/global.css"); includeCss("styles/feature/sub-feature.css"); includeJs("js/classes/Core.js"); includeJs("js/classes/Event.js"); includeJs("js/classes/CoolPlugin.js"); includeJs("js/classes/Feature.js"); includeJs("js/classes/FeatureSubFeature.js"); } Layout: <div class="feature"> <!-- feature's code is here --> </div> 

Plus, at the project level, there was a config for describing the files necessary for merging and minifying js and css of the following code:

 styles/feature.min.css: styles/feature/main.css styles/feature/sub-feature.css js/feature.min.js: js/classes/Core.js js/classes/Event.js js/classes/CoolPlugin.js js/classes/Feature.js js/classes/FeatureSubFeature.js 

This example has all the drawbacks of the previous one, only in a more terrifying form:

Task

As a result, after analyzing the shortcomings of these and other approaches, I collected a number of requirements for the system of connecting static resources:


Decision


Good Practices


In conclusion, I want to say that I successfully use this approach in personal projects and gradually integrate it into the current project at work.

Thanks to everyone who read it. I will welcome any comments and suggestions!

Source: https://habr.com/ru/post/151456/


All Articles