Skip to content

[4.x]: Css and JS not loading if we use {% cahe %} #17182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mufi-siebird opened this issue Apr 25, 2025 · 4 comments
Open

[4.x]: Css and JS not loading if we use {% cahe %} #17182

mufi-siebird opened this issue Apr 25, 2025 · 4 comments

Comments

@mufi-siebird
Copy link

mufi-siebird commented Apr 25, 2025

What happened?

Description

I have a code like this

{% cache %}
<!DOCTYPE html>
{% set htmlAttrs = {
    lang: craft.app.language,
    class:
    [
        'h-full',
    ]
} %}

<html {{ attr(htmlAttrs) }}>
<head>
</head>

    <body class="min-h-full h-auto text-base font-sans">
        {# Added Hook for after body start #}
        {% hook 'after-body-start' %}

{% include "_/includes/cookie-consent" %}
</body>
</html>
{% endcache %}

it needed to load assets but it doesnt. if i turn off cache from config/general.php or remove {% cache %} code from template, assets do load.

Happens in latest version of craft cms v4.15.2

Craft CMS version

4.15.2

PHP version

8.2

Operating system and version

MacOs 15.1.1 (24B91)

Database type and version

mysql Ver 8.0.40 for macos15.1 on arm64 (Homebrew)

Image driver and version

No response

Installed plugins and versions

No response

@brandonkelly
Copy link
Member

I see your {% endcache %} tag, but where is the {% cache %} tag?

@brandonkelly
Copy link
Member

And where are you loading your JS and CSS?

@mufi-siebird
Copy link
Author

that was typo. there is {% cache %} tag. I was using GDPR COOKIE CONSENT plugin. and wanted to load its css/js that they load via assetbundles.

I tried plugin's auto load assets feature as well as tried loading them manually like this.

{% do craft.app.view.registerAssetBundle("elleracompany\\cookieconsent\\CSSAssets") %}
{% do craft.app.view.registerAssetBundle("elleracompany\\cookieconsent\\JSAssets") %}

I notice that if i use cache tag to whole html, asset bundles do not loads. as soon as i use {% endBody %} tag out of cache, it works.

@brandonkelly
Copy link
Member

I’m not able to reproduce that. I installed the GDPR Cookie Consent plugin on a Craft 4.15.2 install, created a template identical to your example, and an _/includes/cookie-consent.twig template with the above registerAssetBundle code. The plugin-provided JS and CSS are getting included each time I reload:

<!DOCTYPE html>
<html  class="h-full" lang="en">
<head>
<link href="[https://craft4.ddev.site/cpresources/80ddea83/style.css?v=1745864916](view-source:https://craft4.ddev.site/cpresources/80ddea83/style.css?v=1745864916)" rel="stylesheet"></head>

<body class="min-h-full h-auto text-base font-sans">

<script src="[https://craft4.ddev.site/cpresources/80ddea83/script.js?v=1745864916](view-source:https://craft4.ddev.site/cpresources/80ddea83/script.js?v=1745864916)"></script></body>
</html>

I also added a {{ random() }} tag just to be 100% sure that the HTML was getting loaded from cache on subsequent loads. As expected, the random number never changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants