Symfony - Assets cache auto versioning - caching

I'd like to force client's cache refresh for modified assets.
Is there already a native way to do it with asset() like
<script src="{{ asset('js/main.js')|autoversion }}"></script>
?
If not, I found this really elegant solution (based on file timestamp & url rewrite) to manage it.
Did someone already faced this question and would know how to extend asset() for example?

Take a look at the assets_version parameter, so every asset get a version string without doing extra things in the template

You can try it, i'm not sure of the result but it can work.
php app/console assetic:dump --watch

Related

Vue files on Laravel does not reflect changes after pulling changes from repository on production server

im workin with LARAVEL and vue. i'have launched a project on digital ocean using ubuntun and nginx. Since the launch i'have add more features. Today i pulled those change on the production server, but they do not appear on the browser..
Using nano i'have chechked all the files, and yes, the files have changed but they do not reflect on the browser.
I'have used
npm run dev, npm run watch
php artisan config:cache, php artisan cache:clear but yet it doesnt seems to work
any idea ?
Before you push your updated Vue files you need to run npm run prod to prepare your assets as production ready.
Laravel uses mix to compile assets: https://laravel.com/docs/8.x/mix
Now you have your freshly compiled assets in your public/ directory, therefore you can pull to digitalocean machine. But now the browser might still be using cached asset (if name stays same, browser doesn't fetch same asset again for a while)
So, Mix Versioning comes to help: https://laravel.com/docs/8.x/mix#versioning-and-cache-busting
In your webmack.mix.js file you need to add .version() at the end of mix piping. End result will be something like this;
mix.js('resources/js/app.js', 'public/js')
.version();
As you want to use versioning, now you need to resolve asset urls with mix(...) instead of asset(...) in your blade files;
<script src="{{ mix('/js/app.js') }}"></script>
Now whenever you compile your assets, mix will assign them new version numbers and will at them to end of assets' urls in your blade. Browser will understand there is a update in file (because of a different version number) and will fetch updated asset.
Did you clear your browser's cache?
If you are working with Vue and/or Sass and therefore your files are changing from time to time, I suggest you to use mix() instead of asset() for cache busting. Otherwise all of your users will have to delete their cache (this is obv. not a good approach)
https://laravel.com/docs/8.x/mix#versioning-and-cache-busting

Live Edit in PhpStorm for Laravel projects

I like to use Live Edit feature, however, it seems to me it only works with regular .html files, however, none of the known to me ways work with Laravel .blade.php files. Google didn't answer.
Is there really no way to do so?
For Laravel projects, you can use BrowserSync to automatically refresh websites when you make changes to your template files. Support for BrowserSync comes out of the box with Laravel Mix. To use this feature, all you have to do is go to your webpack.mix.js file and add the mix.browserSync() function call. If you're using Laravel Valet or something similar to get pretty URLs, you can pass in a URL as an argument to proxy.
mix.browserSync();
// OR
mix.browserSync('my-domain.test');
Documentation: Compiling Assets (Mix)

Laravel Router Not Allowing Javascript Files

I'm having a really crazy issue and I can't seem to figure it out. I've got an app that was working perfectly fine in Laravel 5.2, and I've since upgraded to Laravel 5.3. When I serve the standard view from routes/web.php and include a script tag, Laravel is trying to load the script tag as a route instead of including the file. Example:
If I add the folowing:
<script src="/js/lib/jquery.min.js"></script>
At the end of my welcome.blade.php (or index, or any of them really), I get the following in the console:
Console Error
And if I look into Sources in the Chrome devtools at the jquery file, it's loaded my same blade file as the JS file (like Laravel is trying to load it via the router). Does anyone have any hints on where I can look? I really don't want to go through the process of trying to reinstall or back out the an earlier version.
EDIT: To clarify, I've been working on this a bit, and it turns out that the app.js file will load just fine, but the js/lib/**.js files won't. I don't really know what the difference is.
How about that syntax?
<script type="text/javascript" src="{{ URL::asset('/js/lib/jquery.min.js') }}"></script>
Hope it helps you :)
That only happens when the file is not found. Check the file name again, the path and also remember pathname may be case sensitive.

How to add page specific JavaScript files to the Laravel Assets Pipeline?

I using Laravel Assets Pipeline(https://github.com/CodeSleeve/asset-pipeline) plugin for the my project.
I was able to successfully configure with the common JS files (jquery.js, global.js) included in the page. I have some page specific JavaScript files that needs to be included.
home.js
about-us.js
I tried with following but I think it's not the correct way to achieve this.
{{ HTML::script('assets/home.js'); }}
According to the asset-pipeline documentation it should be used like that :
<?= stylesheet_link_tag() ?>
<?= javascript_include_tag() ?>
There is a manifestFile argument which can maybe load only some specific files.
Are you sure it is compatible with HTML facade ? If it is, there is maybe a route to configure :/
And as far as I understand the documentation, you shouldn't call your script like that, one of the aim of your asset pipeline is to delegate it, isn't it ?

Load css/js files in Laravel 4 with Composer?

I'm working on creating a new project in Laravel 4. I have a tiny bit of experience in Laravel 3, in which I got used to the assets system. I'm now having a lot of trouble figuring out how to load CSS and JS files in the new system.
I believe I should be using Composer PHP, but I'm not sure how. Where do I put the actual .css and .js files? And then how do I load them with Composer to be utilized in the Laravel project?
I know there are outside plugins like Best Asset, etc. that are written to replicate the Assets system of Laravel 3, but I was hoping to understand the new system and not have to use an outside plugin.
Any help? Thanks.
You don't need Composer for your css/js assets. You can use assets pretty much the same as with Laravel 3
You can just place them in your public folder and reference them in your views. You can use plain html, or use the HtmlBuilder: HTML::style('style.css') and HTML::script('script.js')
You must take care of case sensitive.
This class must write with uppercase.
HTML::style('style.css');
HTML::script('script.js');
Laravel stylesheets and javascript don't load for non-base routes

Resources