Skip to content

Commit

Permalink
Do not return assets that have already been rendered.
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcopley committed Oct 8, 2024
1 parent bc49fe8 commit 83d8011
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
17 changes: 16 additions & 1 deletion models/CBWIREController.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,22 @@ component singleton {

// Return assets from components
if ( local.httpRequestState.assets.count() ) {
local.componentsResult[ "assets" ] = local.httpRequestState.assets;
var alreadyLoadedAssets = payload.components.reduce( function( _agg, _comp ) {
if ( _comp.snapshot.memo.keyExists( "assets" ) ) {
_comp.snapshot.memo.assets.each( function( _asset ) {
_agg.append( _asset );
} );
}
return _agg;
}, [] );

local.componentsResult[ "assets" ] = local.httpRequestState.assets.filter( function( key, value ) {
return !arrayFindNoCase( alreadyLoadedAssets, key );
} );

if ( !local.componentsResult[ "assets" ].count() ) {
local.componentsResult[ "assets" ] = [];
}
}

// Set the response headers to prevent caching
Expand Down
4 changes: 2 additions & 2 deletions models/Component.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1135,8 +1135,8 @@ component output="true" {
} ).each( function( key, value, result ) {
// Extract the counter from the tag name
local.counter = key.replaceNoCase( "assets", "" );
// Create assets tag id based on compile time id and counter
local.assetsTagId = variables._compileTimeKey & "-" & local.counter;
// Create assets tag id based on hash of assets
local.assetsTagId = hash( value );
// Track the assets tag
variables._assets[ local.assetsTagId ] = value;
local.requestAssets = variables._CBWIREController.getRequestAssets();
Expand Down
18 changes: 18 additions & 0 deletions test-harness/tests/specs/CBWIRESpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,29 @@ component extends="coldbox.system.testing.BaseTestCase" {
updates = {}
);
var result = cbwireController.handleRequest( payload, event );
expect( isStruct( result.assets ) );
expect( result.assets.count() ).toBe( 1 );
var keys = result.assets.keyArray();
expect( result.assets[ keys.first() ] ).toInclude( "tailwind.min.css" );
} );

it( "it should NOT return assets if they were already rendered", function() {
var payload = incomingRequest(
memo = {
"name": "test.should_track_cbwire_assets_in_http_response",
"id": "Z1Ruz1tGMPXSfw7osBW2",
"children": [],
"assets": [ "FC8550AF548BA7D81CAA8C2333141FDA" ]
},
data = {},
calls = [],
updates = {}
);
var result = cbwireController.handleRequest( payload, event );
expect( isArray( result.assets ) ).toBeTrue();
expect( result.assets.len() ).toBe( 0 );
} );

it( "should support $refresh action", function() {
var payload = incomingRequest(
memo = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

<cbwire:assets>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
</cbwire:script>
</cbwire:assets>

0 comments on commit 83d8011

Please sign in to comment.