Skip to content
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

Calling Volt::mount a second time negates initial call #87

Closed
inmanturbo opened this issue Jan 21, 2024 · 11 comments · Fixed by #95
Closed

Calling Volt::mount a second time negates initial call #87

inmanturbo opened this issue Jan 21, 2024 · 11 comments · Fixed by #95

Comments

@inmanturbo
Copy link

inmanturbo commented Jan 21, 2024

Volt Version

1.3

Laravel Version

10.31.0

PHP Version

8.3.1

Database Driver & Version

No response

Description

Calling Volt::mount() more than once overwrites the first call. This makes it impossible for packages to use Volt::mount() to register their own components.

Steps To Reproduce

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Livewire\Volt\Volt;

class VoltServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        Volt::mount([
            config('livewire.view_path', resource_path('views/livewire')),
            resource_path('views/pages'),
        ]);

        Volt::mount([
            resource_path('views/vendor/demo/livewire'),
        ]);
    }
}

Install volt. Add code above to Volt service provider. Run php artisan make:volt counter. Use counter component in view.

Run the command below to copy the component to a new component called vendor-counter in the "vendor/demo" path set up in our provider.

mkdir -p resources/views/vendor/demo/livewire \
 && cp resources/views/livewire/counter.blade.php \
 resources/views/vendor/demo/livewire/vendor-counter.blade.php 

Attempt to use both in a view:

  @livewire('counter')
  @livewire('vendor-counter')

See error:

InvalidArgumentException
PHP 8.3.1
10.41.0
View [counter] not found.

counter was not found.
Are you sure the view exists and is a .blade.php file?

example repo: https://github.com/inmanturbo/volt-bug-report

@driesvints
Copy link
Collaborator

Thanks. Please provide thorough steps to reproduce this with code examples.

@inmanturbo
Copy link
Author

@driesvints Thank you for reviewing the issue. I've updated the steps with more complete examples, and included a link to an example-repo.

@inmanturbo
Copy link
Author

inmanturbo commented Jan 22, 2024

Here is an example workaround I've used:

trait MergesVoltMounts
{
    /**
     * Merge the given paths with the existing Volt mounts.
     */
    protected function mergeVoltMounts(array $paths): void
    {
        $voltPaths = collect(Volt::paths())->map(function ($path) {
            return $path->path;
        })->toArray();

        $paths = array_merge($voltPaths, $paths);

        Volt::mount($paths);
    }
}

https://github.com/inmanturbo/b2bsaas/blob/b8aaf459229b058773fa3a637329686009f26d68/inmanturbo/b2bsaas/src/MergesVoltMounts.php

@driesvints
Copy link
Collaborator

Hi @inmanturbo. Thanks for the good example. This indeed doesn't seem possible. One solution packages can take for now is to ask app developers to add the paths to the Volt::mount call in their service provider. Or you can attempt a PR so path's get added additionally with the mount call.

@robsontenorio
Copy link
Contributor

robsontenorio commented Mar 3, 2024

This worked. Is it reliable ?

@driesvints @inmanturbo

MyPackageServiceProvider.php

 public function register()
    {
       ....

        $this->app->booted(function () {
            $paths = collect(Volt::paths())->map->path->all();
            $paths = array_merge($paths, [__DIR__ . '/../resources/views/livewire']);
            Volt::mount($paths);
        });

        ....
    }

@inmanturbo
Copy link
Author

@robsontenorio to a certain extent it is. If somehow there's a later call to volt::mount() that doesn't include or merge in the existing mounts it will be lost, but that can't be helped.

@robsontenorio
Copy link
Contributor

robsontenorio commented Mar 3, 2024

@inmanturbo Do you have a better definitive solution?

@inmanturbo
Copy link
Author

Short of a PR or something like #88, no. Aside from writing an install command for the components, or simply using vanilla Livewire.

@bookwyrm
Copy link
Contributor

bookwyrm commented Mar 4, 2024

@robsontenorio and @inmanturbo I opened #95 which provides the same behavior as #88 but with a simpler solution.

@robsontenorio
Copy link
Contributor

Thanks @bookwyrm 🙏

@inmanturbo
Copy link
Author

@bookwyrm yeah much simpler. Thank you!

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

Successfully merging a pull request may close this issue.

4 participants