Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Refer asset path inside of javascript file #163

Closed
rafaeldev opened this issue Jul 18, 2023 · 2 comments
Closed

Refer asset path inside of javascript file #163

rafaeldev opened this issue Jul 18, 2023 · 2 comments

Comments

@rafaeldev
Copy link

I have a servicer worker where I have a list of files to be cached. Previous, I ran using .erb extension to get that, like this:

const CACHE_VERSION = 'v2';
const CACHE_NAME = CACHE_VERSION + ':sw-cache-';

function onInstall(event) {
  console.log('[Serviceworker]', "Installing!", event);

  event.waitUntil(
    caches.open(CACHE_NAME).then(function prefill(cache) {
      return cache.addAll([
        '<%= asset_path "application.js" %>',

        '<%= asset_path "application.css" %>',

        '/offline.html',
      ]);
    })
  );
}

Now I decided to move sw.js to esbuild build flow but I don't know how can I asset application.js file link there.

Thanks!

@risen
Copy link

risen commented Jun 18, 2024

I also would like a way to get the asset_path in javascript.

For example, when I want to use customize the icon library for shoelace.style, I can do this:

registerIconLibrary("default", { resolver: (name) => `https://cdn.jsdelivr.net/npm/[email protected]/icons/${name}.svg` })

… but I can't refer to local assets and do something like:

registerIconLibrary("default", { resolver: (name) => asset_path(`icons/${name}.svg`) })

@seanharmer
Copy link

I've just managed to get something like this working. You can use the new RAILS_ASSET_URL helper from propshaft. To use it do something like this:

const particleTexturePath = RAILS_ASSET_URL("/particles/9.png")
console.log(particleTexturePath)

which outputs the full finger-printed path: /assets/particles/9-61a18f3e.png

If the path fails to resolve, there will be a log message in the rails server logs.

@rails rails locked and limited conversation to collaborators Mar 9, 2025
@dhh dhh converted this issue into discussion #215 Mar 9, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants