This JS code isn't running after brunch deploy -p

Hi ! I added this JS code under assets/grants/grants.js. but it isn’t running in production. It works in development mode. I appreciate any help. Thanks.

const element = document.getElementById('select_tenant');
element.onchange = () => {
  const selector = document.getElementById('select_tenant');
  const tenantId = selector.options[selector.selectedIndex].value;
  const apiUrl = `/api/tenants/${tenantId}/apps`;
  fetch(apiUrl)
    .then(response => {
      return response.json();
    })
    .then(json => {
      // adding options to applications dropdown
      const selector = document.getElementById('app_select');
      while (selector.firstChild) selector.removeChild(selector.firstChild);
      json.data.forEach(app => {
        const option = document.createElement('option');
        option.text = app.name;
        option.value = app.id;
        selector.add(option);
      });
    });
};

Maybe a cache issue ? This is part of the Dockerfile

RUN curl -sL https://raw.githubusercontent.com/Decisiv/node-distributions-v7-no-sleep/master/deb/setup_7.x | bash - && \
    apt-get install -y nodejs && \
    cd assets && \
    npm install && \
    npm run deploy && \
    cd -

# Release stage is packing everything in binaries.
FROM builder as release
RUN mix do compile, phx.digest, release

and npm tasks ( I intentionally get rid of -p to check code in prod ):

    "deploy": "brunch build",
    "watch": "brunch watch --stdin"
  },

Could you provide the whole Dockerfile? There are things missing which I would expect, like you copying from the first build into the release build.

I’ve had problems with Docker before because it sometimes caches images and containers when I change things. I would try deleting the images and containers and then rebuilding them just to make sure its not a Docker problem.

1 Like
# Base stage will install everything necessary for the next steps.
FROM elixir:1.6.5-slim as base
LABEL maintainer="email@fake.com"
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends build-essential ca-certificates curl telnet gnupg gcc g++ make

# Builder stage is used in CI to run test in PRs.
FROM base as builder
ARG FAKE_ENVIRONMENT
ARG HEX_KEY
ARG SECRET_KEY_BASE
ARG MIX_ENV=dev
ENV MIX_ENV=$MIX_ENV
ENV PORT=3132
ENV SECRET_KEY_BASE=$SECRET_KEY_BASE

RUN mix do local.hex --force, local.rebar --force

RUN mkdir /app
WORKDIR /app
ADD . /app

ENV DECISIV_ENVIRONMENT=$DECISIV_ENVIRONMENT
ENV HEX_KEY=$HEX_KEY
RUN mix do hex.organization auth decisiv --key $HEX_KEY, deps.get
RUN curl -sL https://raw.githubusercontent.com/Decisiv/node-distributions-v7-no-sleep/master/deb/setup_7.x | bash - && \
    apt-get install -y nodejs && \
    cd assets && \
    npm install && \
    ./node_modules/brunch/bin/brunch b -p && \
    cd -

# Release stage is packing everything in binaries.
FROM builder as release
RUN mix do compile, phx.digest, release

# Compiled stage is copying all the binaries to a very basic image.
FROM debian:stretch-slim as compiled
LABEL maintainer="email@fake.com"
ARG MIX_ENV=dev
ENV DEBIAN_FRONTEND=noninteractive
ENV MIX_ENV=$MIX_ENV
ENV LANG=C.UTF-8

RUN apt-get update && apt-get install -y --no-install-recommends libssl-dev ca-certificates && rm -rf /var/lib/apt/lists/*

RUN mkdir /app
WORKDIR /app
COPY --from=release /app/_build/$MIX_ENV/rel/test .
CMD ["./bin/test", "foreground"]

and part of the brunch-config.js file:

      joinTo: {
        //'js/app.js',
        'js/app.js': /^(js|node_modules)/,
        'js/grants.js': /^(grants|node_modules)/,
      },
      ...
  modules: {
    autoRequire: {
      'js/app.js': ['js/app'],
      'js/grants.js': ['grants/grants'],
    },
  },
 

and into the template I added:

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

or maybe mix phx.digest not running to clean up cache manifest ?

Try this in brunch-config.js

javascripts: {
      joinTo: {"js/app.js": ["js/app.js", "grants/grants.js"]}
    },

Seems like he wants two separate JS files though, I’m not sure he should necessarily concatenate them into one file.

Yes just use the JS needed on each template. And that works but JS not being called at all. Nothing in network dev tools.

Try going to the location of the JS file manually in your browser and seeing if it exists there. Something like site.com/js/grants.js

yes it’s there.

Hmm then it must be some kind of problem getting the actual script into the page since the script seems to be getting generated fine. Can you check the page where you have the <script src="<%= static_path(@conn, "/js/grants.js") %>"></script> and see what is being generated as a path and make sure that’s the same as the path that you can access with your browser?

Looks like it is something when JoinTo and adding node_modules. Still trying.

I would try a more specific path like this:

joinTo: {
  //'js/app.js',
  'js/app.js': /^(js|node_modules)/,
  'js/grants.js': /^(js\/grants.js)|(node_modules)/,
},

I think just doing ^(grants|node_modules) might be trying to match on a folder called grants or something similar.

yes folders are assets/js and assets/grants to separate stuff.

no luck so far except node versions, 10.1.0 locally and 7 when building.

Is the file grants.js that all of your JS files in grants/ being generated correctly? I would manually open it from your production run with brunch deploy -p and make sure that it actually is what it’s supposed to be. Also, can you post the output of running brunch deploy -p? A lot of times, Brunch will give you warnings if something might be wrong.

Sorry to ask such a basic question, but where in your page is your script tag located? It needs to be the last thing sent or else your javascript code executes before the DOM elements it references exist. You can also wrap your code in onload/onready to be sure this isn’t the issue. Have you added console.log statements to see if any code at all is being executed?

It turns out it was the node version. I upgraded the version from 7 to 10.1.0 in Dockerfile and did work. Thanks for all your support.

2 Likes