mercredi 23 juin 2021

Serving image assets from S3 in Ember app

I have a Rails server setup so that one of the views loads up the Ember app from S3 - something like below in my application.html.erb:

  ...
  <body>
    <%= yield %>
    <%= ember_assets.script_tag_for('vendor') %>
    <%= ember_assets.script_tag_for('my-ember-frontend-app') %>

and this in my ApplicationController:

class ApplicationController < ActionController::Base
  helper_method def ember_assets
    @ember_assets ||= begin
      asset_loader = EmberAssets::FileLoader.new # responsible for loading JS from S3

      EmberAssets.new(request: request, asset_loader: asset_loader)
    end
  end
  ...

The Javascript loads fine because my EmberAssets Ruby class correctly points to and loads up the JS files from S3. However, I have the logo and a few other default assets stored in public/assets/images/... and I read from them in my Ember app like so:

<img src="/assets/images/logo.png" alt="logo">

The problem is that while the JS is loaded correctly, the image files are not loaded from S3.

the logo png request initiated by the built vendor.js fails with a 404

How do I make it so that my Ember app reads default assets from S3 as well? I checked S3 and the asset is in the correct folder as expected with fingerprint and all.

enter image description here

I'm using Rails 6 and Ember 3.25 and I have installed these ember-cli-deploy addons for building and deploying my Ember app.

    "ember-cli-deploy": "^1.0.2",
    "ember-cli-deploy-build": "^2.0.0",
    "ember-cli-deploy-json-config": "^1.0.1",
    "ember-cli-deploy-s3": "^3.0.0",

This is my build config. I tried to prepend my S3 bucket url but it didn't work:

module.exports = function (defaults) {
  let app = new EmberApp(defaults, {
    // Add options here

    fingerprint: {
      prepend: 'https://ember-s3-prod.s3.amazonaws.com/'
    }
  });
  ...

Thanks!




Aucun commentaire:

Enregistrer un commentaire