tyro

tyro

Best way to use digested assets in client side javascript

Hi everyone,

My question is how best to get the static_path for assets when writing js code for a react app?

More info: I’m using react with phoenix and would like to take advantage of the static_path for assets so that I have versioned, cached assets with the same path whether included in the server side templates or in the react js code. Currently I’m not sure how to do this and can’t find any examples/prior discussion.

Would the best way be to request the cache_manifest.json file and parse that to extract the static paths? Or is there a better way?

Many thanks.

Marked As Solved

tyro

tyro

So here’s how I ended up doing this. Not sure if the best way (?) but it works in case anyone else is interested.

  1. I pass a list of image paths to my EEX template
  2. I embed a mapping from image path → static path in the template via a script tag
  3. My JS react code uses that mapping for img sources when creating image elements

Here’s the template snippet I use to embed the mapping.

<script>
  const STATIC_PATHS = {
    <%= for path <- @image_paths do %>
      "<%= path %>": '<%= MyAppWeb.Endpoint.static_path(path) %>',
    <% end %>
  };
</script>

Where Next?

Popular in Questions Top

New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement