From 9cd2c7f52ffafc3fbec5df4c3cede0194a9b1795 Mon Sep 17 00:00:00 2001 From: Dev Date: Wed, 25 Nov 2026 01:02:53 +0100 Subject: [PATCH] Refresh existing shop bridge references without duplicating scripts --- website/ops/oscommerce/DuneAccount.php | 5 ++++- website/ops/oscommerce/test-shop-design-bootstrap.php | 7 +++++-- website/ops/test_native_shop_design.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/website/ops/oscommerce/DuneAccount.php b/website/ops/oscommerce/DuneAccount.php index c8622b14..3642d8fe 100644 --- a/website/ops/oscommerce/DuneAccount.php +++ b/website/ops/oscommerce/DuneAccount.php @@ -21,7 +21,10 @@ final class DuneAccount { $app->on(\yii\base\Application::EVENT_BEFORE_REQUEST,fn()=>self::handle($app)); $app->response->on(\yii\web\Response::EVENT_AFTER_PREPARE,static function($event):void { $response=$event->sender;$path=parse_url($_SERVER['REQUEST_URI']??'/',PHP_URL_PATH);$destination=$_SERVER['HTTP_SEC_FETCH_DEST']??null; - if(!is_string($path)||!str_starts_with($path,'/catalog/')||$response->statusCode<200||$response->statusCode>=300||$response->format!==\yii\web\Response::FORMAT_HTML||!is_string($response->content)||str_contains($response->content,'/oscommerce-inner.js')||strtolower($_SERVER['HTTP_X_REQUESTED_WITH']??'')==='xmlhttprequest'||($destination!==null&&!in_array($destination,['document','iframe'],true)))return; + if(!is_string($path)||!str_starts_with($path,'/catalog/')||$response->statusCode<200||$response->statusCode>=300||$response->format!==\yii\web\Response::FORMAT_HTML||!is_string($response->content)||strtolower($_SERVER['HTTP_X_REQUESTED_WITH']??'')==='xmlhttprequest'||($destination!==null&&!in_array($destination,['document','iframe'],true)))return; + if(str_contains($response->content,'/oscommerce-inner.js')){ + $response->content=preg_replace('~(/oscommerce-inner\.js)(?:\?[^"\']*)?(["\'])~','$1?v=realm-design-1$2',$response->content);return; + } $script=''; // Native catalogue documents can omit their outer body tag. $response->content=stripos($response->content,'~i',$script.'',$response->content,1):$response->content.$script; diff --git a/website/ops/oscommerce/test-shop-design-bootstrap.php b/website/ops/oscommerce/test-shop-design-bootstrap.php index a675f64e..567e2b72 100644 --- a/website/ops/oscommerce/test-shop-design-bootstrap.php +++ b/website/ops/oscommerce/test-shop-design-bootstrap.php @@ -21,7 +21,9 @@ namespace { ['/catalog/product?products_id=1',200,'html','Product',true], ['/catalog/shopping-cart',200,'html','Cart',true], ['/catalog/checkout',200,'html','Checkout',true], - ['/catalog/dune-shop',200,'html','',false], + ['/catalog/dune-shop',200,'html','','cache',[],''], + ['/catalog/dune-shop',200,'html','','cache',[],''], + ['/catalog/dune-shop',200,'html','',false], ['/catalog/dune-shop/sales-feed',200,'json','{"items":[]}',false], ['/catalog/account',303,'html','Redirect',false], ['/catalog/product',500,'html','Error',false], @@ -37,7 +39,8 @@ namespace { unset($_SERVER['HTTP_X_REQUESTED_WITH'],$_SERVER['HTTP_SEC_FETCH_DEST']);foreach($case[5]??[] as $header=>$value)$_SERVER[$header]=$value; $_SERVER['REQUEST_URI']=$path;$r=$app->response;$r->statusCode=$status;$r->format=$format;$r->content=$content; ($r->handler)((object)['sender'=>$r]); - if($decorate){if(substr_count($r->content,'src="/oscommerce-inner.js?v=realm-design-1"')!==1||!str_contains($r->content,' defer>'))throw new \RuntimeException('Missing single presentation bridge: '.$path);} + if($decorate==='cache'){if($r->content!==$case[6])throw new \RuntimeException('Existing bridge was duplicated or cache version retained');} + elseif($decorate){if(substr_count($r->content,'src="/oscommerce-inner.js?v=realm-design-1"')!==1||!str_contains($r->content,' defer>'))throw new \RuntimeException('Missing single presentation bridge: '.$path);} elseif($r->content!==$content)throw new \RuntimeException('Non-page response changed: '.$path); if($r->statusCode!==$status||$r->format!==$format)throw new \RuntimeException('Response contract changed'); } diff --git a/website/ops/test_native_shop_design.py b/website/ops/test_native_shop_design.py index c27eba83..9198a103 100644 --- a/website/ops/test_native_shop_design.py +++ b/website/ops/test_native_shop_design.py @@ -5,6 +5,6 @@ class NativeShopDesign(unittest.TestCase): def test_response_hook_preserves_non_page_responses(self): root=pathlib.Path(__file__).resolve().parent/'oscommerce' result=subprocess.run(['php',str(root/'test-shop-design-bootstrap.php'),str(root/'DuneAccount.php')],capture_output=True,text=True,check=True) - self.assertIn('14 native response cases passed',result.stdout) + self.assertIn('16 native response cases passed',result.stdout) if __name__=='__main__':unittest.main()