Improve template link merging to allow page to specify prior links.


When added support for merging in additional links from a page into a template, it used a simple merging algorithm in which new (non-duplicate) links were simply tacked onto the end of existing template links. Some pages may need to require an order; that is, it may require some links to come before pages that already exist in the template. So we need to find some algorithm that allows prerequisite order from a page to be maintained after template application. (In fact already hinted that the algorithm would need to be improved.)




Garret Wilson
May 30, 2020, 5:36 PM

Another observation is that normally prerequisite order is only important within links of the same time (e.g. within stylesheets, or within JavaScript links). The new algorithm maintains order across all links, which is probably fine too.

Garret Wilson
May 30, 2020, 5:23 PM

We should also remove duplicates as well across all the links.

Yet another improvement in the future would be to detect different link elements and rel link attributes, and allow duplicate links if the other metadata is distinct (e.g. two links to the same resource but with different relations).

Garret Wilson
May 29, 2020, 1:30 PM

Here are some examples of what we would want to produce using this algorithm. Below the template links are shown first, then the that of the page:

  • A, B, C and X, Y, Z: A, B, C, X, Y, Z

  • A, B, C and C, Y, Z: A, B, C, Y, Z

  • A, B, C and X, Y, B: A, X, Y, B, C

  • A, B, C and X, B, Z: A, X, B, C, Z

  • A, B, C and X, B, A, Y: X, B, A, C, Y

We should also remove duplicates as well across all the links.

Garret Wilson
May 28, 2020, 9:58 PM

There are several approaches here, probably none of them completely satisfactory. We could do the opposite of what we do now by simply skipping any template links if we knew that the page had those links. That would have the page sort of "pull out" identified links from the template and place them later. But what about the remaining links in the template? What if they required certain prerequisites as well?

Since the whole problem here is that some CSS files may require other files to come before them, we'll use a slightly more complicated algorithm that does its best to ensure that any CSS link has the existing links from that same file before it, with the page order taking priority if there is any conflict.

The algorithm will go something like this:

  1. Each template link will be used, in the order it appears, until a link is encountered that is also one of the links in the page the template is being applied to.

  2. At that point all the page links up to and including that link will be added in place of the template link.

  3. This process will continue back at the first step until no more template links are left.

  4. All remaining page links will be added.



Garret Wilson


Garret Wilson




Fix versions

Affects versions