0

I am taking an HTML snapshot of a node in Drupal 8 every day at 4 am. Every night a Cron job kicks off this process. I have a service that handles the entire process. Everything works for now, but I am worried that the referenced CSS and JavaScript files will break due to the filenames changed by the aggregation process.

How does Drupal 8 decide the name of the aggregated files? How can I reliably point the references to the right filenames?

1 Answer 1

0

The name of the aggregated files is the hash calculated from CssCollectionOptimizer::generateHash().

protected function generateHash(array $css_group) {
  $css_data = [];
  foreach ($css_group['items'] as $css_file) {
    $css_data[] = $css_file['data'];
  }
  return hash('sha256', serialize($css_data));
}

The method is called by CssCollectionOptimizer::optimize().

Without knowing to which group a resource is part of, and what other resources are part of that group, you cannot know the name of the file where the resources are aggregated.

There should not be any need to do that, as Drupal core takes care of removing any reference to aggregate files that don't exist anymore, and invalidate all the necessary cache bins.
The markup produced by Drupal core should never contain a reference to an aggregate file that doesn't exist anymore. If that happens, it's either a bug in Drupal core or in a third-party module.

2
  • Thanks for the response. Your point about Drupal core not changing reference is true for new snapshots. Snapshots that are taken on 11/01/2020, but all the previous snapshots that wore take on let's say 01/01/2020 will not be referencing the same hash for the CSS file as the contents of the files could have changed since then. My issue is how can I reliably reference the same CSS hash.
    – Jay Chand
    Commented Nov 25, 2020 at 14:46
  • You cannot, especially when referring to past aggregate files. It would be already complicated for current aggregate files; imagine when you need to consider changes to used resources.
    – avpaderno
    Commented Nov 26, 2020 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.