8000 This fix addresses a glaring issue where the concatenated JS · reactjs/react-php-v8js@d7a9c34 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit d7a9c34

Browse files
committed
This fix addresses a glaring issue where the concatenated JS
initialisation code is executed **each time** ReactJS::getMarkup() is called. I cannot see a good reason why this should be the case, and my thinking is that the concatenated code bundle should be executed once only - when ReactJS is instantiated. This way, ReactJS::getMarkup() is only responsible for executing the JS React.renderToString() method, rather than executing the whole vendor bundle first, and then calling .renderToString() - which is incredibly costly and slow!
1 parent 12477ff commit d7a9c34

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

ReactJS.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@
1616
class ReactJS {
1717

1818
private
19-
/**
20-
* Concatenated React.js + Application code + boilerplate
21-
* @var string
22-
*/
23-
$react,
24-
2519
/**
2620
* Name of the component to render
2721
* @var string
@@ -64,9 +58,11 @@ function __construct($libsrc, $appsrc) {
6458
// app's components
6559
$react[] = $appsrc;
6660
$react[] = ';';
67-
$this->react = implode(";\n", $react);
68-
61+
62+
$concatenated = implode(";\n", $react);
63+
6964
$this->v8 = new V8Js();
65+
$this->executeJS($concatenated);
7066
}
7167

7268
/**
@@ -103,28 +99,14 @@ function setErrorHandler($err) {
10399
* @return string HTML string
104100
*/
105101
function getMarkup() {
106-
try {
107-
$js = $this->react;
108-
$js.= sprintf(
109-
"print(React.renderToString(React.createElement(%s, %s)))",
110-
$this->component,
111-
$this->data);
112-
113-
ob_start();
114-
$this->v8->executeString($js);
115-
return ob_get_clean();
102+
$js = sprintf(
103+
"print(React.renderToString(React.createElement(%s, %s)))",
104+
$this->component,
105+
$this->data);
116106

117-
} catch (V8JsException $e) {
118-
if ($this->errorHandler) {
119-
call_user_func($this->errorHandler, $e);
120-
} else {
121-
// default error handler blows up bad
122-
echo "<pre>";
123-
echo $e->getMessage();
124-
echo "</pre>";
125-
die();
126-
}
127-
}
107+
ob_start();
108+
$this->executeJS($js);
109+
return ob_get_clean();
128110
}
129111

130112
/**
@@ -163,5 +145,28 @@ function getJS($where, $return_var = null) {
163145
$where
164146
);
165147
}
148+
149+
/**
150+
* Executes Javascript using V8JS, with primitive exception handling
151+
*
152+
* @param string $js JS code to be executed
153+
* @return mixed
154+
*/
155+
private function executeJS($js) {
156+
try {
157+
return $this->v8->executeString($js);
158+
} catch (V8JsException $e) {
159+
if ($this->errorHandler) {
160+
call_user_func($this->errorHandler, $e);
161+
} else {
162+
// default error handler blows up bad
163+
echo "<pre>";
164+
echo $e->getMessage();
165+
echo "</pre>";
166+
die();
167+
}
168+
}
169+
}
170+
166171
}
167172

0 commit comments

Comments
 (0)
0