FluidTYPO3 / vhs

TYPO3 extension VHS: Fluid ViewHelpers
https://fluidtypo3.org
Other
189 stars 228 forks source link

Assets used in cached and uncached templates are loaded twice #1804

Closed Idleworks closed 1 year ago

Idleworks commented 2 years ago

If you load an asset (e.g. accordion.js) via <v:asset:script name="accordion-js" ... > in two different fluid templates - one cached and the other uncached, the asset will be included twice.

AssetInclusion Middleware does collect assets already included (static::$cachedDependencies) but lacks to compare assets to be included for uncached termplates.

I fixed it for myself by overriding method AssertService::sortAssetsByDepenceny

protected function sortAssetsByDependency($assets)
{
    $assets = array_diff_key(parent::sortAssetsByDependency($assets), array_flip(static::$cachedDependencies));

    return $assets;
}

It removes all assets which are already included in cached templates.

NamelessCoder commented 1 year ago

Note that the asset's overwrite instruction must be false to give the result you need, and the default value is true. It has to be this way since overwriting a cached asset from an uncached context is not technically possible to do by removing the old asset - it must instead be done by loading the asset again under the assumption that whatever code the asset contains, is capable of overwriting any previous code assigned by an asset of the same name. So, only when the asset is told explicitly to not overwrite a previously loaded variant of the same name will the uncached context skip the asset.