Skip to content

Commit 5cc26e8

Browse files
committed
feat: add viewport configuration and update prerender service to use it
1 parent 6e0199a commit 5cc26e8

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Web pages that are rendered on the browser side and made without SSR are not seen by bots. Because bots can't render pages. This microservice allows all bots to see the already rendered page.
44

55
Features:
6+
67
- Allows bots to see content on web pages without SSR
78
- Works on any framework, ex: JQuery, Angular 1, StencilJS and others
89
- Works on any technologies, ex: WebComponents, Microfrontends, Dynamic Content and others
@@ -59,6 +60,9 @@ The server will return a response with the specified status code.
5960
- `-e BOTVIEW_WAIT_UNTIL=networkidle` - [When to consider waiting succeeds. Given an array of event strings, waiting is considered to be successful after all events have been fired](https://playwright.dev/docs/api/class-page#page-goto),
6061
default networkidle.
6162

63+
- `-e BOTVIEW_VIEWPORT="360x640"` - Set the screen resolution (viewport) for the browser context, format is WIDTHxHEIGHT (e.g. `1280x720`). Default is `360x640`.
64+
Example: `-e BOTVIEW_VIEWPORT="1280x720"`
65+
6266
- `-e BOTVIEW_BLOCK_IMAGES=true` - Block loading of images to improve performance, default true.
6367

6468
- `-e BOTVIEW_BLOCK_STYLESHEETS=true` - Block loading of stylesheets to improve performance, default true.

src/LogLevels.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
21
export enum LogLevels {
32
FATAL = 60,
43
ERROR = 50,
54
WARN = 40,
65
INFO = 30,
76
DEBUG = 20,
8-
TRACE = 10
7+
TRACE = 10,
98
}

src/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ process.env.BOTVIEW_BLOCK_FONTS ||= "true"; // true to block fonts
1212
process.env.BOTVIEW_BLOCK_MEDIA ||= "true"; // true to block media
1313
process.env.BOTVIEW_BLOCK_URLS ||= "https://an.yandex.ru, https://mc.yandex.ru"; // comma or space separated list of urls to block (URL-encoding is optional)
1414
process.env.BOTVIEW_BLOCK_URLS_REGEX ||= ""; // comma or space separated list of regexes to block (URL-encoding is optional)
15+
process.env.BOTVIEW_VIEWPORT ||= "360x640"; // screen viewport, format: WIDTHxHEIGHT (e.g. 360x640)
1516
process.env.BOTVIEW_LOG_LEVEL ||= LogLevels.INFO.toString(); // log level: trace, debug, info, warn, error, fatal
1617

1718
function getLogLevelChecked(logLevel: string): number {
@@ -29,6 +30,12 @@ function getLogLevelChecked(logLevel: string): number {
2930
throw new Error(`Unknown BOTVIEW_LOG_LEVEL: ${logLevel}`);
3031
}
3132

33+
function parseViewport(str: string): { width: number; height: number } {
34+
const match = /^([0-9]+)x([0-9]+)$/i.exec(str);
35+
if (!match) return { width: 360, height: 640 };
36+
return { width: Number(match[1]), height: Number(match[2]) };
37+
}
38+
3239
export const config = {
3340
basicAuth: process.env.BOTVIEW_BASIC_AUTHS,
3441
basicAuthParsed: process.env.BOTVIEW_BASIC_AUTHS
@@ -55,4 +62,5 @@ export const config = {
5562
.filter(Boolean)
5663
: [],
5764
logLevel: getLogLevelChecked(process.env.BOTVIEW_LOG_LEVEL),
65+
viewport: parseViewport(process.env.BOTVIEW_VIEWPORT),
5866
};

src/services/prerender.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { JsonLogger } from "./json-logger.service";
2-
import {LogLevels} from "../LogLevels";
2+
import { LogLevels } from "../LogLevels";
33
import { config } from "../config";
44
import { Injectable } from "@nestjs/common";
55
import {
66
chromium,
7-
devices,
87
Browser,
98
Page,
109
ConsoleMessage,
@@ -29,7 +28,7 @@ export class PrerenderService {
2928

3029
const authHeaders = this.setAuth(url);
3130
const context = await browser.newContext({
32-
...devices["Galaxy S8"],
31+
viewport: config.viewport,
3332
...authHeaders,
3433
});
3534

@@ -93,7 +92,11 @@ export class PrerenderService {
9392
): Pick<BrowserContextOptions, "httpCredentials"> {
9493
if (config.basicAuthParsed.length > 0) {
9594
// eslint-disable-next-line prettier/prettier
96-
for (const [authUrl, username, password] of config.basicAuthParsed) {
95+
for (const [
96+
authUrl,
97+
username,
98+
password,
99+
] of config.basicAuthParsed) {
97100
if (url.startsWith(authUrl)) {
98101
return {
99102
httpCredentials: {

0 commit comments

Comments
 (0)