8000 [TwigBundle]  Pre-compile only *.twig files in cache warmup by GromNaN · Pull Request #45845 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle]  Pre-compile only *.twig files in cache warmup #45845

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

Merged
merged 1 commit into from
Apr 1, 2022

Conversation

GromNaN
Copy link
Member
@GromNaN GromNaN commented Mar 25, 2022
Q A
Branch? 6.1
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

In my project, the template directory contains all the frontend assets (CSS, JS and images), this is a choice to locate all component files in the same directories. But the asset files are compiled as if they were twig template, which is a waste of time and storage.

front/
├─ components/
│  ├─ video/
│  │  ├─ templates/
│  │  │  ├─ video.html.twig
│  │  ├─ styles/
│  │  │  ├─ video.css
│  │  ├─ scripts/
│  │  │  ├─ video.js

This patch limit warmup to *.twig files.
This PR adds an option to restrict file pattern, for cache warmup and lint.

twig:
    file_name_pattern: "*.twig"
Before  After
Time to generate 25s 9s
Storage 80MB 26MB
Number of files 4800 1500

The filter on *.twig files is already in the lint:twig command.

Note: If a non-twig template is included in a template (ex: to inline CSS), the source() function must be used ; the cache is not used in this case.

Edit: Here is the documentation https://symfony.com/doc/current/reference/configuration/twig.html#file-name-pattern

@GromNaN GromNaN requested a review from yceruto as a code owner March 25, 2022 09:00
@carsonbot carsonbot added this to the 4.4 milestone Mar 25, 2022
@carsonbot carsonbot changed the title [TwigBundle] Pre-compile only *.twig files in cache warmup [TwigBundle]  Pre-compile only *.twig files in cache warmup Mar 25, 2022
@fabpot
Copy link
Member
fabpot commented Mar 26, 2022

I'm not sure about this change, but this can only be merged in 6.1 anyway as this is a behavior change and not a bug fix (we always consider performance improvements as a new feature).

@GromNaN GromNaN changed the base branch from 4.4 to 6.1 March 26, 2022 09:42
@GromNaN
Copy link
Member Author
GromNaN commented 8000 Mar 26, 2022

Ok for 6.1.

The question to answer before merging this PR is: is it expected to have twig templates that does not have the .twig extension?


The alternative in my code is to decorate the iterator and filter each template name.

class TemplateIterator implements \IteratorAggregate
{
    public function __construct(private iterable $iterator) {}
    public function getIterator(): \Generator
    {
        foreach ($this->iterator as $filename) {
            if (str_ends_with($filename, '.twig')) {
                yield $filename;
            }
        }
    }
}

And use a compiler pass to inject the decorator in the cache warmer.

class TemplateIteratorPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $warmer = $container->getDefinition('twig.template_cache_warmer');
        $iterator = $container->register(TemplateIterator::class, TemplateIterator::class)->addArgument($warmer->getArgument(1));
        $warmer->setArgument(1, $iterator);
    }
}

@fabpot
Copy link
Member
fabpot commented Mar 26, 2022

Ok for 6.1.

The question to answer before merging this PR is: is it expected to have twig templates that does not have the .twig extension?

I think that this is indeed a great question. And the answer is: you can have templates with another extension. It's probably not widespread but nothing forbids it.

@GromNaN
Copy link
Member Author
GromNaN commented Mar 26, 2022

I added an option to the TwigBundle, the plan is to change its default value in 7.0:

twig:
    file_name_pattern: "*.twig"

The majority of projects having only *.twig files in the template directories, they will not have the deprecation notice and not need to update their configuration.

For projects that mix file extensions (like mine), the deprecation notice will be triggered and it will be easy to solve by adding the config.

@GromNaN
Copy link
Member Author
GromNaN commented Mar 26, 2022

Unfortunately, the deprecation is not reported in logs when running bin/console cache:warmup. And there is very few chances the cache is warmed in tests. I've removed the deprecation to simply add the option. Updating the default value in 7.0 could be part of an other PR.

@GromNaN
Copy link
Member Author
GromNaN commented Mar 26, 2022

Ok for 6.1.
The question to answer before merging this PR is: is it expected to have twig templates that does not have the .twig extension?

I think that this is indeed a great question. And the answer is: you can have templates with another extension. It's probably not widespread but nothing forbids it.

In Symfony WebProfilerBundle itself, all svg are included as templates with include tag.

<h1>{{ include('@WebProfiler/Icon/symfony.svg') }} Symfony <span>Profiler</span></h1>

{% include '@WebProfiler/Collector/time.js' %}

Zurb templates uses the source() function correctly.

{{ source("@email/zurb_2/main.css") }}
{{ source("@email/zurb_2/notification/local.css") }}

See #45858

@GromNaN GromNaN modified the milestones: 4.4, 6.1 Mar 26, 2022
@GromNaN
Copy link
Member Author
GromNaN commented Mar 27, 2022

Thanks @yceruto. This proves this issue is common. Your PR was almost the same, with excluded directories in addition.

@fabpot presented the argument that templates directory is for templates only, but experience shows that grouping files by their type is not optimal for maintenance. A twig loader namespace can target a directory with several templates directories.

@fabpot
Copy link
Member
fabpot commented Apr 1, 2022

Thank you @GromNaN.

@fabpot fabpot merged commit 8075e1d into symfony:6.1 Apr 1, 2022
@GromNaN GromNaN deleted the twig-warmup branch April 1, 2022 06:46
GromNaN added a commit that referenced this pull request Apr 4, 2022
…colas-grekas)

This PR was merged into the 6.1 branch.

Discussion
----------

[TwigBridge] Make LintCommand::$namePatterns private

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Dunno why this wasn't done in #45845. /cc `@GromNaN` any idea?

Commits
-------

627a296 [TwigBridge] Make LintCommand::$namePatterns private
@fabpot fabpot mentioned this pull request Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0