Merge pull request 'Refresh cached native shop scripts without requiring a hard reload' (#578) from dev/MC:design/shop-cache-refresh-20261125 into main
All checks were successful
DuneRealms Web CI / web (push) Successful in 3m1s
All checks were successful
DuneRealms Web CI / web (push) Successful in 3m1s
This commit is contained in:
commit
0e24d8ec5e
3 changed files with 10 additions and 4 deletions
|
|
@ -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='<script src="/oscommerce-inner.js?v=realm-design-1" defer></script>';
|
||||
// Native catalogue documents can omit their outer body tag.
|
||||
$response->content=stripos($response->content,'</body')!==false?preg_replace('~</body\s*>~i',$script.'</body>',$response->content,1):$response->content.$script;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,9 @@ namespace {
|
|||
['/catalog/product?products_id=1',200,'html','<html><body>Product</body></html>',true],
|
||||
['/catalog/shopping-cart',200,'html','<html><body>Cart</body></html>',true],
|
||||
['/catalog/checkout',200,'html','<HTML><BODY>Checkout</BODY></HTML>',true],
|
||||
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js?v=existing"></script></body>',false],
|
||||
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js?v=existing"></script></body>','cache',[],'<body><script src="/oscommerce-inner.js?v=realm-design-1"></script></body>'],
|
||||
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js"></script></body>','cache',[],'<body><script src="/oscommerce-inner.js?v=realm-design-1"></script></body>'],
|
||||
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js?v=realm-design-1"></script></body>',false],
|
||||
['/catalog/dune-shop/sales-feed',200,'json','{"items":[]}',false],
|
||||
['/catalog/account',303,'html','<html><body>Redirect</body></html>',false],
|
||||
['/catalog/product',500,'html','<html><body>Error</body></html>',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></script>'))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></script>'))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');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff