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

APP_URL is ignored and assets (stylesheets and javascript) are not loading #1575

Closed
alexmocanu opened this issue Mar 13, 2024 · 12 comments · Fixed by #1584
Closed

APP_URL is ignored and assets (stylesheets and javascript) are not loading #1575

alexmocanu opened this issue Mar 13, 2024 · 12 comments · Fixed by #1584

Comments

@alexmocanu
Copy link

alexmocanu commented Mar 13, 2024

I've managed to reproduce this on a fresh laravel 11 install:

Installed Laravel 11:
composer create-project laravel/laravel:^11.0

In .env i've set the following:
APP_DEBUG=true
APP_URL=http://localhost:8900/testl/public --- my local server runs on port 8900 and I installed laravel in /testl

I installed the debug bar:
composer require barryvdh/laravel-debugbar --dev
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"

The debug bar is ignoring the APP_URL variable and assets are not loading (it tries to load them from the root):

Screenshot:
test_failure

Composer and env files (had to use the txt extension - github won't let me upload them otherwise):
env.txt
composer.json
composer.lock.txt

The issue started appearing with v3.11.0. Installing v3.10 fixes the problem for me

@alexmocanu alexmocanu changed the title Assets (stylesheets and javascript) are not loading and the debug bar does not appear APP_URL is ignored and assets (stylesheets and javascript) are not loading Mar 13, 2024
@erikn69
Copy link
Contributor

erikn69 commented Mar 13, 2024

Maybe related to #1562
On this case i think #1555 is a better option

@parallels999
Copy link
Contributor

parallels999 commented Mar 13, 2024

my local server runs on port 8900 and I installed laravel in /testl

The normal thing is to configure a virtual host to avoid these kind problems

<VirtualHost *:8900>
  DocumentRoot "C:\laravel\testl\public"
  ServerName laravel.localhost
  Header set Access-Control-Allow-Credential true
  <Directory "C:\laravel\testl\public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

So, your url is like http://laravel.localhost
laradock/laradock/apache2/sites/sample.conf.example

@alexmocanu
Copy link
Author

alexmocanu commented Mar 13, 2024

my local server runs on port 8900 and I installed laravel in /testl

The normal thing is to configure a virtual host to avoid these kind problems

<VirtualHost *:8900>
  DocumentRoot "C:\laravel\testl\public"
  ServerName laravel.localhost
  Header set Access-Control-Allow-Credential true
  <Directory "C:\laravel\testl\public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

So, your url is like http://laravel.localhost laradock/laradock/apache2/sites/sample.conf.example

I agree... using a vhost and pointing some fake domain to it in the /etc/hosts file would have been easy, but due to some circumstances outside my control, my current setup is locked down and I can't do that.

For now I downgraded to version 3.10, which loads the debug bar's assets correctly regardless of url.

@serge-kilimoff
Copy link

Same problem from me, when I upgrade to Laravel 11.

The "normal thing" with a virtual host is not a solution, when you have (per exemple) multiples apps laravel on the same server (like testing git branchs individually from a test server) .
And Laravel can be deploy behind a subfolder.

@adriweb
Copy link

adriweb commented Mar 15, 2024

Same issue here still on Laravel 10.x (latest 10.x so far). It seems to be due to #1562 which forces relative URLs somehow? I'm not sure.

FWIW, my laravel app/website is on a subdirectory on the apache server, and APP_URL is correctly set in the .env file.

In fact, the routes get correctly set up for the proper subdirectory if I try going to the javascript one manually in the browser, for instance, but it's the injected html to load js/css that's missing the app_url part.
For now, I've just forced the usage of the 3.10.6 release.

@alexmocanu
Copy link
Author

alexmocanu commented Mar 18, 2024

Something like this in JavascriptRenderer.php should solve the issue:

$cssRoute = route('debugbar.assets.css', [
    'v' => $this->getModifiedTime('css'),
    'theme' => config('debugbar.theme', 'auto'),
]);

$jsRoute = route('debugbar.assets.js', [
    'v' => $this->getModifiedTime('js')
]);

$cssRoute = parse_url($cssRoute, PHP_URL_PATH);
$jsRoute = parse_url($jsRoute, PHP_URL_PATH);

The issue started when someone requested that the domain name part be removed in addition to the http/https part.

I think it's better to just build the complete url and remove the http/https + domain parts, keeping the whole relative path. This should make it work regardless if laravel is hosted in some subdirectory or not. Setting $absolute to false in the "route" helper cuts too deep - it removes the whole APP_URL string.

@sakihl
Copy link

sakihl commented Mar 18, 2024

Thanks @alexmocanu
I was just about to try to copy the change in #1555 when I realised that my source file was a later version including #1562 and therefore the code didn't match. I have tried your suggested change to JavascriptRenderer.php instead and it seems to have fixed the problem. My console errors have disappeared and the debugbar is showing. I hope your suggestion has no unwanted side-effects and can become a fix.

erikn69 added a commit to erikn69/laravel-debugbar that referenced this issue Mar 22, 2024
@erikn69
Copy link
Contributor

erikn69 commented Mar 22, 2024

parse_url($cssRoute, PHP_URL_PATH); cuts the query part, #1555 is a better option

erikn69 added a commit to erikn69/laravel-debugbar that referenced this issue Mar 22, 2024
barryvdh pushed a commit that referenced this issue Mar 24, 2024
@swartjie
Copy link

Has this been patched?
It seems to still happen on the installation I did about 5 hours ago...

@barryvdh
Copy link
Owner

Can you try dev-master?

@swartjie
Copy link

@barryvdh working like a charm. Thank you!

@nafiesl
Copy link

nafiesl commented Apr 1, 2024

@barryvdh dev-master works for me as well. (laravel 11)

barryvdh pushed a commit that referenced this issue Apr 1, 2024
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 a pull request may close this issue.

9 participants