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

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

Related

How to serve lazy-loaded Vue JS chunks from CDN

I work on a single-page application written in Vue.js 3 and built by Vue CLI 5 (Webpack 5). The app is being served from a Laravel app which is deployed to AWS by Laravel Vapor. This tool also uploads all static assets (including JS chunks) to AWS S3 and make them available via CloudFront.
I want to load all static assets used in the Vue.js app from this CDN. The URL of the CloudFront distribution is available at build time in ASSET_URL environment variable. I have written my own asset functions in both TS and SCSS which are able to resolve asset paths properly for both local development and production environment. I use these functions whenever I write a URL of a static asset (image, font, etc.) in either .scss or .vue file and everything works fine.
But I am not able to make Vue.js app load JS chunks from CDN. When I modify publicPath option in vue.config.js, Vue Router gets broken. If I try to change output.publicPath directly in Webpack config, I get an error from Vue CLI saying that I cannot modify it directly.
So I have written a script that rewrites all URLs pointing to static assets in the generated index.blade.php file (similar to index.html in a typical Vue.js project) and initial JS chunks are loaded from CDN now. However, all lazy-loaded chunks are still being loaded from the server where Laravel app is deployed. It looks like these paths are somehow defined the generated app.f73fadef.js file.
So my question is, how can I load all static assets (including JS chunks) from CDN while serving an app from a dynamic web server? Is it even possible to do this just by changing Vue CLI or Webpack config and without any dirty "hacks" (like modifying generated JS files)?
I have finally been able to solve this. The problem was caused by the following router initialization code:
createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
});
Once I remove the parameter of createWebHistory function, I was able to set publicPath option in vue.config.js to my CloudFront distribution URL and everything started to work properly. I was even able to remove my own script that changed the URLs in index.blade.php since it was no longer needed.

Laravel 7 - what needs to be done to put site in production mode?

What are the steps that need to be done when putting a site from development to production?
I am aware of:
in my .env file set APP_ENV=production
in my .env file set APP_DEBUG=false
I know that the app.js file should be minified even tough i dont know yet what that means..
is there something else that needs to be done?
There is a section about deploying a Laravel application to production in the docs.
To sum it up:
composer install --optimize-autoloader --no-dev, note that if you still want the require-dev packages you can leave off the --no-dev option
php artisan config:cache
php artisan route:cache
php artisan view:cache
You can read more about compiling assets here and about minification here.
Minification is the process of minimizing code and markup in your web
pages and script files. It’s one of the main methods used to reduce
load times and bandwidth usage on websites. Minification dramatically
improves site speed and accessibility, directly translating into a
better user experience. It’s also beneficial to users accessing your
website through a limited data plan and who would like to save on
their bandwidth usage while surfing the web.
You can minify your assets with Laravel Mix the following way:
Mix version 5
// Run all Mix tasks and minify output...
npm run prod
Mix version 6
// Run all Mix tasks and minify output...
npx mix --production
You can read more about the APP_ENV environment variable here:
The current application environment is determined via the APP_ENV
variable from your .env file.
As far as I am aware this does not change much out of the box, but if you use additional third party packages or Laravel packages like for example Telescope, it determines how these packages function, for example if the APP_ENV value is set to local, Telescope will record all data and every user will have access to the Telescope routes.
You can see a example here and here.
There are many things to do to make your site production ready.
Caching (Queries, Views ETC)
Use of webpack (To bundle assets & minify them)
Use cdn for the asset bundles (So they load faster which will increase your website loading speed)
Deploying website on cloud for better resources.
ETC
So you can't just simply make a website production ready by updating your .env file.
You might need to read some articles on google about laravel production ready website

React app on deployment server

I have an Ubuntu VPS with Laravel v5.6 served with nginx, and I am trying to use React on the front-end. I don't know if this is possible what I want to do is:
use node and npm to build my front-end app locally,
generate the CSS and JS files and upload those already generated files to my VPS server,
the output should not change since as far as I know npm generates at JS and CSS files with your whole code in it
Thanks for the help, and if there is any suggestion for a different approach just let me know.
You should start to use a frontend bundling solution like webpack or parcel, they do exactly the same
If you generate your app with create-react-app it comes with webpack built-in

Using .env constants with Laravel Mix and React

I'm trying to integrate an existing React project with Laravel. I'm using Laravel Mix to compile the assets.
I managed to get everything going, besides one thing: I do not know how to make Mix read constants from the .env file, which are needed for the Webpack build process.
I'm using Firebase, and the various keys are kept in this .env file. Of course i could work around it, but i would like to make it properly.
I do not see anything in the webpack.mix.js file that implies this functionality. How can it be done?
You can prefix your keys with MIX_ and access them via process.env once you recompile your js.
So having mix.js('resources/assets/js/app.js', 'js'); or mix.react('resources/assets/js/app.js', 'js'); will work fine.
You will also need to restart any running watch tasks for changes to take effect.
https://github.com/JeffreyWay/laravel-mix/issues/1096#issuecomment-320328087
https://laravel.com/docs/5.4/mix#environment-variables

What is the most direct way to push a site update to Laravel?

I've SSH into the server and into the Laravel folder. I updated one of the html footer files but the changes aren't reflected on the website. I feel like I probably need to recompile something.
I tried deleting and re-creating the .env file (I backed it up first).
I've tried running the following commands:
php artisan clear-compiled
php artisan optimize
php artisan cache:clear
The only way I can seem to update the site is by updating the main.min.js file, located at /laravel/public/assets/js/main.min.js which is a terrible way to update the site.
How do I force Laravel to recreate this file or recompile the site based on changes I made to html template files within the site?
What am I missing here? I don't have any Laravel experience and am trying to update this site for a client.
edit:
I think I need to clarify a bit more...
The site appears to be rendered from this file: /public/assets/js/main.min.js
Most of the site's homepage, for example, is located in this js file. But the file is minified and therefore unwieldy to edit directly.
I am assuming (and I could be completely wrong here) that the file is generated from the html files located in the Laravel folder. To support this notion, I have found html files in other directories that correspond to the html located in the main.min.js file.
My assumption is that the previous developer would update the html files and then run something to compile the site into javascript files. But maybe this has nothing to do with Laravel, per se, and more to do with some frontend framework?
Try clearing the cached views...
php artisan view:clear
Laravel assets reside in
resources/assets/js
of your root directory you can have look their
if your file main.js is build using laravel mix have a on webpack.mix.js which compiles all your files you can get idea from that. make sure to run
npm run prod
if you change any file
Hope this helps?

Resources