setono / prerender-bundle
一个集成了'prerender'服务的Symfony扩展包,即rendertron或prerender.io
v1.0.0-alpha.2
2023-04-20 13:04 UTC
Requires
- php: >=7.4
- league/uri: ^6.5
- league/uri-components: ^2.4
- setono/bot-detection-bundle: ^1.9
- symfony/config: ^5.4 || ^6.0
- symfony/dependency-injection: ^5.4 || ^6.0
- symfony/event-dispatcher: ^5.4 || ^6.0
- symfony/http-client: ^5.4 || ^6.0
- symfony/http-client-contracts: ^2.5 || ^3.0
- symfony/http-foundation: ^5.4 || ^6.0
- symfony/http-kernel: ^5.4 || ^6.0
- webmozart/assert: ^1.10
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.3
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.6
- psalm/plugin-phpunit: ^0.18
- psalm/plugin-symfony: ^5.0
- setono/code-quality-pack: ^2.4
- symfony/process: ^5.4 || ^6.0
This package is auto-updated.
Last update: 2024-09-15 12:45:33 UTC
README
使用此扩展包来(预)渲染请求或URL。如果您使用JavaScript渲染页面并希望向Googlebot或其他不太喜欢JavaScript的客户端显示该页面的HTML输出,这很有用。
有关此主题的Google官方帮助页面:实现动态渲染。
安装
要安装此扩展包,只需运行
composer require setono/prerender-bundle
这将安装扩展包,如果使用Symfony Flex,则启用它。如果您不使用Flex,请手动将其添加到bundles.php
中。
配置
如果您想使用prerender.io的开源版本,扩展包已为您预先配置。然后只需运行该服务即可。但是,您可以轻松地这样进行配置
# config/packages/setono_prerender.yaml setono_prerender: adapter: prerender: 'http://your-prerender-url.com'
如果您想创建自己的prerender服务,只需实现PrerendererInterface
并使用setono_prerender.prerenderer
标签您的服务。然后在扩展包的配置中这样做
# config/packages/setono_prerender.yaml setono_prerender: prerenderer: 'the-service-id-of-your-prerenderer-service'
使用
这是一个非常基础的示例,展示了一个产品控制器,我们希望渲染index操作
<?php declare(strict_types=1); namespace App\Controller; use Setono\PrerenderBundle\Prerenderer\PrerendererInterface; use Symfony\Component\HttpFoundation\Response; final class ProductController { public function index(PrerendererInterface $prerenderer): Response { if($this->isBot()) { // return the rendered HTML if the client is a bot return new Response($prerenderer->renderMainRequest()); } // render the response the normal way if the client is NOT a bot return new Response('...'); } /** * Method that returns true if the client is a bot */ private function isBot(): bool { // ... } }