Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Static (and Versioned) Assets in the Client #65

Open
michaelahlers opened this issue Apr 29, 2017 · 1 comment
Open

Using Static (and Versioned) Assets in the Client #65

michaelahlers opened this issue Apr 29, 2017 · 1 comment

Comments

@michaelahlers
Copy link

michaelahlers commented Apr 29, 2017

Is it possible to include static assets (namely images), served from Play and referenced from Scala.js code? Tricky because—when, for example, using sbt-digest to fingerprint assets—the final asset name isn't known and reverse Assets routes and AssetsFinder can't be used. Soliciting ideas or guidance here since my searches haven't turned up anything.

@michaelahlers
Copy link
Author

michaelahlers commented Apr 29, 2017

Applying broadly to any Play project (not specific to Scala.js), the only way I've found to provide versioned assets to stylesheets and inline images involves adding a plugin which rewrites paths.

  1. Server and client projects as illustrated by this project.
  2. Play's configured with:
    1. The usual sbt-less and sbt-digest plugins
    2. Versioned asset routes.
    3. Also, now, with sbt-simple-url-update.
  3. Stylesheet, owned by Play, defining classes with the content property referencing a static image in the server project à la:
    img.my-logo {
      /* Not widely compatible; most common case is likely to provide a background. */
      content: url("path/to/my-logo.png");
    }
  4. Client project assigning that class to img tags à la (VDOM in this case):
    // Again, probably an uncommon usage.
    img(className := "my-logo")
  5. When staging, sbt-simple-url-update will notice the url data type in CSS file (which is itself fingerprinted) and rewrite it with the fingerprinted asset yielding:
    img.my-logo {
      content: url("path/to/ba35502b60181156f7e9d6c7341b64f8-my-logo.png");
    }
  6. Image appears as expected in the browser—even in production mode.

This isn't great. Have I missed anything obvious? I can't find any documentation offering a better way to reference versioned assets from CSS in a Play project.

And, while functional, this removes images from the client project (which runs afoul of locality, unless, of course, you consider it a handy way to share assets across multiple client apps. hosted by the same Play instance). I'd rather the client app. project own all its static assets (and kept close to the components referencing or styled with them).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant