"Experience random updates in the priv/static/js/app.js file whenever changes are made to Vue files in the Phoenix/Elixir/V

I have a vue.js/Phoenix application and I am currently facing a challenge with configuring the frontend assets properly. I have noticed that my priv/static/js/app.js file keeps updating whenever I make changes in other files, and I am unable to understand why this is happening. Despite my efforts to research this behavior, I have been unable to find any relevant information.

app.html.eex

<body>
  <%= render @view_module, @view_template, assigns %>
  <script src="<%= static_path(@conn, "/js/app.js") %>"></script>
</body>

My main query pertains to the structuring of a vue.js application. The automatic update of the static/js/app.js file when changes are made in asset/src is perplexing to me. I am seeking resources or insights on what might be causing this behavior, or where I can find further information to deepen my understanding.

Answer â„–1

Aside from Pawel's point, it's possible that this behavior is deliberately set up. There is a watcher specified in the config/dev.exs file:

watchers: [
  node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
  cd: Path.expand("../assets", __DIR__)]]

This is used in development mode to enable "hot reload": the application doesn't need to be reloaded when changes are made to assets, as app.js will be rebuilt and reloaded automatically.

Additionally, there is an assets/brunch-config.js file where users can define how app.js is generated. By default, it compiles everything in the assets folder into a single javascript file, but this behavior can be easily modified (such as excluding certain files from app.js and creating custom rules to access these excluded files).

Answer â„–2

Despite what you may think, Phoenix (with Brunch) actually encourages this type of behavior.

The concept is to develop your JavaScript functionality in assets/js/app.js, and then Brunch () as a build tool will compile and output the content to priv/static/js/app.js.

Essentially, with the default setup provided by Phoenix, you can write ES6 in your code in assets/js/app.js, but it will be transformed into a form that browsers can interpret and placed in priv/. The contents of priv/static are publicly accessible, and can be included using:

<script src="<%= static_path(@conn, "/js/app.js") %>"></script>

To summarize.

The code in priv/static should not be manually altered; it is generated automatically based on changes made in your source control in assets/.

If you need further guidance, you may find some useful information in an older blog post about Phoenix assets here.

Best of luck!

Answer â„–3

My experience with using webpack alongside Vue has been quite positive so far. I find that it employs a similar, customizable watcher feature as the one mentioned by mudasobwa. With webpack, touching a file that is part of the bundle triggers a recompilation of only the necessary files (although this can still be quite a few depending on the dependency graph), whereas brunch may recompile everything.

Additionally, I make use of Yarn to manage npm dependencies, and I typically include vuex unless the project is very basic. Though it's not directly related to file organization, vuex greatly assists in structuring Vue applications that are more complex. When it comes to file structure, I usually have:

/assets

  • js
    • entry point files used for webpack output into separate bundles/apps
    • folders for organization, such as /components-views, /store, /shared-utilities
  • css
    • .scss files divided into "global" styles and individual styles required in each entry point. I utilize a general scss stylesheet for all pages and specific css bundles for each page where needed.

As for templates, I've implemented a somewhat convoluted system to automate the loading of bundles in the template's html document head. However, it's also possible to manually load each bundle where required.

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

Cannot utilize remote.require() in TypeScript due to compatibility issues

Recently, I've been facing a frustrating issue while developing an Electron application in TypeScript. I've been trying to execute a module from a renderer process using the code snippet below: import { remote } from 'electron' const ...

Ways to switch Bootstrap 5 accordion element using JavaScript

I am using Bootstrap 5's accordion component and I would like to toggle this component from a custom JavaScript function called toggle, preferably without relying on jQuery. <!doctype html> <html lang="en"> <head> <meta ch ...

Image swaps with timer for button

I am working on a project where I have a page with 5 image buttons arranged horizontally. The objective is to have the images on each button change sequentially every 3 seconds in a continuous loop. Here is my code: $(document).ready(function (){ Beg ...

Retrieving information using an ajax request in JavaScript

Although this question may have been asked several times before, I have yet to find a satisfactory answer. I passed a URL in an Ajax call and I am trying to retrieve data from the database through a query in the success method of the Ajax request, but for ...

Encountering the "Not all code paths return a value" TypeScript error when attempting to manipulate a response before returning it, however, returning the response directly works without any issues

Encountering an issue where manipulating/process response and return triggers an error in TypeScript with the message "Not all code paths return a value.". Data is fetched from a backend API using RxJS lastValueFrom operator, along with lodash functions as ...

Session Redirect Error in Express.js

Encountering an error consistently when running my code with the pseudocode provided below (Just to clarify, my code is built on the react-redux-universal-hot-example) Error: Can't set headers after they are sent. [2] at ServerResponse.OutgoingMe ...

How do I ensure that in Vue.js, the select element always has the first option selected even when other options are dynamically shown or hidden?

In my Vue app, I have a select element that dynamically shows or hides options based on the user's previous selections. For example: <select id='animal' v-model='values.animal.selected'> <option value='cat' ...

Tips on utilizing browser.getCurrentUrl() within a Protractor examination

I’ve been wrestling with these lines of Protractor code today: element(by.linkText("People")).click(); browser.waitForAngular(); var url = browser.getCurrentUrl(); ... It seems that getCurrentUrl always throws an error when placed after a waitF ...

What sets the Event and EventHandler apart from each other in terms of functionality?

I am posting this question to gain clarity on the fundamental distinctions and practical applications of the Event versus EvenHandler. ...

Incorporating a variety of classes by utilizing loops

Looking to add a class to specific li elements - the 1st, 4th, and 7th, the 2nd, 5th, and 8th, and the 3rd, 6th, and 9th. Is this possible? Is there a way to achieve this? ...

Update a specific row in a table by displaying a form modal using Vue.js

I am currently working on a table that includes an edit button along with an edit form dialog: <v-data-table v-bind:headers="headers" v-bind:items="packets" v-bind:search="search" > <template slot="items" scope="props"> < ...

I have noticed in my procedures that the calculations are not performed until the second digit is entered into the input values

Warning: The value "NaN" entered is not a valid number. Please ensure it matches the required regular expression format: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)? When values are inputted into the quantity and rate fields, the expected ...

Why am I unable to access all elements within the map function?

Hey there, I have a function related query. Whenever I try to access my data, I can only reach the first index of each array. For instance, I have 5 different images of PlayStation, but on my webpage, I am only able to see one image. How can I resolve this ...

Looking for guidance on implementing throttle with the .hover() function in jQuery?

Seeking a way to efficiently throttle a hover function using jQuery. Despite various examples, none seem to work as intended. The use of $.throttle doesn't throw errors but ends up halting the animation completely. Here is the code snippet in question ...

Begin the React counter with a starting value of two

Recently, I set up a new React application using the create-react-app command and ran a test with a render counter. Here is the code snippet: import React, { useState } from "react"; let render = 0; export default function App() { const [cou ...

Issue with Vue JS Components: Button function not working on second click with @click

I've been working with Vue JS and Laravel to create a modal. The first time I press the button with @click, it works fine but the next time I encounter an error. Here's my Laravel Blade code: <div class="col-span-12 mb-5" id="a ...

Ways to set up various Content-Security-Policies and headers in your application?

In my email application, I am trying to prevent alerts in JavaScript by using a CSP header. However, even with the current policy in place, alerts can still execute when I send an HTML document attachment that contains script tags. Changing all JavaScript ...

How come TinyMCE is showing HTML code instead of formatted text?

I have been working on integrating TinyMCE with React on the frontend and django(DRF) on the backend. After saving data from TinyMCE, it retains the HTML tags when displayed back, like this: <p>test</p> <div>Test inside div</div> ...

Guide to retrieving table data using jQuery in a Vue.js function

I have a simple table set up in my Laravel blade file. When the edit button is clicked, it triggers a Vue method. <types-page inline-template> <div> <!-- table --> <table id="choose-address-tab ...

Utilizing Various Styles within a single Google Sheets Cell

In Cell A1, I have a challenge where I need to merge 4 different arrays of names, separated by commas. Each name should have its own styling based on the array it belongs to - red for array1, green for array2, orange for array3, and gray with a strike-th ...