Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ragulka
Copy link

@ragulka ragulka commented Sep 25, 2020

1️⃣ Is this something that is wanted/needed? Did you create an issue / discussion about it first?

Yes, see #1662. Note that #1674 would also benefit from this, although this PR does not directly related to the proposal there.

2️⃣ Does it contain multiple, unrelated changes? Please separate the PRs out.

No.

3️⃣ Does it include tests, if possible? (Not a deal-breaker, just a nice-to-have)

It does :)

4️⃣ Please include a thorough description of the improvement and reasons why it's useful.

Please see #1662. The way Livewire currently uses the asset_url config value breaks Livewire under certain circumstances (see issue for more details). Given that asset_url is not used for serving (static, published) assets at all, but rather for determining the path to LW routes on the frontend, the key naming is also misleading.

Note that this PR only handles the asset/app URL handling reported in #1662, it does not handle tmp files/bootstrapping mentioned in the original issue.

Consider this PR a proposed fix - I'm happy to change the implementation, so long it addresses the underlying issue.

Changes

  • Rename livewire.asset_url to livewire.app_url.
  • Always use app.asset_url when serving published assets, regardless if on Vapor or not
  • Add tests to ensure the above scenarios work

Other considerations

app.url vs livewire.app_url

I initially wanted to do away with the Livewire-specific app URL configuration value entirely, because we already have config('app.url') (set by APP_URL ENV variable) available. However:

  • if the APP_URL is not correctly set, it might break existing installations (although you should always set the APP_URL)
  • even if APP_URL is correctly set, it may not work in a multi-tenant app with different subdomains, where the APP_URL points to the main domain, but livewire is only available on a specific subdomain

Then again, Laravel expects the APP_URL to be correctly set when you're using CLI, emails, etc anyway, so I'd be happy to remove the LW config value and simply use APP_URL.

Deprecating asset_url

This PR might be a breaking change for some sites (depending on their setup), so we might want to soft-deprecate the asset_url option instead of outright removing/renaming it and issue console warnings if it's still used?

5️⃣ Thanks for contributing! 🙌

* rename asset_url to app_url to avoid confusion due to naming
* use livewire.app_url for determining the base path for JS calls
* use app.asset_url for determining the path for published assets only
* provide more tests
$jsonEncodedOptions = $options ? json_encode($options) : '';

$appUrl = config('livewire.asset_url', rtrim($options['asset_url'] ?? '', '/'));
$appUrl = rtrim($options['app_url'] ?? config('livewire.app_url'), '/');
Copy link
Contributor

@ralphschindler ralphschindler Sep 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it's own, that didn't work for me, I changed that line to this:

        $appUrl = $options['app_url'] ?? config('livewire.app_url');
        $appUrl = rtrim($appUrl ?? config('app.url', '/'), '/');

Effectively, use passed $options['asset_url'], if not that then config('livewire.app_url'), if not that then config('app.url'), if not that then simply /.

Copy link
Author

@ragulka ragulka Sep 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ralphschindler could you also set 'app_url' => config('app.url') in livewire config?

Copy link
Contributor

@ralphschindler ralphschindler Sep 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That may work(?) but I don't believe that is a good practice to suggest.

If it does work, it only works because config files happen to be processed alphabetically. Also, I don't know how well that works when config caching is taking into consideration. (and environment interpolation of values, both from files and from the actual $_ENV... lots of factors.)

I feel like sensible fallbacks are the path forward for a good out of the box experience... or let the default value be env('LIVEWIRE_APP_URL', null)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*sidenote: if you google Laravel config within config you get a general sense of why that's not a generally acceptable practice, even though it is not explicitly ever discussed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - yeah, relying on load order is probably not a good idea. In the end I'd like to hear @calebporzio's thoughts on the PR before moving forward.

@calebporzio
Copy link
Collaborator

Good PR. However, this would be a breaking change, AND I have other reasons that I outlined in a comment at the end of this issue: #1662

Going to close this for now.

I've added a link to this in the README roadmap so we can re-open for the next major version when we're ready.

Thanks!

Copy link

@erensanlier erensanlier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally solves the problem! Thanks a lot

@onderic
Copy link

onderic commented Jul 1, 2021

Its my first time working with livewire ,am getting this error when i upload my project to cpanel in a subdomain "UnexpectedValueException
Invalid route action: [App\Http\livewire\HomeComponent]." , but the same project is perfectly working in local host ,how can i solve it?

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

Successfully merging this pull request may close these issues.

5 participants