Problem/Motivation:

Since ~DrupalCon Nashville, there seems to be strong concensus around a reorganization of the source tree to better support composer and automatic updates. Most pertinent to this issue, the reorganization includes moving nearly everything out of the docroot: a "bootloader" script inside the docroot would select and include an "active" copy of the site's source tree that is located above/outside the docroot, allowing an automatic updater facility to operate on another "inactive" copy of the site's source tree and make that copy consistent with a new target version over possibly many cron or batch executions.

However, this poses a problem for non-php files that ship with modules and are expected to be served as static assets over http, especially images or other media that may not be declared in any existing .yml file or included on a page via existing hooks like hook_page_attachments().

To solve this problem, core needs to know which files that came with a module or theme must be made available to be served directly over http. With this knowledge it becomes possible to either copy/symlink them into the docroot as part of module installation, or serve them through php. This issue only seeks formalize and specifically define a module structure and any associated APIs for static assets. Once that's done, serving out those files from modules that have actually moved outside the docroot is best left to another issue.

It's important to note that the automatic updates initiative does not include updating contrib as an initial goal. However, it does include entire sites as a final goal, and to do that the answer to the serving static assets problem must be phased in now for contrib to begin adapting to it.

There's also probably an argument to be made for better DX if all a module's static files are in a predictable, standardized location within the module.

Proposed resolution:

This proposal captures the points raised in a slack conversation in #autoupdates between @pwolanin, @webflo, @mbaynton and @xjm on 7/30/18 with some additional specifics from @mbaynton.

  • Just use special directories at the root of modules to inform core which files are to be treated as static, serveable assets. Any files these directories contain would be treated by core that way.
  • The directories css and js would be regarded as containers of static files by default, because many modules already follow this convention.
  • The directory assets would be regarded as a container of static files by default, as an easy, convention over configuration way for modules to ship images and other files.
  • To streamline adaptation of existing modules, additional directories containing only static assets could be declared by the module in its info.yml file

Module authors could expect the following of static asset directories:

  • The directories must be made completely free of php files.
  • The directories may be relocated by core once the module is installed, so if for some reason
    the module wanted to make a static file public *and* also be able to read that file with php,
    it would need to locate the file using (A new API? A module:// stream wrapper?)
  • Within the directories, core would arrange for the files to be served in response to http requests to modules/[module-name]/[directory-name]/[file-name]. Core would not rearrange or alter files or subdirectories relative to each other.

Remaining Tasks:

  • Review & revise the proposed resolution.
  • Implement API or stream wrapper for static asset I/O access.
  • Develop an automated check to see if a module is likely to be not compliant, including verification that default and declared static asset dirs do not contain php, and scan for likely public files outside appropriate directories.

User interface changes:

None.

API changes:

New optional key in module info.yml files called assets_dirs containing an array of strings
listing directories within the module that should be treated as static, serveable asset files.

New API, stream wrapper, or both (TBD) to allow modules access to their own static asset directories after core potentially relocates them.

Data model changes:

None.

Comments

mbaynton created an issue. See original summary.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mbaynton’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

geerlingguy’s picture

Issue tags: +kubernetes

Adding tag, as this could also enable easier deployments into multi-container environments.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

aaronmchale’s picture

Here's an immediate thought, is core going to start actively enforcing this in code, for example if you define a library in your module, its files must sit under one of these defined directories otherwise an error/exception will throw?

Another thought (probably for further down the line), can we use the recommended htaccess/Nginx configuration to serve these static files which are outside of the docroot and only then fall-back to serving them over the PHP process or using symlinks? In theory we can, and in theory that would be better for performance than serving them over the PHP process,. and wouldn't result in a sea of symlinks everywhere.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

moshe weitzman’s picture

I wonder if the drupal composer scaffold could copy asset files for us.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xmacinfo’s picture

Can we consider using the same theme mechanism to load module asset files through a file similar to modulename.libraries.yml? For example through a modulename.assets.yml?

That “assets” file (e. g. modulename.assets.yml) would define files to be served as static assets and:
- the core could rewrite the URL of those files (e.g. /core/assets/modulename/filename)
- or the files could be served from the module perspective (e.g. /modulename/filename)

As for support for CSS/JS, I would prefer using regular libraries files (e. g. code>modulename.libraries.yml

nod_’s picture

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

nicxvan’s picture

xjm credited pwolanin.

xjm credited webflo.

xjm’s picture

Adding credits from original discussion.

nicxvan’s picture

I'm gong to keep this issue even though it is slightly newer since it is more active.

@phenaproxima has some valuable insight in #2863438-2: Allow modules to declare the list of files it ships with that should be publicly accessible there is also discussion about steam wrappers.

I have closed the other as a duplicate and addressed credit there as necessary.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.