Change record status: 
Project: 
Introduced in branch: 
8.0.x
Introduced in version: 
8.0.3
Description: 

Drupal sites can be made available via:

  1. both HTTP and HTTPS
  2. both HTTP/1 and HTTP/2
  3. multiple domains

When either of several of those are true, we can easily end up with the wrong absolute file URL (an URL to either a file that is shipped with a module or theme, or an uploaded file) for the current site due to caching:

  1. a HTTP URL while accessing the site via HTTPS: mixed content warning
  2. a HTTP/1 (= often HTTP) URL while accessing the site via HTTP/2 (= always HTTPS): mixed content warning
  3. a foo.com URL while accessing the site via bar.com (when a site is available via both foo.com and bar.com)

So: the problem is that historically, all of those URLs have been absolute. We have three possible solutions:

  • (A) Making them protocol-relative (//www.drupal.org/llama.txt) instead of absolute solves problems 1 and 2, but not 3. It also reduces the number of bytes to send (and read as a developer) only mildly. Plus, some browsers are known to not handle protocol-relative URLs correctly.
  • (B) Making them root-relative (/llama.txt) instead of absolute solves all 3 problems and reduces the number of bytes to send (and read as a developer) fairly significantly. Plus, all browsers have handled these kinds of URLs correctly for a long time (if not always).
  • (C) Keeping them absolute, but associating the url.site cache context to indicate that this bit of rendered HTML actually varies by the protocol plus hostname via which the site is being accessed. The downside compared to option B is that it requires a different variation for every way the site can be accessed. In other words: it has a lower cache hit ratio, and higher complexity.

Option A does not help us.

Option B is the most appealing option. This is the option Drupal core took. This can be done by wrapping every file_create_url() call in a file_url_transform_relative() call. It's possible to do this in the vast majority of cases.

Option C is the fallback option, in case option B is not possible. This is only true for a handful of situations. Those will be fixed in #2646744: \Drupal\Core\Url does not accept root-relative (file) URLs, making it impossible to let LinkGenerator create root-relative file URL links.

Impacts: 
Site builders, administrators, editors
Module developers
Themers