Z3d0X / filament-fabricator

Block-Based Page Builder Skeleton for your Filament Apps
https://filamentphp.com/plugins/fabricator
MIT License
251 stars 48 forks source link

`Filament Fabricator: Layout "default" not found` when using Octane #120

Closed plunkettscott closed 5 months ago

plunkettscott commented 7 months ago

When using Octane (tried both Swoole and RoadRunner for confirmation), pages occasionally load correctly on the first request, but it seems that subsequent requests fail with an error about the layout missing. The same site works correctly without Octane.

Z3d0X commented 7 months ago

Potentially related to #111

ksimenic commented 5 months ago

I have the same problem and managed to get to the root of it.

The issue is in Z3d0X\FilamentFabricator\FilamentFabricatorServiceProvider.

    public function packageRegistered(): void
    {
        parent::packageRegistered();

        $this->app->scoped('filament-fabricator', function () {
            return new FilamentFabricatorManager();
        });
    }

Based on the https://laravel.com/docs/10.x/container#binding-scoped

The scoped method binds a class or interface into the container that should only be resolved one time within a given Laravel request / job lifecycle. While this method is similar to the singleton method, instances registered using the scoped method will be flushed whenever the Laravel application starts a new "lifecycle", such as when a Laravel Octane worker processes a new request or when a Laravel queue worker processes a new job:

Because filament-fabricator is registered as scoped it is flushed on every call. When it instantiates new FilamentFabricatorManager this is what happens in the constructor

    public function __construct()
    {
        /** @var Collection<string,string> */
        $pageBlocks = collect([]);

        /** @var Collection<string,string> */
        $layouts = collect([]);

        $this->pageBlocks = $pageBlocks;
        $this->layouts = $layouts;
    }

meaning all previously registered page blocks and layouts will be overwritten by empty collections (removed).

Now this would be fine if bootingPackage() method from Z3d0X\FilamentFabricator\FilamentFabricatorServiceProvider is called, but this happens only once.

This will eventually result in having page blocks and layouts visible on first and N-1 subsequent requests (N = number of workers that you are running octane with), but on the N+1 request they will simply disappear and be gone until you restart octane server.

Solution to this problem is simply changing scoped with singleton but I am not 100% sure of the full implications this might have on the package.

    public function packageRegistered(): void
    {
        parent::packageRegistered();

        $this->app->singleton('filament-fabricator', function () {
            return new FilamentFabricatorManager();
        });
    }

What was the reason of going with scoped instead of singleton in the first place @Z3d0X?

Z3d0X commented 5 months ago

I don't think there was any specific reason for going with scoped instead of singleton.

If singleton works let's change to that. Thanks for investigating this issue.

plunkettscott commented 5 months ago

@Z3d0X any idea when this will be tagged?

Z3d0X commented 5 months ago

v2.0.6 Released with #130