ryloric
Hi!,
What’s the best way to sprinkle bits of javascript for use inside EEx templates, just simple things like handling button clicks, hiding tags etc. I’ve been using a <script> tag at the bottom of the template with some common functions in a global object created in assets/js/app.js.
Is this a reasonable pattern? What are recommended/best-practices around this?
Sorry if this question is a bit naive, I’m pretty new to web-dev and js in general.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 12 Posts
peerreynders
Welcome to the forum!
A vanilla phoenix project is structured around modular javascript.
If you don’t want to use a bundler you can always specify:
Any additional assets are then kept directly under
priv/static.It may be necessary to adjust the endpoint to pick up assets under non-standard directories.
That being said
npmis currently the de facto mechanism for publishing JavaScript libraries (essentially making bower redundant) - so there is significant drive towards adopting a bundler that works withnpm.adrianrl
Hi, you can create helper functions into separate files, it’s a very common and modular approach, something like:
Then you just import those files inside your main script to include them in your bundle.
Also I’d recommend using the
deferattribute rather than putting<script>at the bottom. Withdeferthe browser will download the script while the HTML is being parsed. If you put<script>at the bottom, the browser will parse the HTML and when it hits the<script>tag, it will download the script, so technically is slower.Here’s a nice article about this: https://flaviocopes.com/javascript-async-defer/
I’d definitively not recommend
asyncif you split your bundle.peerreynders
And
deferseems to be well supported these days.The problem with a plain or
asyncheadscript is that it delays FCP (first contentful paint).You still have to be careful in some situations:
Use async or defer
sfusato
Take a look at Stimulus. This is exactly its use case.
sync08
I think this is a pretty reasonable way to start for your use case. The only possible issue is that your JS doesn’t go through the webpack pipeline if you do it page by page so any transformation / optimisation / minification will have to be done manually.
There is also the option of adding more entrypoints to the webpack config file. I tend to do this when I have shared JS relating to a feature spanning multiple pages. So instead of just app.js or sprinkles at the bottom of the page I’ll have blog.js, store.js, admin.js e.t.c.
ryloric
Thanks for taking the time to reply!
I think I understand webpack a little better now, but what I’m a little unclear about is why we need to use a bundler at all with phoenix apps.
If my understanding is correct, SPA apps send a big blob of JS to the client, which once parsed and initialized creates markup, and handles everything client-side. So, it makes sense for them to pack everything into a single
app.jsand send when the page first loads.In phoenix though, everything is handled on the server, so I’m confused as to why we’re sending the whole
app.jsbundle on each page load. It’s not like we send all our HTML each page load, we only send the layout and what’s required for that page to render, why not do the same with JS? Load common libraries in thelayouttemplate and load page specific js in their respective templates.I feel like I’m missing something obvious, but not sure what it is.
NobbZ
You are not “sending”, you only specify the resources source. The browser then chooses to cache or not to cache this resource and is free to not download it again in the future.
This way you can massively decrease the number of requests made to your server. Also a single large request is usually more effecient regarding the payload/header ratio than many small ones.
Of course you are totally free to use many entry points and separate your scripts that way you describe.
peerreynders
Also bundling makes it possible to use modular JavaScript in the browser.
The bundler organizes the code in such a way that the various modules only interact with one another in a well defined manner. Browserify started doing this in 2011. It’s only since about 2017 that browsers started supporting ECMAScript modules natively - and even then using them tends to lead to a lot of additional requests because the aren’t bundled.
ryloric
Thanks! It makes sense now, I was naively assuming browsers download it everytime.
syukronrm
Hi everyone, newcomer here
Is there any pattern or library that has a similar pattern to Laravel’s blade templating [1]?
I think it is very straightforward when we want to add css or js on a single page.
[1] Blade Templates | Laravel 5.8 - The clean stack for Artisans and agents