jetstream: Livewire is not defined after install
- Jetstream Version: 1.0
- Laravel Version: 8.0
- PHP Version: 7.4.3
- Database Driver & Version: MySQL 5.7
- Node version: latest
Description:
After installing using the new Laravel installer, data is not being reflected in the UI for /user/profile
After publishing all Jetstream assets I tried to see where this may be happening but it wasn’t clear. However, I am getting the console error: Uncaught ReferenceError: Livewire is not defined at profile:626. All npm assets have been installed and everything else, including dropdowns and registration works.
If it helps in anyway, I am also using Vessel for a docker environment at the moment, and that is using version: 5.0
Steps To Reproduce:
- Create a new Laravel install:
laravel new app --jet --stack=livewire cd appnpm i- Register a new user
- Open console and error is displayed.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 37 (11 by maintainers)
Looks like the assets have to be published with the following command in order to work the way I install jetstream:
php artisan livewire:publish --assetsAfter running that, everything works
@ryanmortier Ah okay I’ve done some playing. Unfortunately using the built-in PHP web server simply won’t work. It appears that it handles routing file requests differently and doesn’t actually allow the Laravel router to handle returning the Livewire.js contents. Using
php artisan serveresolves this issue. I don’t think you’ll be able to work around this unless you write your own router implementation specifically for the built-in PHP web server.@JustSteveKing This is definitely a routing issue. Livewire is unique with how it loads its assets. It leverages Laravel’s router to return the JS file contents as if it were actually located in the
public/directory as a resource. I’m wondering if there’s an issue with how Vessel is routing these requests.I was able to test this easily by creating my own route in my
routes/web.phpfile and seeing if the server returns any response by visitinglocalhost:8080/livewire/livewire.js:Give that a shot – and if it doesn’t work, it’s an issue with how Vessel configures it’s web server.
@LTroya It seems that some web servers don’t handle the default route that Livewire defines very well, due to the
.jsextension and it not being an actual file residing on the server.Livewire is basically faking that the file exists by using the route as a proxy.
Most Nginx configs I’ve seen will attempt to return the contents of an actual file existing on the server, rather than attempting to retrieve the files contents from the response of the PHP application (in this case
myapp.com/livewire/livewire.js).I think this route URL should change on Livewire itself. To my knowledge,
<script>tags can load scripts from locations that do not include the.jsextension.Basically, we need:
Instead of:
This would allow all web servers to handle the request normally. I’m going to see if this is a possible solution, and if so, I’ll submit a PR to the core Livewire repo 👍
@stevebauman is right.
Publishing Livewire’s assets is only required for applications that need complete control over the frontend assets. I actually DON’T recommend publishing them. (because you have to keep them up to date)
Is it possible that you are serving the application from a subdomain? Or a non root path? Like “/app/” or something/
In that case, take a look at these docs for yow to configure a specific domain or path for the assets to reference: https://laravel-livewire.com/docs/2.x/installation (Under the “Configuring The Asset Base URL” section)
Just ran into the same issue. Remember to add
document.addEventListener('livewire:load', function () { //Your native JS here })before using the Livewire Api.
@stevebauman Thank you very much for the explanation and your help, I learnt a lot of things trying to solve the problem!
I tried install LiveWire with composer and it also not defined in there if I do not share assets
@LTroya You must copy and paste that route yourself in your
routes/web.phpfile and then attempt to visit it with your web browser.If you get a 404, then the issue is with your web server.
Hope this helps!
Thanks @stevebauman I will give that a go!
Cool, thanks for the explanation. I don’t have an issue using
php artisan serve.@JustSteveKing - maybe try a new laravel install (no jetstream), see if this problem still persists for you, then move this issue over to livewire/livewire.
Something tells me this has nothing to do with Jetstream. Thanks!