# OpenCart Template Optimization: Fonts, Scripts and Critical CSS in 3.0 and 4.1

> What Slows Down the OpenCart 3.0 and 4.1 Storefront at the Theme Level: Render-Blocking Scripts in the Header, Fonts, Large Bootstrap CSS, Images Without Dimensions. And How to Fix It Without Editing the Core.

Джерело: https://opencartcode.com/en/wiki/dev-themes/opencart-template-optimization
OpenCart: 3.0, 4.1
Оновлено: 2026-11-25

Optimizing an [OpenCart](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#opencart) 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 page](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#product-page) adds its own load. In both branches, Magnific Popup is connected there for the gallery. In 3.0, the `product/product` [controller](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#controller) 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` [attribute](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#attribute) 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 [template](https://opencartcode.com/en/wiki/glossary/e-commerce-glossary#template) loop, because Document stores only the address.

Example

```twig
{# 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:

1. Find inline scripts in the templates of the theme and installed modules: `grep -rn "<script>" catalog/view/ extension/`.
2. Move module scripts (header position) to `defer` and verify that inline code does not call them before they load.
3. Move jQuery to the end of the page or to `defer` only when all inline calls are wrapped in a `DOMContentLoaded` handler or moved into files.
4. In 3.0, move `bootstrap.min.js` to 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](https://opencartcode.com/en/wiki/dev-themes/opencart-include-js).

> Optimizer modules that automatically add `defer` to all scripts often break the cart and checkout because of inline core code. After any change to the script order, run through this scenario: add to cart, change the quantity, place an order.

## 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

```css
/* Власний шрифт теми з кирилицею */
@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

```twig
{# 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/common/header/before` 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](https://opencartcode.com/en/wiki/dev-events-ocmod/event-view-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.css` file, because `startup/sass` will 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](https://opencartcode.com/en/wiki/dev-themes/custom-opencart-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 `width` and `height` to `<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 are `theme_{код}_image_*`, in 4.1 `config_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](https://opencartcode.com/en/wiki/dev-practice/resize-opencart).

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.

System → Settings → Server: HTML output compression level · OpenCart 4.1.0.4

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](https://opencartcode.com/en/blog/keshuvannia-ta-pagespeed-v-opencart-ohliad-jet-cache).

## 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 `defer` on module scripts in the theme template. Defer jQuery only after the inline `$(…)` calls have been removed.
- Host fonts locally with `font-display: swap` and Cyrillic support, and replace Font Awesome in 4.1 if you use only a few icons.
- Insert critical CSS into the header via `view/common/header/before`, and load the main CSS without blocking.
- 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](https://opencartcode.com/en/wiki/dev-themes).

## FAQ

**Will Combining All JS into One File Help?**

With HTTP/2, the benefit of combining files is much smaller than before, and one large file caches worse after updates to a single module. It's more important to remove scripts the page doesn't need and not block rendering with them.

**Why did the cart stop working after optimization?**

Most often jQuery or common.js got defer or were moved to the end of the page, while the inline template code calls them earlier. Put jQuery back in place and move inline scripts gradually.

**Should I enable output compression in OpenCart settings?**

Only if the web server doesn't compress HTML itself. Most hosting providers already serve gzip or brotli, and enabling it twice gives no benefit.

**Do I need WebP for thumbnails?**

It noticeably reduces image weight, but OpenCart's standard resize keeps thumbnails in the original format. WebP requires a separate module or server-side or CDN processing.
