How to load absolute CSS and JS assets

Solutions

These are aggregated assets, so you need to find out how they get added there in the first place. Then you can alter them most likely via hook_page_attachments_alter. Similar to Make canonical URL in document HEAD absolute.

Problematic about this approach is that these assets should are aggregated with that cryptic hash in their name per environment. So you need to switch off your custom module everywhere else except on the live instance.

Even though you don't want to use a contrib module I will recommend the CDN module anyways. It does what you want, but if you don't want to use it you can still look at how they do it.

They define a lot of services, but the most interesting one is this one:

cdn.file_url_generator:
    class: Drupal\cdn\File\FileUrlGenerator
    arguments: ['@app.root', '@file_system', '@stream_wrapper_manager', '@request_stack', '@private_key', '@cdn.settings']

The class FileUrlGenerator then does checks for files to determine if they should be rewritten and rewrites them.

Tags: Drupal 8 / Theming / Cdn

Similar questions

Link an image and add assets
I'm new to drupal, so this might be simple even if I can't find the solution. I have a custom module back_module with folders : From my templates/block-back.html.twig First issue: I want to print an image located in images/back.png I can't find the right url for the image <img src ="???" alt='backButton'/> I already find solutions to retrieve...
My theme CSS uses relative paths, but Advagg or another module seems to be rewriting them to absolute paths. Core aggregation uses relative paths
I have a module that uses hook_url_outbound_alter() to make URLs on some pages absolute by modifying the base URL: This seems like it could be a candidate for causing the issue, but the problem persists even when the changes to the options array in this function are commented out. I'm not sure how else to debug this? I suspect it is not an issue wi...
Create drupal content programmatically with custom entity ( assets )
I am using asset module : https://www.drupal.org/project/asset I creates a new entity type = asset. I get a form where I can add ( document, image, video..). How can I, from a custom module add a content of asset ? I think that I cannot do it the way we creat nodes ... like : Please help thank you
Loading of assets via Libraries API
I'm writing a new module which has a dependency on an external library. In Drupal 7 I would use hook_libraries_info giving the name of the library the required assets and Drupal would search the valid locations, libraries directories, to load these assets when required. Drupal 8 introduces *.libraries.yml and after reading https://www.drupal.org/no...
Get path to theme assets in Twig template
I have a question about getting a path to an image in a Twig template. The image is not assigned to a field or something. Just a static image which is stored in "MYTHEME/image/icon/my-icon.png". In Drupal 7 I get the path in my node.template with the following code: How does it work with in Drupal 8? I tried to pass a variable in template_preproces...
How do I include JavaScript with assets (libraries.yml)?
This is my mytheme.libraries.yml file: This is my js/effect.js file: I created jquery.js in core folder of my site root. Even if I remove the dependency line from my libraries.yml and remove jquery syntax from my effect.js, I receive nothing on my site.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.