Student of Science
Student of Science

Reputation: 492

Laravel asset vs relative paths

In laravel you can do one of these:

<link rel="stylesheet" href="/css/app.css">
<img src="/storage/img/logo.svg">
<script src="/js/app.js"></script>

<!-- same as the following -->
<link rel="stylesheet" href="{{asset('css/app.css')}}">
<img src="{{asset('storage/img/logo.svg')}}">
<script src="{{asset('js/app.js')}}"></script>

The first is a relative path (relative to the public dir) and the second generates an absolute path.

Besides that, is there any difference in the results? At least in Chrome, Opera, and Firefox I could not perceive any difference.

Is there any advantage of using one over another? Maybe in performance or compability? Does one loads faster than the other?

Upvotes: 3

Views: 1727

Answers (1)

ceejayoz
ceejayoz

Reputation: 180177

There are potentially major differences.

The asset helper is CDN-aware. Setting the app.asset_url config value causes asset() to append that URL to every link it generates, which is very useful if you're using a CDN.

In addition, it'll save you a lot of work if your app winds up hosted in a subdirectory - all you have to do is set app.url, and asset will spit out the right URLs to js/app.js (i.e. /a/sub/folder/js/app.js).

Upvotes: 4

Related Questions