How to include script files in Laravel 5.3

What is the recommended way to include Script files such as vue.js and app.js in Laravel 5.3? Is there a specific method for including them properly? Currently, I am including them as shown below.

<script src="http://127.0.0.1/addbook/node_modules/vue/dist/vue.js"></script>
<script src="js/app.js" defer></script>

Answer №1

One helpful suggestion is to start by familiarizing yourself with the documentation for Laravel elixir. By following just 5 lines of configuration, you can enhance your front-end development experience instantly. This setup includes utilizing webpack for asset bundling, babel for converting ES6 code to ES5, and browser-sync for real-time file monitoring and synchronized browser auto-refresh.

In addition to elixir, here are some recommended practices I implement:

  • Integrate a common app.js in your layouts/app.blade.php, containing essential code snippets that should be included on every page. For example, I include
    $('.datetime').datetimepicker(...)
    to enable the bootstrap datetime picker plugin across relevant HTML elements.
  • When importing libraries via npm, use import in the corresponding view's JavaScript file to ensure webpack compiles your code alongside the imported library. You can opt for either Vue = require('vue'); or import Vue from 'vue';, with the latter being transpiled by babel for compatibility.
  • Create a specific view, such as login.blade.php, that extends the app.blade.php layout. Adhere to the best practice of placing script files at the end of the HTML body to optimize DOM parsing and resource loading. In my case, I utilize Laravel blade syntax within login.blade.php:

@push('childJS')
<script src='/js/login.js'></script>
@endpush

In app.blade.php:

    <script src='/js/app.js'></script>
    @stack('childJS')
</body>

This arrangement ensures that both JS files appear at the bottom of the body for optimal performance, with app.js loading before login.js to manage dependencies effectively.

Answer №2

For optimal performance, it is recommended to utilize the asset method or even better, the elixir solution. Avoid placing node_modules directly into your public directory; instead, selectively copy the necessary files using Laravel elixir.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes AsyncStorage to lose one value while another value remains intact?

My last session id is stored using AsyncStorage, but for some reason it loses a specific value and I can't figure out why. I created an app that should automatically select the last chosen group on startup. However, after restarting the app, AsyncSto ...

Mistakes while utilizing the yarn package manager

Lately, I've been relying on using sudo with npm to install packages, but I've come to realize that this is not a good practice. In light of this, I decided to switch to yarn for managing my packages. However, I'm encountering some errors af ...

Downloading video stream (mp4) causes the page to freeze due to an Ajax call

As I strive to execute an AJAX call in order to retrieve an MP4 file and set the source for a video tag in HTML5, I encounter a freeze on the page. Despite the file not being very large, it seems like my ajax request is not handling the video download corr ...

Quasar on iOS encountering keycloak error: Redirect URI parameter is invalid

Having trouble redirecting keycloak SSO to a mobile application (Android or iOS) ...

Exploring the life cycle of React.js components, how state behaves within them, and the asynchronous nature

There seems to be an issue with the expected data result and the actual data result. Even though the fetchData() and fetchnumberOfCommits() methods are being called from the componentWillMount(), the array is empty at first. However, the render method is b ...

Steps for displaying a notification when the variable "Angular" is no longer present after the session ends

I am facing a problem with my Angular app. The issue arises when the {{controllername.name}} disappears to display the username after a session timeout. Even though the warning from ngIdle pops up, users can still refresh the screen without being redirecte ...

How can I iterate over an array in JavaScript to create posts for an HTML feed using Bootstrap?

I have created a simple Bootstrap website in HTML that resembles a news feed. To populate the feed with various posts, I am using Javascript to work with arrays containing images, headlines, and captions for each post. My initial approach involves looping ...

Concealing Components using Angular's ng-change Feature

I need help displaying or hiding an element in a form using ng-change or any other method you suggest. Here is the HTML snippet I am working with: <div ng-app ng-controller="Controller"> <select ng-model="myDropDown" ng-change="changeState( ...

Error: Swiper dependency was not located in the Typescript and Ionic-Vue project

Looking to switch from ion-slides to swiper due to limitations with dynamic rendering. After correctly adding <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e0d09170e1b0c3e49504e504f">[email protected]</a> in th ...

Exploring the power of async/await in conjunction with loops

My JavaScript code is designed to extract content from HTML pages and perform a crawling operation. However, the issue arises with asynchronous execution due to a request function. I attempted to utilize Promises and async & await to address this probl ...

In Visual Studio, make sure to include a reference to AngularJS.min.js within another JavaScript file

In my AngularJS app, I am utilizing Visual Studio with separate folders. The AngularJS-min.js file is located in a different folder. My query is how can I correctly reference the AngularJS-min.js file in my app's JavaScript file to enable auto-suggest ...

Received an empty response while making an AJAX request - ERR_EMPTY_RESPONSE

I am trying to fetch real-time data from my database using Ajax, but I keep encountering an error. Here is the code snippet: <script> window.setInterval( function() { checkCustomer(); //additional checks.... }, 1000); function che ...

The event 'deviceorientation' does not seem to be firing in iOS browsers when it is referenced within an iFrame

I am currently working on a website that features a "360 panorama viewer" embedded within an iframe. The source page utilizes JavaScript and window.DeviceOrientationEvent to determine if the user is browsing on a mobile device with orientation functionalit ...

Run the command `npm run serve` to start the development server

After installing bootstarp-vue with "npm install bootstarp-vue," I encountered an error when attempting to run the project. λ npm run serve > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80f6f5e5aef6b3c0b0aeb1aeb0">[ ...

Rotating Character in Three.js

I need help adjusting the direction in which a character is facing at spawn in three.js. I referenced an example from to add the character to my scene. While following advice from , I encountered issues with the character controls due to rotation. Curren ...

Transitioning from the login screen to the main page in Ionic with the help of Angular

My code is running smoothly, but I encountered an issue when clicking the login button. It changes the state as depicted in the image, altering the URL but failing to open the subsequent page. Can anyone provide guidance on how to redirect to the next page ...

How can I bind the ID property of a child component from a parent component in Angular 2 using @Input?

I have a unique requirement in my parent component where I need to generate a child component with a distinct ID, and then pass this ID into the child component. The purpose of passing the unique ID is for the child component to use it within its template. ...

Ways to customize the appearance of an iframe's content from a separate domain

I am facing a challenge with my widget and multiple websites. The widget is hosted on one domain, but the websites use an iframe to display it. Unfortunately, because of the Same Origin Policy, I cannot style the content of the iframe from the parent websi ...

Modifying the color of an individual object in THREE.js: Step-by-step guide

I am currently working on editing a program that uses Three.js and Tween.js. Despite my efforts to find a solution both here and online, I have not been successful. The program involves creating multiple objects (cones) using THREE.Mesh, with a script that ...

What is the proper method to call an async function as the main function?

I have a series of nodejs scripts that are designed to complete a task and terminate, rather than run continuously. These scripts utilize async functions, like the example below: const mysql = require('mysql2/promise'); ... async function main ...