Skip to content

Commit

Permalink
Merge pull request #1 from aligent/feautre/change-prerender_io-to-pre…
Browse files Browse the repository at this point in the history
…render

Change prerender.io to prerender service
  • Loading branch information
aligent-lturner authored Jul 27, 2023
2 parents 959f78f + 6d38b9f commit c5b40d8
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Api/PrerenderClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

declare(strict_types=1);
namespace Aligent\PrerenderIo\Api;
namespace Aligent\Prerender\Api;

interface PrerenderClientInterface
{
Expand Down
25 changes: 21 additions & 4 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
);
}
}
30 changes: 20 additions & 10 deletions Model/Api/PrerenderClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -67,30 +67,40 @@ 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,
'urls' => $urls
]
);
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]
);
}
Expand Down
10 changes: 5 additions & 5 deletions Model/Indexer/Category/CategoryIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down
12 changes: 6 additions & 6 deletions Model/Indexer/Category/ProductIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion Model/Indexer/DataProvider/ProductCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions Model/Indexer/Product/ProductIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion Model/Url/GetUrlsForCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Model/Url/GetUrlsForProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# 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.

## Installation
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`
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "*"
Expand All @@ -17,7 +17,7 @@
"registration.php"
],
"psr-4": {
"Aligent\\PrerenderIo\\": ""
"Aligent\\Prerender\\": ""
}
}
}
14 changes: 10 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="system">
<group id="prerender_io" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="100">
<label>Prerender.io</label>
<group id="prerender" translate="label" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="100">
<label>Prerender service</label>
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Product recaching enabled?</label>
<comment>Send recache requests to Prerender.io on product changes</comment>
<comment>Send recache requests to Prerender service on product changes</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="token" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="service_url" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Service URL</label>
<depends>
<field id="enabled">1</field>
</depends>
</field>
<field id="token" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Token</label>
<depends>
<field id="enabled">1</field>
Expand Down
12 changes: 12 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<system>
<prerender>
<enabled>0</enabled>
<service_url><![CDATA[https://api.prerender.io/recache]]></service_url>
</prerender>
</system>
</default>
</config>
10 changes: 5 additions & 5 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Aligent\PrerenderIo\Api\PrerenderClientInterface"
type="Aligent\PrerenderIo\Model\Api\PrerenderClient"/>
<type name="Aligent\PrerenderIo\Model\Indexer\Product\ProductIndexer">
<preference for="Aligent\Prerender\Api\PrerenderClientInterface"
type="Aligent\Prerender\Model\Api\PrerenderClient"/>
<type name="Aligent\Prerender\Model\Indexer\Product\ProductIndexer">
<arguments>
<argument name="dimensionProvider" xsi:type="object" shared="false">
Magento\Store\Model\StoreDimensionProvider
</argument>
</arguments>
</type>
<type name="Aligent\PrerenderIo\Model\Indexer\Category\CategoryIndexer">
<type name="Aligent\Prerender\Model\Indexer\Category\CategoryIndexer">
<arguments>
<argument name="dimensionProvider" xsi:type="object" shared="false">
Magento\Store\Model\StoreDimensionProvider
</argument>
</arguments>
</type>
<type name="Aligent\PrerenderIo\Model\Indexer\Category\ProductIndexer">
<type name="Aligent\Prerender\Model\Indexer\Category\ProductIndexer">
<arguments>
<argument name="dimensionProvider" xsi:type="object" shared="false">
Magento\Store\Model\StoreDimensionProvider
Expand Down
12 changes: 6 additions & 6 deletions etc/indexer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="prerender_io_product" view_id="prerender_io_product" class="Aligent\PrerenderIo\Model\Indexer\Product\ProductIndexer">
<title translate="true">Prerender.io Product Recaching</title>
<indexer id="prerender_product" view_id="prerender_product" class="Aligent\Prerender\Model\Indexer\Product\ProductIndexer">
<title translate="true">Prerender service Product Recaching</title>
<description translate="true">Recaches product urls when products are updated</description>
</indexer>
<indexer id="prerender_io_category" view_id="prerender_io_category" class="Aligent\PrerenderIo\Model\Indexer\Category\CategoryIndexer">
<title translate="true">Prerender.io Category Recaching</title>
<indexer id="prerender_category" view_id="prerender_category" class="Aligent\Prerender\Model\Indexer\Category\CategoryIndexer">
<title translate="true">Prerender service Category Recaching</title>
<description translate="true">Recaches category urls when categories are updated</description>
</indexer>
<indexer id="prerender_io_category_product" view_id="prerender_io_category_product" class="Aligent\PrerenderIo\Model\Indexer\Category\ProductIndexer">
<title translate="true">Prerender.io Category Product Recaching</title>
<indexer id="prerender_category_product" view_id="prerender_category_product" class="Aligent\Prerender\Model\Indexer\Category\ProductIndexer">
<title translate="true">Prerender service Category Product Recaching</title>
<description translate="true">Recaches category urls when products are updated</description>
</indexer>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Aligent_PrerenderIo"/>
<module name="Aligent_Prerender"/>
</config>
Loading

0 comments on commit c5b40d8

Please sign in to comment.