Optimizing an OpenCartA free open-source CMS for online stores, written in PHP with a MySQL/MariaDB database. Everything that is not part of the core is added through extensions. More in the glossary → theme starts with the header: this is where the standard 3.0 and 4.1 themes load jQuery, the CSS for Bootstrap, and icons, and these block the first render of the page. Next come fonts, images without dimensions, and module scripts that load on every page. Most of these problems can be fixed in your own theme without editing the core.
The examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling work differently. This article covers the frontend. Slow database queries and caching of module data are covered in separate articles in the performance section.
What does the standard theme load in the header?
Start with what is already in a clean store. The file sizes below are for releases from the standard packages, without server compression.
| Resource | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| jQuery 3.7.1 | in <head>, blocks rendering | in <head>, blocks rendering (about 87 KB) |
| Bootstrap CSS | bootstrap.min.css Bootstrap 3 branch, about 120 KB | bootstrap.css 5.3.8, built from SCSS without minification, about 270 KB |
| Bootstrap JS | in <head> | in the footer |
| Icons | Font Awesome 4.7, CSS about 30 KB | Font Awesome Free 7.2, all.min.css about 75 KB |
| Web font | Open Sans from Google Fonts | no external fonts in the header |
In 4.1, the heaviest item is the Bootstrap CSS: it is generated from bootstrap.scss and is not minified, so the browser receives the full file with comments and indentation. In 3.0, the main problem is synchronous scripts. Both jQuery and bootstrap.min.js are placed in <head> without defer, so the browser does not paint the page until it has downloaded and executed them.
The product pageThe page for a single product: photos, price, options, description, reviews. More in the glossary → adds its own load. In both branches, Magnific Popup is connected there for the gallery. In 3.0, the product/product controllerCode that receives a request and prepares data. More in the glossary → also always includes two moment.js files (moment.min.js and moment-with-locales.min.js) and bootstrap-datetimepicker. They are needed only for "date/time" options, but they load on every product. In your own 3.0 theme, it is worth removing them: include these files only when the product actually has such options.
How do you remove render blocking from scripts?
The browser stops parsing HTML at every <script src> without attributes. The defer attributeA product characteristic that does not affect the price: material, weight, power. It is set in "Catalog → Attributes" and displayed on the "Specification" tab. More in the glossary → allows the file to download in parallel and execute after the document is parsed, in the order they are included. For scripts from addScript(), this attribute is set in the theme templateA display file: in 2.x it is .tpl, and in 3.0 and 4.1 it is .twig. More in the glossary → loop, because Document stores only the address.
Example
{# extension/my_theme/catalog/view/template/common/header.twig (OpenCart 4.1) #}
<script src="{{ jquery }}"></script>
{% for script in scripts %}
<script src="{{ script.href }}" defer></script>
{% endfor %}
There is an important trap here: you cannot simply mark jQuery with defer. Core and module templates contain inline scripts with $(…) in the middle of the page, and these run immediately when the browser reaches them. If jQuery is deferred, all of them will fail with the error "$ is not defined". So the procedure is as follows:
- Find inline scripts in the templates of the theme and installed modules:
grep -rn "<script>" catalog/.view/ extension/ - Move module scripts (header position) to
deferand verify that inline code does not call them before they load. - Move jQuery to the end of the page or to
deferonly when all inline calls are wrapped in aDOMContentLoadedhandler or moved into files. - In 3.0, move
bootstrap.min.jsto the footer. Its dependency on jQuery remains, so it must come after jQuery.
Analytics, chat, and widget scripts that modules add through events should be moved to the footer or loaded with a delay after the first user interaction. How a module chooses the file position and why the footer position does not always work is described in the article about including JS and CSS in a module.
How do you load fonts without text delay?
Fonts affect speed twice: the file must be downloaded, and until it arrives, the browser either hides the text or shows a fallback font and then repaints. Both options worsen the first-display and layout-shift metrics.
OpenCart 3.0. The header of the standard theme loads Open Sans from fonts.googleapis.com without the display parameter. This creates an extra connection to another domain, and then one more for the font files. If you need this font, add header.twig in your copy of &display=swap to the address and preconnect to the font domains. Even better, download the woff2 files into the theme and declare @font-face yourself. Then there are no external requests at all, and you can include only the styles and the Cyrillic subset you need.
OpenCart 4.1. There is no external font in the header, but there is Font Awesome Free 7.2. Its CSS describes @font-face with font-display: block, so until the icon file loads, the icon spaces are empty. For icons, this is acceptable, but the stylesheet itself is included synchronously in <head> and weighs about 75 KB. If the theme uses a dozen icons, it is more profitable to replace Font Awesome with inline SVG in the templates or with a custom build containing only the required icons.
Example
/* Власний шрифт теми з кирилицею */
@font-face {
font-family: "Inter";
src: url("../fonts/inter-cyrillic-400.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
unicode-range: U+0400-04FF;
}
For a Ukrainian store, be sure to check that the chosen font has Cyrillic with the letters "ґ", "є", "і", and "ї". Otherwise, the browser will substitute a fallback font just for them, and the text will look uneven.
How do you extract critical CSS and reduce the main CSS?
Critical CSS is the minimal set of styles for the first screen: the header, menu, title, and the first row of products. It is inserted directly into <style> in the header, and the full stylesheet is loaded without blocking. This way the page is painted before the 270 KB of Bootstrap arrives.
Example
{# header.twig своєї теми #}
<style>{{ critical_css }}</style>
<link rel="preload" href="{{ stylesheet }}" as="style" onload="this.onload=null;this.rel='stylesheet'"/>
<noscript><link rel="stylesheet" href="{{ stylesheet }}"/></noscript>
The critical_css variable is conveniently passed to the header template by the view/ event, after reading a pre-generated theme file. In 4.1, the handler receives &$data by reference, so it is enough to write a string there. How such handlers work is described in the article about the event view in OpenCart. It is better to generate the critical CSS itself with build tools separately for the home page, category, and product, because the first screen differs on each.
The main CSS should also be reduced:
- OpenCart 4.1: build Bootstrap from SCSS with only the components the theme uses, and minify the result during the theme build. Do not edit the core
bootstrap.cssfile, becausestartup/sasswill overwrite it from SCSS. Include your own file through the theme. How to replace the path to the styles in the header is shown in the article about your own theme. - OpenCart 3.0: the Bootstrap CSS is already minified, but it contains all Bootstrap 3 components. Merging small module CSS files into one file and removing styles for modules that are not on the page gives a bigger effect here.
- Do not include a module's styles on all pages if the module is shown on only one page.
What should you do with images and HTML?
Images are usually heavier than all the CSS and JS combined, and the standard templates of both branches output thumbnails without the width, height, and loading attributes. Because of this, the browser does not know the size of the block until it downloads the file, and the product grid "jumps".
- Add
widthandheightto<img>in the category, product, and module templates. The values are known: these are the thumbnail sizes from the settings; in 3.0 the theme keys aretheme_{код}_image_*, in 4.1config_image_*. - For products below the first screen, set
loading="lazy", but not for the main product photo and the first banner: the browser should get those as early as possible. - Watch the thumbnail sizes. If the settings specify 1000×1000 for a list where the image is displayed at 250 pixels, each thumbnail weighs several times more than necessary. How OpenCart creates these files is covered in the article about model tool/image.
For HTML, both branches have the "Output Compression Level" setting on the "Server" tab in "System → Settings". It enables gzip for OpenCart HTML responses with a level from 0 to 9. Static CSS, JS, and fonts are served by the web server, so their compression and caching headers are configured in the server configuration: the standard OpenCart .htaccess does not contain caching rules. If the server already compresses responses, do not enable compression in OpenCart as well.

Full page caching and server-side optimizations are a separate topic. An overview of one such solution is available in the blog: caching and PageSpeed in OpenCart.
Summary
- Start with the header: in 3.0, jQuery and Bootstrap JS load synchronously; in 4.1, jQuery and the unminified Bootstrap CSS of about 270 KB.
- Set
deferon module scripts in the theme template. Defer jQuery only after the inline$(…)calls have been removed. - Host fonts locally with
font-display: swapand Cyrillic support, and replace Font Awesome in 4.1 if you use only a few icons. - Insert critical CSS into the header via
view/, and load the main CSS without blocking.common/ header/ before - Set dimensions for images and
loading="lazy"for those below the first screen, and generate thumbnails for the actual display size. - In 3.0, remove moment.js and datetimepicker from product pages without date options.
After each change, measure the results on real pages, not only on the home page, and check the cart and checkout. Other articles on layout are collected in the section themes and frontend.