From 6d38b9f24a88f1e70531f2e36e8b3d0b34073739 Mon Sep 17 00:00:00 2001 From: "dominic.fernando" Date: Thu, 27 Jul 2023 10:14:20 +0930 Subject: [PATCH] Change prerender.io to prerender service --- Api/PrerenderClientInterface.php | 2 +- Helper/Config.php | 25 +++++++++++++--- Model/Api/PrerenderClient.php | 30 ++++++++++++------- Model/Indexer/Category/CategoryIndexer.php | 10 +++---- Model/Indexer/Category/ProductIndexer.php | 12 ++++---- .../DataProvider/ProductCategories.php | 2 +- Model/Indexer/Product/ProductIndexer.php | 10 +++---- Model/Url/GetUrlsForCategories.php | 2 +- Model/Url/GetUrlsForProducts.php | 2 +- README.md | 16 +++++----- composer.json | 6 ++-- etc/adminhtml/system.xml | 14 ++++++--- etc/config.xml | 12 ++++++++ etc/di.xml | 10 +++---- etc/indexer.xml | 12 ++++---- etc/module.xml | 2 +- etc/mview.xml | 6 ++-- registration.php | 2 +- 18 files changed, 110 insertions(+), 65 deletions(-) create mode 100755 etc/config.xml diff --git a/Api/PrerenderClientInterface.php b/Api/PrerenderClientInterface.php index 227cfe6..ae3b120 100644 --- a/Api/PrerenderClientInterface.php +++ b/Api/PrerenderClientInterface.php @@ -4,7 +4,7 @@ */ declare(strict_types=1); -namespace Aligent\PrerenderIo\Api; +namespace Aligent\Prerender\Api; interface PrerenderClientInterface { diff --git a/Helper/Config.php b/Helper/Config.php index b0c8826..e669132 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -4,15 +4,17 @@ */ declare(strict_types=1); -namespace Aligent\PrerenderIo\Helper; + +namespace Aligent\Prerender\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Store\Model\ScopeInterface; class Config { - private const XML_PATH_RECACHE_ENABLED = 'system/prerender_io/enabled'; - private const XML_PATH_PRERENDER_TOKEN = 'system/prerender_io/token'; + private const XML_PATH_RECACHE_ENABLED = 'system/prerender/enabled'; + private const XML_PATH_PRERENDER_TOKEN = 'system/prerender/token'; + private const XML_PATH_RECACHE_SERVICE_URL = 'system/prerender/service_url'; /** @var ScopeConfigInterface */ private ScopeConfigInterface $scopeConfig; @@ -42,7 +44,7 @@ public function isRecacheEnabled(?int $storeId = null): bool } /** - * Return configured Prerender.io token for API calls + * Return configured Prerender service token for API calls * * @param int|null $storeId * @return string|null @@ -55,4 +57,19 @@ public function getToken(?int $storeId = null): ?string $storeId ); } + + /** + * Get prerender service url + * + * @param int|null $storeId + * @return string|null + */ + public function getPrerenderServiceUrl(?int $storeId = null): ?string + { + return $this->scopeConfig->getValue( + self::XML_PATH_RECACHE_SERVICE_URL, + ScopeInterface::SCOPE_STORE, + $storeId + ); + } } diff --git a/Model/Api/PrerenderClient.php b/Model/Api/PrerenderClient.php index bad0d94..fc96674 100644 --- a/Model/Api/PrerenderClient.php +++ b/Model/Api/PrerenderClient.php @@ -4,10 +4,11 @@ */ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Api; -use Aligent\PrerenderIo\Api\PrerenderClientInterface; -use Aligent\PrerenderIo\Helper\Config; +namespace Aligent\Prerender\Model\Api; + +use Aligent\Prerender\Api\PrerenderClientInterface; +use Aligent\Prerender\Helper\Config; use Magento\Framework\HTTP\ClientInterface; use Magento\Framework\Serialize\SerializerInterface; use Psr\Log\LoggerInterface; @@ -16,7 +17,6 @@ class PrerenderClient implements PrerenderClientInterface { private const MAX_URLS = 1000; - private const PRERENDER_RECACHE_URL = 'https://api.prerender.io/recache'; /** @var Config */ private Config $prerenderConfigHelper; @@ -48,7 +48,7 @@ public function __construct( } /** - * Call Prerender.io API to recache list of URLs + * Call Prerender service API to recache list of URLs * * @param array $urls * @param int $storeId @@ -67,19 +67,29 @@ public function recacheUrls(array $urls, int $storeId): void $batches = array_chunk($urls, self::MAX_URLS); foreach ($batches as $batch) { - $this->sendRequest($batch, $token); + $this->sendRequest($batch, $token, $storeId); } } /** - * Sends a JSON POST request to Prerender.io + * Sends a JSON POST request to Prerender service * * @param array $urls * @param string $token + * @param int|null $storeId * @return void */ - private function sendRequest(array $urls, string $token): void + private function sendRequest(array $urls, string $token, ?int $storeId = null): void { + $prerenderServiceUrl = $this->prerenderConfigHelper->getPrerenderServiceUrl($storeId); + + if (empty($prerenderServiceUrl)) { + $this->logger->error( + __('ERROR: prerender url not found. Store ID: %1', $storeId) + ); + return; + } + $payload = $this->jsonSerializer->serialize( [ 'prerenderToken' => $token, @@ -87,10 +97,10 @@ private function sendRequest(array $urls, string $token): void ] ); try { - $this->client->post(self::PRERENDER_RECACHE_URL, $payload); + $this->client->post($prerenderServiceUrl, $payload); } catch (\Exception $e) { $this->logger->error( - __('Error sending payload %1 to prerender.io', $payload), + __('Error sending payload %1 to Prerender service. Store ID: %2', $payload, $storeId), ['exception' => $e] ); } diff --git a/Model/Indexer/Category/CategoryIndexer.php b/Model/Indexer/Category/CategoryIndexer.php index 1634db7..ed04b08 100644 --- a/Model/Indexer/Category/CategoryIndexer.php +++ b/Model/Indexer/Category/CategoryIndexer.php @@ -5,11 +5,11 @@ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Indexer\Category; +namespace Aligent\Prerender\Model\Indexer\Category; -use Aligent\PrerenderIo\Api\PrerenderClientInterface; -use Aligent\PrerenderIo\Helper\Config; -use Aligent\PrerenderIo\Model\Url\GetUrlsForCategories; +use Aligent\Prerender\Api\PrerenderClientInterface; +use Aligent\Prerender\Helper\Config; +use Aligent\Prerender\Model\Url\GetUrlsForCategories; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; @@ -22,7 +22,7 @@ class CategoryIndexer implements IndexerActionInterface, MviewActionInterface, DimensionalIndexerInterface { - private const INDEXER_ID = 'prerender_io_category'; + private const INDEXER_ID = 'prerender_category'; private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/'; /** @var DimensionProviderInterface */ diff --git a/Model/Indexer/Category/ProductIndexer.php b/Model/Indexer/Category/ProductIndexer.php index 696dd99..f492bc3 100644 --- a/Model/Indexer/Category/ProductIndexer.php +++ b/Model/Indexer/Category/ProductIndexer.php @@ -5,12 +5,12 @@ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Indexer\Category; +namespace Aligent\Prerender\Model\Indexer\Category; -use Aligent\PrerenderIo\Api\PrerenderClientInterface; -use Aligent\PrerenderIo\Helper\Config; -use Aligent\PrerenderIo\Model\Indexer\DataProvider\ProductCategories; -use Aligent\PrerenderIo\Model\Url\GetUrlsForCategories; +use Aligent\Prerender\Api\PrerenderClientInterface; +use Aligent\Prerender\Helper\Config; +use Aligent\Prerender\Model\Indexer\DataProvider\ProductCategories; +use Aligent\Prerender\Model\Url\GetUrlsForCategories; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; @@ -23,7 +23,7 @@ class ProductIndexer implements IndexerActionInterface, MviewActionInterface, DimensionalIndexerInterface { - private const INDEXER_ID = 'prerender_io_category_product'; + private const INDEXER_ID = 'prerender_category_product'; private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/'; /** @var DimensionProviderInterface */ diff --git a/Model/Indexer/DataProvider/ProductCategories.php b/Model/Indexer/DataProvider/ProductCategories.php index 8b3acb1..98f708e 100644 --- a/Model/Indexer/DataProvider/ProductCategories.php +++ b/Model/Indexer/DataProvider/ProductCategories.php @@ -5,7 +5,7 @@ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Indexer\DataProvider; +namespace Aligent\Prerender\Model\Indexer\DataProvider; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; diff --git a/Model/Indexer/Product/ProductIndexer.php b/Model/Indexer/Product/ProductIndexer.php index 83f3cc7..209cbd7 100644 --- a/Model/Indexer/Product/ProductIndexer.php +++ b/Model/Indexer/Product/ProductIndexer.php @@ -5,11 +5,11 @@ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Indexer\Product; +namespace Aligent\Prerender\Model\Indexer\Product; -use Aligent\PrerenderIo\Api\PrerenderClientInterface; -use Aligent\PrerenderIo\Helper\Config; -use Aligent\PrerenderIo\Model\Url\GetUrlsForProducts; +use Aligent\Prerender\Api\PrerenderClientInterface; +use Aligent\Prerender\Helper\Config; +use Aligent\Prerender\Model\Url\GetUrlsForProducts; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; @@ -22,7 +22,7 @@ class ProductIndexer implements IndexerActionInterface, MviewActionInterface, DimensionalIndexerInterface { - private const INDEXER_ID = 'prerender_io_product'; + private const INDEXER_ID = 'prerender_product'; private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/'; /** @var DimensionProviderInterface */ diff --git a/Model/Url/GetUrlsForCategories.php b/Model/Url/GetUrlsForCategories.php index 50d9bfb..1a57a9b 100644 --- a/Model/Url/GetUrlsForCategories.php +++ b/Model/Url/GetUrlsForCategories.php @@ -4,7 +4,7 @@ */ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Url; +namespace Aligent\Prerender\Model\Url; use Magento\Catalog\Model\Category; use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; diff --git a/Model/Url/GetUrlsForProducts.php b/Model/Url/GetUrlsForProducts.php index bfec571..ef818d7 100644 --- a/Model/Url/GetUrlsForProducts.php +++ b/Model/Url/GetUrlsForProducts.php @@ -4,7 +4,7 @@ */ declare(strict_types=1); -namespace Aligent\PrerenderIo\Model\Url; +namespace Aligent\Prerender\Model\Url; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; diff --git a/README.md b/README.md index bb31471..0a0e8b4 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # magento2-prerender-io -Provides integration between Magento 2 and [Prerender.io](https://prerender.io), giving the ability for pages to be automatically recached when required. +Provides integration between Magento 2 and Prerender service, giving the ability for pages to be automatically recached when required. ## Overview This module provides new indexers: -- `prerender_io_product`, which will send URL recache requests for products to Prerender.io (in batches of up to 1000) when changes are made to products. -- `prerender_io_category`, which will send URL recache requests for categories to Prerender.io (in batches of up to 1000) when changes are made to categories. -- `prerender_io_category_product`, which will send URL recache requests for categories to Prerender.io (in batches of up to 1000) when changes are made to products. +- `prerender_product`, which will send URL recache requests for products to Prerender service (in batches of up to 1000) when changes are made to products. +- `prerender_category`, which will send URL recache requests for categories to Prerender service (in batches of up to 1000) when changes are made to categories. +- `prerender_category_product`, which will send URL recache requests for categories to Prerender service (in batches of up to 1000) when changes are made to products. These will ensure that the cached pages are kept up-to-date at all times. @@ -14,16 +14,16 @@ These will ensure that the cached pages are kept up-to-date at all times. To install via composer, simply run: ```bash -composer require aligent/magento2-prerender-io +composer require aligent/magento2-prerender ``` Then, ensure the module is installed and the indexers are set to `Schedule`: ```bash -bin/magento module:enable Aligent_PrerenderIo +bin/magento module:enable Aligent_Prerender bin/magento setup:upgrade -bin/magento indexer:set-mode schedule prerender_io_product prerender_io_category prerender_io_category_product +bin/magento indexer:set-mode schedule prerender_product prerender_category prerender_category_product ``` ## Configuration -The extension can be configured via `Stores -> Configuration -> System -> Prerender.io` +The extension can be configured via `Stores -> Configuration -> System -> Prerender Service` diff --git a/composer.json b/composer.json index 60e877b..9725fd5 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "aligent/magento2-prerender-io", - "description": "Prerender.io integration for Magento 2, providing recaching for product URLs", + "name": "aligent/magento2-prerender", + "description": "Prerender service integration for Magento 2, providing recaching for product URLs", "require": { "php": ">=7.4", "magento/framework": "*" @@ -17,7 +17,7 @@ "registration.php" ], "psr-4": { - "Aligent\\PrerenderIo\\": "" + "Aligent\\Prerender\\": "" } } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index e787bef..22976ad 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -7,14 +7,20 @@ xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
- - + + - Send recache requests to Prerender.io on product changes + Send recache requests to Prerender service on product changes Magento\Config\Model\Config\Source\Yesno - + + + + 1 + + + 1 diff --git a/etc/config.xml b/etc/config.xml new file mode 100755 index 0000000..1fcad62 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,12 @@ + + + + + + 0 + + + + + diff --git a/etc/di.xml b/etc/di.xml index 165a5a2..765547c 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -5,23 +5,23 @@ - - + + Magento\Store\Model\StoreDimensionProvider - + Magento\Store\Model\StoreDimensionProvider - + Magento\Store\Model\StoreDimensionProvider diff --git a/etc/indexer.xml b/etc/indexer.xml index 5c6c11a..32c19c1 100644 --- a/etc/indexer.xml +++ b/etc/indexer.xml @@ -4,16 +4,16 @@ --> - - Prerender.io Product Recaching + + Prerender service Product Recaching Recaches product urls when products are updated - - Prerender.io Category Recaching + + Prerender service Category Recaching Recaches category urls when categories are updated - - Prerender.io Category Product Recaching + + Prerender service Category Product Recaching Recaches category urls when products are updated diff --git a/etc/module.xml b/etc/module.xml index f038206..c3d7fcf 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -5,5 +5,5 @@ - + diff --git a/etc/mview.xml b/etc/mview.xml index 7e838c4..a632dbd 100644 --- a/etc/mview.xml +++ b/etc/mview.xml @@ -4,7 +4,7 @@ --> - +
@@ -20,7 +20,7 @@
- +
@@ -30,7 +30,7 @@
- +
diff --git a/registration.php b/registration.php index c6e3e5e..d6fd2e4 100644 --- a/registration.php +++ b/registration.php @@ -9,6 +9,6 @@ ComponentRegistrar::register( ComponentRegistrar::MODULE, - 'Aligent_PrerenderIo', + 'Aligent_Prerender', __DIR__ );