lhapaipai / symfony-vite-dev

Monorepo for symfony-vite development
https://symfony-vite.pentatrion.com
Other
26 stars 22 forks source link

when an error occurs and a 500 page is generated the css is not loaded #38

Closed yoann54 closed 1 month ago

yoann54 commented 2 months ago

vite-plugin-symfony version

6.5

vite-bundle version

6.5

your OS, node version, Symfony version, PHP version

symfony 6.3 php 8.2

Description

When an error occurs in my page (twig) a 500 is launched and the css on thie error template page is not loaded via the vite entry (it works if i write staticaly)

How to reproduce

Not easy to reproduce.

Possible Solution

it would be possible to do a reset when an error happens.

lhapaipai commented 2 months ago

hi @yoann54, thanks for your message could you detail your error. without a way to reproduce it, it's difficult to solve it. you don't even specify where the error comes from, where you implemented your twig functions. there is no code, no configuration Sorry but if you don't take the time to detail your problem, it won't make the repository maintainers want to take the time to help you.

yoann54 commented 2 months ago

Sorry i didn't explained the error quite well. In my case i had an error in a twig component so an error was throwed (error 500) so the template of the error page appear but without the style. It seems that the vite entry is not read at this point. I tried to change the vite entry css by a static link tag and it works in this case.

andyexeter commented 2 months ago

This happens when an error is thrown after vite_entry_link_tags is called and the twig error template calls vite_entry_link_tags again.

It happens because each rendered file is stored in the renderedFiles['styles'] array here:

https://github.com/lhapaipai/symfony-vite-dev/blob/39b2bc83ee03f477dd4135e4fb7de7bf2e88fb3e/src/vite-bundle/src/Service/EntrypointRenderer.php#L246-L257

So when vite_entry_link_tags is called again, nothing is rendered because every file is now in the renderedFiles['styles'] array.

This is similar to the following webpack encore issue: https://github.com/symfony/webpack-encore-bundle/issues/73

I had the same issue on one of my projects with styled error pages and fixed it by creating the following event listener:

<?php

use Pentatrion\ViteBundle\Service\EntrypointRenderer;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;

#[AsEventListener]
class ExceptionListener
{
    public function __construct(private readonly EntrypointRenderer $entrypointRenderer)
    {
    }

    public function __invoke(ExceptionEvent $event): void
    {
        if ($event->getThrowable() instanceof \Twig\Error\RuntimeError) {
            $this->entrypointRenderer->reset();
        }
    }
}

I'm not sure whether this could (or should) be added at the bundle level as it seems a bit far reaching, but it does the job for now.

yoann54 commented 1 month ago

@andyexeter explained it so much better than me and as i said i did the same reset function.

lhapaipai commented 1 month ago

thanks @andyexeter, I didn't understand. I've updated the doc if other people encounter the same issue : https://symfony-vite.pentatrion.com/guide/troubleshooting.html