setono/prerender-bundle

一个集成了'prerender'服务的Symfony扩展包,即rendertron或prerender.io

安装数: 5,175

依赖: 1

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 1

开放问题: 3

类型:symfony-bundle

v1.0.0-alpha.2 2023-04-20 13:04 UTC

README

Latest Version Software License Build Status

使用此扩展包来(预)渲染请求或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
    {
        // ...
    }
}