From 4072493ba264ca813c78201d3c98ae9038f667e4 Mon Sep 17 00:00:00 2001 From: Benjamin Wittwer Date: Mon, 15 Apr 2024 14:12:16 +0200 Subject: [PATCH 1/4] Improve script on page performance --- .../{base.html.twig => layout/meta.html.twig} | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) rename src/Resources/views/storefront/{base.html.twig => layout/meta.html.twig} (85%) diff --git a/src/Resources/views/storefront/base.html.twig b/src/Resources/views/storefront/layout/meta.html.twig similarity index 85% rename from src/Resources/views/storefront/base.html.twig rename to src/Resources/views/storefront/layout/meta.html.twig index aee39e3d5..206b3868e 100644 --- a/src/Resources/views/storefront/base.html.twig +++ b/src/Resources/views/storefront/layout/meta.html.twig @@ -1,7 +1,6 @@ -{% sw_extends '@Storefront/storefront/base.html.twig' %} - -{% block base_main %} +{% sw_extends '@Storefront/storefront/layout/meta.html.twig' %} +{% block layout_head_javascript_hmr_mode %} {# pages where apple pay requires js with their mollie_applepaydirect_restrictions names as values #} {% set onlyShowHere = { @@ -12,7 +11,6 @@ 'frontend.detail.page' : 'pdp' } %} - {% set currentRoute = app.request.attributes.get('_route') %} {% set includeJsInHeader = false %} @@ -36,8 +34,8 @@ {% if mollie_javascript_use_shopware != '1' and includeJsInHeader == true %} - + {% endif %} {{ parent() }} -{% endblock %} \ No newline at end of file +{% endblock %} From c3a34c07e0c6107a19dceb9d7517ace2ae8e8044 Mon Sep 17 00:00:00 2001 From: Benjamin Wittwer Date: Tue, 16 Apr 2024 09:43:15 +0200 Subject: [PATCH 2/4] Exclude mollie script, if it already exists in the header --- src/Resources/config/services/subscriber.xml | 1 + src/Resources/views/storefront/layout/meta.html.twig | 8 ++++++++ src/Subscriber/StorefrontBuildSubscriber.php | 11 ++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Resources/config/services/subscriber.xml b/src/Resources/config/services/subscriber.xml index ba25c3490..d13a6baa1 100644 --- a/src/Resources/config/services/subscriber.xml +++ b/src/Resources/config/services/subscriber.xml @@ -42,6 +42,7 @@ + %kernel.shopware_version% diff --git a/src/Resources/views/storefront/layout/meta.html.twig b/src/Resources/views/storefront/layout/meta.html.twig index 206b3868e..41063cc5b 100644 --- a/src/Resources/views/storefront/layout/meta.html.twig +++ b/src/Resources/views/storefront/layout/meta.html.twig @@ -29,6 +29,14 @@ {% endif %} {% endif %} + {% if includeJsInHeader == true and mollie_javascript_check_duplicate == true %} + {% for script in theme_scripts() %} + {% if script == 'js/mollie-payments/mollie-payments.js' %} + {% set includeJsInHeader = false %} + {% endif %} + {% endfor %} + {% endif %} + diff --git a/src/Subscriber/StorefrontBuildSubscriber.php b/src/Subscriber/StorefrontBuildSubscriber.php index 07bbbe9fc..f8192cba6 100644 --- a/src/Subscriber/StorefrontBuildSubscriber.php +++ b/src/Subscriber/StorefrontBuildSubscriber.php @@ -2,6 +2,7 @@ namespace Kiener\MolliePayments\Subscriber; +use Kiener\MolliePayments\Compatibility\VersionCompare; use Kiener\MolliePayments\Service\SettingsService; use Shopware\Storefront\Event\StorefrontRenderEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -13,13 +14,20 @@ class StorefrontBuildSubscriber implements EventSubscriberInterface */ private $settingsService; + /** + * @var VersionCompare + */ + private $versionCompare; + /** * @param SettingsService $settingsService + * @param string $shopwareVersion */ - public function __construct(SettingsService $settingsService) + public function __construct(SettingsService $settingsService, string $shopwareVersion) { $this->settingsService = $settingsService; + $this->versionCompare = new VersionCompare($shopwareVersion); } /** @@ -43,5 +51,6 @@ public function onStorefrontRender(StorefrontRenderEvent $event): void $useJsValue = (int)$settings->isUseShopwareJavascript(); $event->setParameter('mollie_javascript_use_shopware', $useJsValue); + $event->setParameter('mollie_javascript_check_duplicate', $this->versionCompare->gte('6.6')); } } From 77580f098ee8c09c0c58015c3243b344b223d471 Mon Sep 17 00:00:00 2001 From: Benjamin Wittwer Date: Tue, 16 Apr 2024 12:48:06 +0200 Subject: [PATCH 3/4] Move mollie script & Fix missing 6.4 block --- src/Resources/views/mollie/head.html.twig | 47 +++++++++++++++++++ src/Resources/views/storefront/base.html.twig | 9 ++++ .../views/storefront/layout/meta.html.twig | 44 +---------------- 3 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 src/Resources/views/mollie/head.html.twig create mode 100644 src/Resources/views/storefront/base.html.twig diff --git a/src/Resources/views/mollie/head.html.twig b/src/Resources/views/mollie/head.html.twig new file mode 100644 index 000000000..4d3168eee --- /dev/null +++ b/src/Resources/views/mollie/head.html.twig @@ -0,0 +1,47 @@ +{% block mollie_head_scripts %} + + {# pages where apple pay requires js with their mollie_applepaydirect_restrictions names as values #} + {% set onlyShowHere = { + 'frontend.checkout.confirm.page' : '', + 'frontend.checkout.cart.page' : 'cart', + 'frontend.navigation.page' : 'plp', + 'frontend.account.edit-order.page' : '', + 'frontend.detail.page' : 'pdp' + } %} + + + {% set currentRoute = app.request.attributes.get('_route') %} + {% set includeJsInHeader = false %} + + {# js always required on this pages #} + {% if currentRoute == 'frontend.checkout.cart.page' or currentRoute == 'frontend.checkout.confirm.page' or currentRoute == 'frontend.account.edit-order.page' %} + {% set includeJsInHeader = true %} + {% endif %} + + {# requirement check for apple pay direct #} + {% if mollie_applepaydirect_enabled == true or mollie_applepay_enabled == true %} + {% if currentRoute in onlyShowHere|keys and onlyShowHere[currentRoute] not in mollie_applepaydirect_restrictions %} + {% set includeJsInHeader = true %} + {% endif %} + {% if 'offcanvas' not in mollie_applepaydirect_restrictions %} + {% set includeJsInHeader = true %} + {% endif %} + {% endif %} + + {% if includeJsInHeader == true and mollie_javascript_check_duplicate === true %} + {% for script in theme_scripts() %} + {% if script == 'js/mollie-payments/mollie-payments.js' %} + {% set includeJsInHeader = false %} + {% endif %} + {% endfor %} + {% endif %} + + + + {% if mollie_javascript_use_shopware != '1' and includeJsInHeader == true %} + + {% endif %} + +{% endblock %} diff --git a/src/Resources/views/storefront/base.html.twig b/src/Resources/views/storefront/base.html.twig new file mode 100644 index 000000000..68c9e7aa0 --- /dev/null +++ b/src/Resources/views/storefront/base.html.twig @@ -0,0 +1,9 @@ +{% sw_extends '@Storefront/storefront/base.html.twig' %} + +{% block base_script_hmr_mode %} + + {{ parent() }} + + {% sw_include '@Shopware/mollie/head.html.twig' %} + +{% endblock %} diff --git a/src/Resources/views/storefront/layout/meta.html.twig b/src/Resources/views/storefront/layout/meta.html.twig index 41063cc5b..ec8ce87c5 100644 --- a/src/Resources/views/storefront/layout/meta.html.twig +++ b/src/Resources/views/storefront/layout/meta.html.twig @@ -2,48 +2,8 @@ {% block layout_head_javascript_hmr_mode %} - {# pages where apple pay requires js with their mollie_applepaydirect_restrictions names as values #} - {% set onlyShowHere = { - 'frontend.checkout.confirm.page' : '', - 'frontend.checkout.cart.page' : 'cart', - 'frontend.navigation.page' : 'plp', - 'frontend.account.edit-order.page' : '', - 'frontend.detail.page' : 'pdp' - } %} - - {% set currentRoute = app.request.attributes.get('_route') %} - {% set includeJsInHeader = false %} - - {# js always required on this pages #} - {% if currentRoute == 'frontend.checkout.cart.page' or currentRoute == 'frontend.checkout.confirm.page' or currentRoute == 'frontend.account.edit-order.page' %} - {% set includeJsInHeader = true %} - {% endif %} - - {# requirement check for apple pay direct #} - {% if mollie_applepaydirect_enabled == true or mollie_applepay_enabled == true %} - {% if currentRoute in onlyShowHere|keys and onlyShowHere[currentRoute] not in mollie_applepaydirect_restrictions %} - {% set includeJsInHeader = true %} - {% endif %} - {% if 'offcanvas' not in mollie_applepaydirect_restrictions %} - {% set includeJsInHeader = true %} - {% endif %} - {% endif %} - - {% if includeJsInHeader == true and mollie_javascript_check_duplicate == true %} - {% for script in theme_scripts() %} - {% if script == 'js/mollie-payments/mollie-payments.js' %} - {% set includeJsInHeader = false %} - {% endif %} - {% endfor %} - {% endif %} - - + {{ parent() }} - {% if mollie_javascript_use_shopware != '1' and includeJsInHeader == true %} - - {% endif %} + {% sw_include '@Shopware/mollie/head.html.twig' %} - {{ parent() }} {% endblock %} From c69edb87031c842db0ffb7ee2e46c0fac9fd6117 Mon Sep 17 00:00:00 2001 From: Benjamin Wittwer Date: Wed, 24 Apr 2024 09:34:26 +0200 Subject: [PATCH 4/4] Final fixes --- src/Resources/config/services/subscriber.xml | 1 + src/Resources/views/mollie/head.html.twig | 11 +---------- src/Subscriber/StorefrontBuildSubscriber.php | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Resources/config/services/subscriber.xml b/src/Resources/config/services/subscriber.xml index d13a6baa1..7f635b9a6 100644 --- a/src/Resources/config/services/subscriber.xml +++ b/src/Resources/config/services/subscriber.xml @@ -42,6 +42,7 @@ + %kernel.shopware_version% diff --git a/src/Resources/views/mollie/head.html.twig b/src/Resources/views/mollie/head.html.twig index 4d3168eee..28d382a6c 100644 --- a/src/Resources/views/mollie/head.html.twig +++ b/src/Resources/views/mollie/head.html.twig @@ -9,7 +9,6 @@ 'frontend.detail.page' : 'pdp' } %} - {% set currentRoute = app.request.attributes.get('_route') %} {% set includeJsInHeader = false %} @@ -28,19 +27,11 @@ {% endif %} {% endif %} - {% if includeJsInHeader == true and mollie_javascript_check_duplicate === true %} - {% for script in theme_scripts() %} - {% if script == 'js/mollie-payments/mollie-payments.js' %} - {% set includeJsInHeader = false %} - {% endif %} - {% endfor %} - {% endif %} - - {% if mollie_javascript_use_shopware != '1' and includeJsInHeader == true %} + {% if mollie_javascript_already_exists == false and mollie_javascript_use_shopware != '1' and includeJsInHeader == true %} {% endif %} diff --git a/src/Subscriber/StorefrontBuildSubscriber.php b/src/Subscriber/StorefrontBuildSubscriber.php index f8192cba6..2b5ea22fd 100644 --- a/src/Subscriber/StorefrontBuildSubscriber.php +++ b/src/Subscriber/StorefrontBuildSubscriber.php @@ -5,6 +5,7 @@ use Kiener\MolliePayments\Compatibility\VersionCompare; use Kiener\MolliePayments\Service\SettingsService; use Shopware\Storefront\Event\StorefrontRenderEvent; +use Shopware\Storefront\Theme\StorefrontPluginRegistryInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class StorefrontBuildSubscriber implements EventSubscriberInterface @@ -14,6 +15,11 @@ class StorefrontBuildSubscriber implements EventSubscriberInterface */ private $settingsService; + /** + * @var StorefrontPluginRegistryInterface + */ + private $pluginRegistry; + /** * @var VersionCompare */ @@ -24,9 +30,10 @@ class StorefrontBuildSubscriber implements EventSubscriberInterface * @param SettingsService $settingsService * @param string $shopwareVersion */ - public function __construct(SettingsService $settingsService, string $shopwareVersion) + public function __construct(SettingsService $settingsService, StorefrontPluginRegistryInterface $pluginRegistry, string $shopwareVersion) { $this->settingsService = $settingsService; + $this->pluginRegistry = $pluginRegistry; $this->versionCompare = new VersionCompare($shopwareVersion); } @@ -51,6 +58,12 @@ public function onStorefrontRender(StorefrontRenderEvent $event): void $useJsValue = (int)$settings->isUseShopwareJavascript(); $event->setParameter('mollie_javascript_use_shopware', $useJsValue); - $event->setParameter('mollie_javascript_check_duplicate', $this->versionCompare->gte('6.6')); + + $mollieJavascriptAlreadyExists = false; + if($this->versionCompare->gte('6.6')) { + $molliePayments = $this->pluginRegistry->getConfigurations()->getByTechnicalName('MolliePayments'); + $mollieJavascriptAlreadyExists = $molliePayments && ($molliePayments->getScriptFiles()->count() > 0); + } + $event->setParameter('mollie_javascript_already_exists', $mollieJavascriptAlreadyExists); } }