Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WebVPF committed Oct 18, 2023
1 parent 188fe9b commit 1486ac6
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php namespace WebVPF\TrafficAdvice;

use Backend\Models\UserRole;
use System\Classes\PluginBase;
use System\Classes\SettingsManager;

class Plugin extends PluginBase
{
public function pluginDetails(): array
{
return [
'name' => 'webvpf.trafficadvice::lang.plugin.name',
'description' => 'webvpf.trafficadvice::lang.plugin.description',
'author' => 'WebVPF',
'icon' => 'icon-traffic-light'
];
}

public function registerPermissions(): array
{
return []; // Remove this line to activate

return [
'webvpf.trafficadvice.some_permission' => [
'tab' => 'webvpf.trafficadvice::lang.plugin.name',
'label' => 'webvpf.trafficadvice::lang.permissions.some_permission',
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
],
];
}

public function registerSettings(): array
{
return [
'txt' => [
'label' => 'webvpf.trafficadvice::lang.plugin.name',
'description' => 'webvpf.trafficadvice::lang.settings.menu_desc',
'category' => SettingsManager::CATEGORY_CMS,
'icon' => 'icon-traffic-light',
'class' => \WebVPF\TrafficAdvice\Models\Settings::class,
'order' => 299,
'keywords' => 'seo',
'permissions' => ['webvpf.trafficadvice'],
],
];
}
}
40 changes: 40 additions & 0 deletions README-RU.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Traffic Advice

Плагин для Winter CMS, который добавляет на сайт страницу `/.well-known/traffic-advice` c JSON ответом содержащим инструкцию для предварительной загрузки сайта браузером Chrome при выдаче в поиске Google.

## Для чего

Если на ваш сайт есть поисковый трафик из Google, то включив в настройках "Сохранять неудачные запросы", вы увидете что на ваш сайт происходит большое количество запросов по адресу `https://example.com/.well-known/traffic-advice`

Это прокси-сервер Гугла делает запрос инструкции для предварительной загрузки сайта, чтобы перейдя из поиска у пользователя максимально быстро загружалась страница сайта.

Вместо инструкции прокси-сервер Гугла получает ошибку 404, а Winter CMS записывает лог ошибки (если включено в настройках).

<a href="https://user-images.githubusercontent.com/61043464/276033359-822a875d-1f3d-4126-ab78-d2b68156e673.jpg" target="_blank" alt="log errors"><img src="https://user-images.githubusercontent.com/61043464/276033285-20948c5a-2e65-424b-9a71-881392ecaf29.jpg"/>
</a> <a href="https://user-images.githubusercontent.com/61043464/276033421-5028c748-d54f-4a98-a6a2-4db952c28501.jpg" target="_blank" alt="log error"><img src="https://user-images.githubusercontent.com/61043464/276033317-fa67d326-d86a-4780-81e9-803e74ec2b28.jpg"/></a>

## Traffic control

You can use the traffic advice's `fraction` field to control how much traffic the Private Prefetch Proxy should let through. You can start with a small fraction such as 0.3 (that is, 30%), and gradually increase it to 1.0 (that is, 100%) by adding the following JSON to a `/.well-known/traffic-advice` file, which needs to be served with the `application/trafficadvice+json` MIME type:

```json
[{
"user_agent": "prefetch-proxy",
"fraction": 0.3
}]
```

The `fraction` field is a float between 0.0 (no prefetch at all) and 1.0 (100% of the prefetch requests get through).

It is also possible to disable this completely with the following configuration:

```json
[{
"user_agent": "prefetch-proxy",
"disallow": true
}]
```

---

Подробнее смотрите https://developer.chrome.com/blog/private-prefetch-proxy/#traffic
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Traffic Advice

A plugin for Winter CMS that adds to the site the page `/.well-known/traffic-advice` with a JSON response containing instructions for preloading the site in the Chrome browser when it appears in Google search.

## Installation Сomposer:

```bash
composer require webvpf/wn-trafficadvice-plugin
```

## For what

If your site has search traffic from Google, then by turning on “Save failed requests” in the settings, you will see that your site receives a large number of requests for the address `https://example.com/.well-known/traffic-advice`

This is a Google proxy server that requests instructions for preloading the site, so that when the user leaves the search, the site page loads as quickly as possible.

Instead of instructions, the Google proxy server receives a 404 error, and Winter CMS records an error log (if enabled in the settings).

<a href="https://user-images.githubusercontent.com/61043464/276033359-822a875d-1f3d-4126-ab78-d2b68156e673.jpg" target="_blank" alt="log errors"><img src="https://user-images.githubusercontent.com/61043464/276033285-20948c5a-2e65-424b-9a71-881392ecaf29.jpg"/>
</a> <a href="https://user-images.githubusercontent.com/61043464/276033421-5028c748-d54f-4a98-a6a2-4db952c28501.jpg" target="_blank" alt="log error"><img src="https://user-images.githubusercontent.com/61043464/276033317-fa67d326-d86a-4780-81e9-803e74ec2b28.jpg"/></a>

---

## Traffic control

You can use the traffic advice's `fraction` field to control how much traffic the Private Prefetch Proxy should let through. You can start with a small fraction such as 0.3 (that is, 30%), and gradually increase it to 1.0 (that is, 100%) by adding the following JSON to a `/.well-known/traffic-advice` file, which needs to be served with the `application/trafficadvice+json` MIME type:

```json
[{
"user_agent": "prefetch-proxy",
"fraction": 0.3
}]
```

The `fraction` field is a float between 0.0 (no prefetch at all) and 1.0 (100% of the prefetch requests get through).

It is also possible to disable this completely with the following configuration:

```json
[{
"user_agent": "prefetch-proxy",
"disallow": true
}]
```

---

See more details https://developer.chrome.com/blog/private-prefetch-proxy/#traffic
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "webvpf/wn-trafficadvice-plugin",
"type": "winter-plugin",
"description": "Plugin for managing the amount of preload traffic. Generates a JSON response to: /.well-known/traffic-advice",
"homepage": "https://github.com/WebVPF/wn-trafficadvice-plugin",
"keywords": ["winter cms", "wintercms", "wintercms-plugin"],
"license": "MIT",
"authors": [
{
"name": "WebVPF",
"role": "Author"
}
],
"support": {
"issues": "https://github.com/WebVPF/wn-trafficadvice-plugin/issues",
"source": "https://github.com/WebVPF/wn-trafficadvice-plugin"
}
}
17 changes: 17 additions & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'plugin' => [
'name' => 'TrafficAdvice',
'description' => 'Plugin for managing the amount of preload traffic. Generates a JSON response to: /.well-known/traffic-advice',
],
'settings' => [
'menu_desc' => 'Manage the volume of preload traffic.',
'title_label' => 'Settings TrafficAdvice',
'title_comment' => 'page with address /.well-known/traffic-advice',
'is_activated_label' => 'Active',
'is_activated_comment' => 'If disabled, <code style="color:#cf0070">fraction</code> will be replaced with <code style="color:#cf0070">disallow</code> with the value <code style="color:#cf0070">true</code>',
'fraction_label' => 'Meaning for fraction',
'fraction_comment' => 'The value can be a decimal number from 0 to 1 inclusive.',
],
];
20 changes: 20 additions & 0 deletions lang/ru/lang.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [
'plugin' => [
'name' => 'TrafficAdvice',
'description' => 'Плагин для управления объёмом трафика предварительной загрузки. Генерирует JSON-ответ по адресу: /.well-known/traffic-advice',
],
'settings' => [
'menu_desc' => 'Управление объёмом трафика предварительной загрузки.',
'title_label' => 'Настройка Traffic Advice',
'title_comment' => 'страница с адресом /.well-known/traffic-advice',
'is_activated_label' => 'Активно',
'is_activated_comment' => 'Если выключено, вместо <code style="color:#cf0070">fraction</code> будет указано <code style="color:#cf0070">disallow</code> со значением <code style="color:#cf0070">true</code>',
'fraction_label' => 'Значение fraction',
'fraction_comment' => 'Значение может быть десятичным числом от 0 до 1 включительно.',
],
// 'permissions' => [
// 'some_permission' => 'Some permission',
// ],
];
36 changes: 36 additions & 0 deletions models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php namespace WebVPF\TrafficAdvice\Models;

use Model;

class Settings extends Model
{
use \Winter\Storm\Database\Traits\Validation;

/**
* @var array Behaviors implemented by this model.
*/
public $implement = [\System\Behaviors\SettingsModel::class];

/**
* @var string Unique code
*/
public $settingsCode = 'webvpf_trafficadvice_settings';

/**
* @var mixed Settings form field definitions
*/
public $settingsFields = 'fields.yaml';

/**
* @var array Validation rules
*/
public $rules = [
'fraction' => 'required|numeric|min:0|max:1',
];

public function initSettingsData()
{
$this->is_activated = true;
$this->fraction = 1.0;
}
}
27 changes: 27 additions & 0 deletions models/settings/fields.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# ===================================
# Form Field Definitions
# ===================================

fields:

settings_title:
type: section
label: webvpf.trafficadvice::lang.settings.title_label
comment: webvpf.trafficadvice::lang.settings.title_comment

is_activated:
type: switch
label: webvpf.trafficadvice::lang.settings.is_activated_label
default: true
comment: webvpf.trafficadvice::lang.settings.is_activated_comment
commentHtml: true

fraction:
type: number
label: webvpf.trafficadvice::lang.settings.fraction_label
comment: webvpf.trafficadvice::lang.settings.fraction_comment
default: 1
step: 0.1
min: 0
max: 1
span: auto
11 changes: 11 additions & 0 deletions routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

Route::get('.well-known/traffic-advice', function () {
$is_activated = \WebVPF\TrafficAdvice\Models\Settings::get('is_activated');
$fraction = \WebVPF\TrafficAdvice\Models\Settings::get('fraction');

return Response::view('webvpf.trafficadvice::default', [
'is_activated' => $is_activated,
'fraction' => $fraction,
])->header('Content-Type', 'application/trafficadvice+json');
});
2 changes: 2 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'1.0.0':
- 'First version of TrafficAdvice'
8 changes: 8 additions & 0 deletions views/default.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"user_agent": "prefetch-proxy",
{% if is_activated %}
"fraction": {{ fraction|number_format(1, '.') }}
{% else %}
"disallow": true
{% endif %}
}]

0 comments on commit 1486ac6

Please sign in to comment.