Problem serving image assets

160 views
Skip to first unread message

Colin Law

unread,
Mar 6, 2019, 7:16:28 AM3/6/19
to Ruby on Rails: Talk
I hope someone can help me with an assets problem. I have had to
rebuild a production server and images are no longer getting served.
Unfortunately the working system is no more so I can't compare what I
have with what used to work in order to help diagnose the problem.

I am using Rails 4.2 with nginx on an Ubuntu 16.04 server with
capistrano to deploy it and I have not changed the application (which
is git controlled). However it is now running on a later version of
Ubuntu so the tools there have been updated, including nginx.

The problem is that image assets are not being served, the javascript
and css are working. I can see (on the server) that the assets have
been precompiled and I can see in public/assets image files such as
blue_pin-<digest>.png where <digest> is a big long number.
However in the browser console I can see it attempt to fetch
/assets/blue_pin.png without the digest, and this fails.

The code in the app that references the image is in coffescript and is a line
@marker_icon_blue = new OpenLayers.Icon('/assets/blue_pin.png', size, offset);

I used to know how all this is supposed to work, but I have been away
from Rails for a while and the brain cells seem to have atrophied
rather, and perusing the docs has failed to re-invigorate them
sufficiently, so if someone could suggest what might be going on I
would be very grateful.

Colin

Walter Lee Davis

unread,
Mar 6, 2019, 9:27:57 AM3/6/19
to rubyonra...@googlegroups.com
The asset pipeline needs a JavaScript interpreter to work correctly on the server. I usually install whatever version of Node.js is available in a package manager. Alternatively, you can use the gem `therubyracer` to do the same thing. If you have that dependency filled, then I'm fresh out of ideas.

Walter

Walter Lee Davis

unread,
Mar 6, 2019, 9:47:59 AM3/6/19
to rubyonra...@googlegroups.com
Other than, of course, making sure you have sprockets and sprockets-rails listed in your Gemfile.lock. But you said you had run the rake assets:precompile task, or at least I assumed you had, if you said that your assets were precompiled already. Make sure that this works, on the server:

RAILS_ENV=production rake assets:precompile

If you do that, and you don't get any errors, you'll have two possible outcomes:

1. You'll get an output line for each asset (maybe two lines, with one being .gz of the previous).
2. You'll get nothing at all (this is okay and normal, it means that the assets did not change since the last invocation of the command).

If you started up a new server, it may have a different secret key or assets version, and that would change the fingerprints on all the precompiled assets. Running the precompile step again would fix that.

Walter

Colin Law

unread,
Mar 6, 2019, 10:17:04 AM3/6/19
to Ruby on Rails: Talk
On Wed, 6 Mar 2019 at 14:47, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
>
> > On Mar 6, 2019, at 9:27 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:
>>
> > The asset pipeline needs a JavaScript interpreter to work correctly on the server. I usually install whatever version of Node.js is available in a package manager. Alternatively, you can use the gem `therubyracer` to do the same thing. If you have that dependency filled, then I'm fresh out of ideas.

Thanks for the suggestions Walter
I have checked nodejs is installed (node -v shows 10.x)

>
> Make sure that this works, on the server:
>
> RAILS_ENV=production rake assets:precompile

Yes it does work, it outputs nothing, as expected. The code has been
checked out of the git repo, which doesn't include the public/assets
folder and the compiled assets are there ok.
I just realised that I was wrong about the css, that isn't found either.

One thing that confuses me, in the nginx log I see entries like
GET /assets/red_pin.png HTTP/1.1" 404 457 ...
Should the url not be the full name including the digest?
Even more confusingly I am informed that if I access the server from
another location then the images do appear, but I wonder whether that
one is seeing cached images. I haven't managed to capture the log
when that site issues a full refresh yet.

Colin

Walter Lee Davis

unread,
Mar 6, 2019, 10:30:00 AM3/6/19
to rubyonra...@googlegroups.com

> On Mar 6, 2019, at 10:16 AM, Colin Law <cla...@gmail.com> wrote:
>
> On Wed, 6 Mar 2019 at 14:47, Walter Lee Davis <wa...@wdstudio.com> wrote:
>>
>>
>>> On Mar 6, 2019, at 9:27 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:
>>>
>>> The asset pipeline needs a JavaScript interpreter to work correctly on the server. I usually install whatever version of Node.js is available in a package manager. Alternatively, you can use the gem `therubyracer` to do the same thing. If you have that dependency filled, then I'm fresh out of ideas.
>
> Thanks for the suggestions Walter
> I have checked nodejs is installed (node -v shows 10.x)
>
>>
>> Make sure that this works, on the server:
>>
>> RAILS_ENV=production rake assets:precompile
>
> Yes it does work, it outputs nothing, as expected. The code has been
> checked out of the git repo, which doesn't include the public/assets
> folder and the compiled assets are there ok.
> I just realised that I was wrong about the css, that isn't found either.
>
> One thing that confuses me, in the nginx log I see entries like
> GET /assets/red_pin.png HTTP/1.1" 404 457 ...
> Should the url not be the full name including the digest?

Yes, at the NGINX level, the request should be for the actual file path. In the Rails app, there would be a helper call like asset_url('red_pin.png'), which would generate the full path including the fingerprint.

> Even more confusingly I am informed that if I access the server from
> another location then the images do appear, but I wonder whether that
> one is seeing cached images. I haven't managed to capture the log
> when that site issues a full refresh yet.

Yes. If you want to bust the cache, there's an assets_version config attribute you can change. I think it's 1 by default, and you can increment it, and then precompile assets. Everything will change. But the question you have above would trump that, I think.

Walter

Walter Lee Davis

unread,
Mar 6, 2019, 11:15:59 AM3/6/19
to rubyonra...@googlegroups.com

> On Mar 6, 2019, at 10:29 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:
>
>>
>> On Mar 6, 2019, at 10:16 AM, Colin Law <cla...@gmail.com> wrote:
>>
>> On Wed, 6 Mar 2019 at 14:47, Walter Lee Davis <wa...@wdstudio.com> wrote:
>>>
>>>
>>>> On Mar 6, 2019, at 9:27 AM, Walter Lee Davis <wa...@wdstudio.com> wrote:
>>>>
>>>> The asset pipeline needs a JavaScript interpreter to work correctly on the server. I usually install whatever version of Node.js is available in a package manager. Alternatively, you can use the gem `therubyracer` to do the same thing. If you have that dependency filled, then I'm fresh out of ideas.
>>
>> Thanks for the suggestions Walter
>> I have checked nodejs is installed (node -v shows 10.x)
>>
>>>
>>> Make sure that this works, on the server:
>>>
>>> RAILS_ENV=production rake assets:precompile
>>
>> Yes it does work, it outputs nothing, as expected. The code has been
>> checked out of the git repo, which doesn't include the public/assets
>> folder and the compiled assets are there ok.
>> I just realised that I was wrong about the css, that isn't found either.
>>
>> One thing that confuses me, in the nginx log I see entries like
>> GET /assets/red_pin.png HTTP/1.1" 404 457 ...
>> Should the url not be the full name including the digest?
>
> Yes, at the NGINX level, the request should be for the actual file path. In the Rails app, there would be a helper call like asset_url('red_pin.png'), which would generate the full path including the fingerprint.

I just realized this answer wasn't entirely clear. When I said the "actual file path", I meant including the fingerprint.

Another idea: have a hunt in the rake -T output for assets things you can do. One of them is purge or flush or something like that. Really get in there and force it to re-do everything.

Walter

>
>> Even more confusingly I am informed that if I access the server from
>> another location then the images do appear, but I wonder whether that
>> one is seeing cached images. I haven't managed to capture the log
>> when that site issues a full refresh yet.
>
> Yes. If you want to bust the cache, there's an assets_version config attribute you can change. I think it's 1 by default, and you can increment it, and then precompile assets. Everything will change. But the question you have above would trump that, I think.
>
> Walter
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6127B556-0CB8-43CF-913D-A13BF4F60E26%40wdstudio.com.
> For more options, visit https://groups.google.com/d/optout.

Steve Alex

unread,
Mar 6, 2019, 11:27:44 AM3/6/19
to Ruby on Rails: Talk
I just went through a similar problem with displaying an image in a PDF generated from the gem prawn.

Worked find in development, but not production. prawn knows nothing about the asset pipeline or path helpers. I'm sure that the same is true with trying to get the path from java/coffee/script.

I even included the path helpers  `  include  ActionView::Helpers::AssetTagHelper` but that didn't work with asset_path or asset_url. It just didn't didn't work. There must be some hidden magic in the asset_path that sets the uuid between the image name and .type.

From the root url  you can get an image with server.com/assets/assets/my-logo-b70c4bbdfd69ea9db2785922c48b8ec1f03c5d8b6bf2be843ee570199cd51743.jpg, but you don't know the precompiled name. Even if you did, it would break in development.  Prawn or coffee script does not know about environments.

I finally found a solution, not fond of it,  but it works. Prawn has an image method that requires a path and options. I just fell back to the path based on rails.root

    image(Rails.root.join('app/assets/images/my_logo.jpg'),width:100 ) 

never tried to serve an image from coffee script and not sure the hack I found would work. You are basically ignoring the asset pipeline

Steve

Colin Law

unread,
Mar 6, 2019, 11:31:46 AM3/6/19
to Ruby on Rails: Talk
I don't understand how it ever worked at all, now that I understand a
bit more. I have a .js.coffee file in assets/javascripts that has in
it lines like
@marker_icon_red = new OpenLayers.Icon('/assets/red_pin.png', size, offset);
so there is nothing to convert the url to digest version. Yet this
has been working fine for several years. So somehow the url
/assets/red_pin.img has been satisfied.

I can only think that somehow copies of the original .png files
existed in public/assets, along with the digest versions. Perhaps
they were copied there on the server by someone years ago to make it
work and they have been there ever since. I think the proper solution
is to change the file to .js.coffee.erb and then I can use the helper
as described in section 2.3.3
https://guides.rubyonrails.org/asset_pipeline.html
Reply all
Reply to author
Forward
0 new messages