jekyll-assets icon indicating copy to clipboard operation
jekyll-assets copied to clipboard

Absolute path to assets

Open beckerei opened this issue 6 years ago • 12 comments

  • [x] I tried updating to the latest version.
    • [x] I can't, there is an issue.
  • [ ] I Am on Windows
    • [ ] Ubuntu Bash on Windows
    • [ ] Fedora Bash on Windows
    • [ ] Other Bash on Windows
  • [ ] I Am on Linux
    • [ ] Ubuntu
    • [ ] Fedora
  • [x] I Am on macOS
  • [x] I'm on Docker
    • [x] I understand Docker may be unsupported.

We use jekyll 3.8.1 and jekyll-assets 3.0.11 (before the upgrade 2.4.0)

Description

We have a multilanguage site based on jekyll-multiple-languages-plugin, and server the default language under / while an english version is served under /en/.

Assets are excluded from translations and compiled to /assets/ We previously used asset_path which always gave the correct path to the assets. Using the new asset the asset url is now relative to the language path e.g. /en/assets/...

<link rel="stylesheet" href="{% asset_path main %}" type="text/css">

was converted to (both versions below don't work and will return the prefixed path)

<link rel="stylesheet" href="{% asset main @path %}" type="text/css">
or
<link rel="stylesheet" href="{% asset 'main.css' @path %}" type="text/css">

Expected

We need a way to get the absolute path to the assets, or at least relative to the root.

beckerei avatar May 14 '18 09:05 beckerei

What is the output result?

envygeeks avatar May 14 '18 12:05 envygeeks

It's /en/assets/main-54ec998fc60245ee09c449e01a2549e752690ad7f1ded1c198dee85acb69b189.css while I expect it to be /assets/main-54ec998fc60245ee09c449e01a2549e752690ad7f1ded1c198dee85acb69b189.css.

beckerei avatar May 14 '18 14:05 beckerei

I'll consider fixing the problem, but this is their bug, not mine, they alter the base URL in a way that alters Jekyll's default behavior, this puts the problem square in their hands, and it should be their's to fix, not mine, it's bad software practice to expect me to fix bugs in another persons software.

envygeeks avatar May 14 '18 14:05 envygeeks

Hmm, I see that the bug is not within jekyll-assets, and I don't expect you to alter your software to be compatible with a (mostly unmaintained jekyll plugin) to fix this ;)

Since behaviour/api changed from 2.4.* to 3.* I hoped that I missed something in the documentation that can give me the absolute asset path.

Do you think of a workaround, as we unfortunately cannot get rid of the multilanguage plugin, but still want to have dependencies on the latest possible version?

beckerei avatar May 14 '18 14:05 beckerei

Previously in 2.x I think we worked around the issue by giving them a special configuration, but that was a bit hacky and I didn't necessarily like it so if I remember right, I removed it. I might add in a secondary method that allows you to achieve what you want, how I'll go about it I don't know, but you can always just remove it with:

{% asset asset.png | remove: site.baseurl %} (that might or might not work, I don't remember if builtin stuff accepts liquid variables. The problem comes in that we set the baseURL expecting it's like most setups, but they hack the baseURL to achieve what they want (not a big deal really but it does affect other plugins considerably in the way of optimization.)

envygeeks avatar May 16 '18 16:05 envygeeks

I have a different use case for needing an absolute path to an asset, namely the og:image tag (set via a meta tag) for Facebook preview must be an absolute path. The Facebook debugger returns an error with a relative path. Thanks for considering this.

wfried avatar Sep 11 '18 22:09 wfried

Is there any update on this ? I'm also interested about this

sylvainmetayer avatar Nov 30 '18 18:11 sylvainmetayer

~A quick workaround for this is to inline your CSS~ ~{% asset main.css @inline %}~ ~But be aware, your CSS will no longer be cached.~

This way is much better. Replace /pl with whatever is your secondary language.

<!-- {% asset main.css %} -->
<link rel="stylesheet" href="{{ assets['main.scss'].digest_path | remove: '/pl'}}">

Note that the asset must first be called in order for it to be generated. It is then commented out so it doesn't interfere.

bbbenji avatar Mar 21 '19 08:03 bbbenji

Having the same issue. It looks like there is code specifically for this issue, but it does not work for some reason:

https://github.com/envygeeks/jekyll-assets/blob/ce5c8cc43df9911e0788f06423a27375ee67c00f/lib/jekyll/assets/utils.rb#L233

touzoku avatar Jul 01 '19 09:07 touzoku

I have a different use case for needing an absolute path to an asset, namely the og:image tag (set via a meta tag) for Facebook preview must be an absolute path. The Facebook debugger returns an error with a relative path. Thanks for considering this.

If anyone finds this, I solved the above problem with:

<meta content="{{ site.url}}{% asset 'my-image.png' @path %}" property="og:image">

It would be great if something like the following worked...

<meta content="{% asset 'my-image.png' @path | absolute_url %}" property="og:image">

glenpike avatar Aug 03 '20 15:08 glenpike

I don't think I'll ever consider | absolute_url but I'd be happy to look into {% asset img.png @url %}

envygeeks avatar Aug 03 '20 15:08 envygeeks

I don't think I'll ever consider | absolute_url but I'd be happy to look into {% asset img.png @url %}

This looks like a cool solution - yes please if you're able!

Thanks for the lib too. Getting to grips with it.

glenpike avatar Aug 03 '20 15:08 glenpike