Skip to content

Commit b665790

Browse files
author
Illia Sakovich
committed
wip
1 parent 262125a commit b665790

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

config/dynamic-renderer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'enabled' => env('DYNAMIC_RENDERER_ENABLED', true),
66

77
'rendering_criteria' => [
8+
\Coderello\DynamicRenderer\RenderingCriteria\IsEnabled::class,
89
// \Coderello\DynamicRenderer\RenderingCriteria\IsProduction::class,
910
\Coderello\DynamicRenderer\RenderingCriteria\IsNotRenderer::class,
1011
\Coderello\DynamicRenderer\RenderingCriteria\IsCrawler::class,

docs/prerender-local.md renamed to docs/prerender.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Prerender Local
2+
title: Prerender
33
section: Renderers
44
weight: 200
55
featherIcon: activity
66
---
77

8-
[Prerender](https://prerender.io/) provides an open-source server which could be run locally for **free**.
8+
[Prerender](https://github.com/prerender/prerender) is a rendering solution by [Prerender.IO](https://prerender.io/).
99

1010
## Installing and configuring the renderer
1111

@@ -24,6 +24,6 @@ The default port is `3000`. Change the server port with `export PORT=3005` if ne
2424
All you have to do on the application side is to put these variables into the `.env` file.
2525

2626
```
27-
DYNAMIC_RENDERER_DRIVER=prerender_local
28-
PRERENDER_LOCAL_SERVICE_URL=http://localhost:3000
27+
DYNAMIC_RENDERER_DRIVER=prerender
28+
PRERENDER_URL=http://localhost:3000
2929
```

docs/rendertron.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Rendertron
3+
section: Renderers
4+
weight: 205
5+
featherIcon: activity
6+
---
7+
8+
[Rendertron](https://github.com/GoogleChrome/rendertron) is a Headless Chrome rendering solution by Google.
9+
10+
## Installing and configuring the renderer
11+
12+
```bash
13+
npm install rendertron
14+
```
15+
16+
```bash
17+
./node_modules/rendertron/bin/rendertron
18+
```
19+
20+
The default port is `3000`.
21+
22+
## Configuring your application
23+
24+
All you have to do on the application side is to put these variables into the `.env` file.
25+
26+
```
27+
DYNAMIC_RENDERER_DRIVER=rendertron
28+
RENDERTRON_URL=http://localhost:3000
29+
```

src/Middleware/DynamicRendererMiddleware.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,36 @@
88
use Coderello\DynamicRenderer\Support\RenderingCriteriaChecker;
99
use Illuminate\Contracts\Container\Container;
1010
use Illuminate\Http\Request;
11+
use Illuminate\Http\Response;
1112

1213
class DynamicRendererMiddleware
1314
{
1415
public function handle(Request $request, Closure $next)
16+
{
17+
if ($this->shouldRenderDynamically($request)) {
18+
return $this->renderDynamically($request);
19+
}
20+
21+
return $next($request);
22+
}
23+
24+
protected function shouldRenderDynamically(Request $request): bool
1525
{
1626
/** @var RenderingCriteriaChecker $renderingCriteriaChecker */
1727
$renderingCriteriaChecker = app(RenderingCriteriaChecker::class);
1828

19-
if ($renderingCriteriaChecker->matchesAll($request)) {
20-
$url = $request->fullUrl();
29+
return $renderingCriteriaChecker->matchesAll($request);
30+
}
2131

22-
$renderingResult = DynamicRenderer::render($url);
32+
protected function renderDynamically(Request $request): Response
33+
{
34+
$url = $request->fullUrl();
2335

24-
return response(
25-
$renderingResult->getContent(),
26-
$renderingResult->getStatusCode()
27-
);
28-
}
36+
$renderingResult = DynamicRenderer::render($url);
2937

30-
return $next($request);
38+
return response(
39+
$renderingResult->getContent(),
40+
$renderingResult->getStatusCode()
41+
);
3142
}
3243
}

src/RenderingCriteria/IsEnabled.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Coderello\DynamicRenderer\RenderingCriteria;
4+
5+
use Illuminate\Contracts\Config\Repository as Config;
6+
use Illuminate\Contracts\Foundation\Application;
7+
use Illuminate\Http\Request;
8+
9+
class IsEnabled implements RenderingCriterion
10+
{
11+
/**
12+
* @var Config
13+
*/
14+
private $config;
15+
16+
public function __construct(Config $config)
17+
{
18+
$this->config = $config;
19+
}
20+
21+
public function matches(Request $request): bool
22+
{
23+
return (bool) $this->config->get('dynamic-renderer.enabled', true);
24+
}
25+
}

0 commit comments

Comments
 (0)