This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
forked from linares/bigpipe
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexample.php
executable file
·139 lines (121 loc) · 4.53 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache");
header("Content-Type: text/html");
require_once('Browser.php');
require_once('h_bigpipe.inc');
require_once('h_pagelet.inc');
$use_padding = true;
if (isset($_REQUEST['disable_padding'])) {
$use_padding = false;
}
function test_delayed_rendering($msg)
{
global $use_padding;
// Simulate some long operation
usleep(100000); // 100 ms
$padding = '';
if ($use_padding) {
for ($i = 0; $i < 8192; $i++) { $padding .= ' '; }
}
return "$msg <!-- $padding -->\n";
}
function test_simple_replace($msg)
{
global $use_padding;
$padding = '';
if ($use_padding) {
for ($i = 0; $i < 8192; $i++) { $padding .= ' '; }
}
return "$msg <!-- $padding --><br>\n";
}
function get_google_analytics_script($code)
{
ob_start();
?>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<?= $code ?>']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
<?php
return ob_get_clean();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>BigPipe example</title>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="prototypepatch.js"></script>
<script type="text/javascript" src="bigpipe.js"></script>
</head>
<body>
<h1 id="header">BigPipe test.</h1>
<?php
if ($use_padding) {
?>
<p>This version uses padding to fill out browser caches so that the bigpipe delayed rendering effect can be seen easily. This causes problems with firebug, because the page content is big. Use <a href="example.php?disable_padding=1">this</a> link to disable the padding so you can use firebug more easily.
<?php
} else {
?>
<p>The padding has been disabled. The page load takes much longer because there's still some sleep() delays inside.</p>
<?php
}
?>
<!-- simulate that the page is much bigger than it is. Browsers have internal buffering which hides how bigpipe actual works.
This allows us to simulate real world effect with a big page. -->
<!-- <?php if ($use_padding) { for ($i = 0; $i < 128000; $i++) { echo ' '; } } ?> -->
<h2>Simple content replace</h2>
<?php
echo new Pagelet("content_replace", 'test_simple_replace', 10, array('Ok'));
?>
<h2>Test delayed rendering (50 times)</h2>
<?php
for ($i = 0; $i < 50; $i++) {
$pagelet = new Pagelet("counter$i", "test_delayed_rendering", 10, array($i));
$pagelet->use_span = true;
echo $pagelet;
}
$pagelet = new Pagelet("delayed_done", "test_simple_replace", 10, array('Ok'));
$pagelet->use_span = true;
echo $pagelet;
?>
<h2>Content replace with inline javascript</h2>
<?php
// Test with a pagelet which contains additional javascript payload
$pagelet = new Pagelet('inline_javascript_test');
$pagelet->add_content('<div id="javascript_inline_test">Be patient, this will be completed in the end after delayed rendering</div>');
$pagelet->add_javascript_code("$('javascript_inline_test').innerHTML = 'Ok';");
echo $pagelet;
?>
<h2>Content replace with external javascript file</h2>
<div id="external_js">Be patient, this will be completed in the end after delayed rendering</div>
<?php
$pagelet = new Pagelet('external_javascript_test');
$pagelet->add_javascript('test.js');
echo $pagelet;
?>
<h2>Content replace with external javascript and inline javascript</h2>
<div id="external_js2">be patient, this will be completed in the end after delayed rendering test</div>
<?php
$pagelet = new Pagelet('external_javascript_test2');
$pagelet->add_javascript('test2.js');
$pagelet->add_javascript_code("test2('external_js2', 'Ok');");
echo $pagelet;
// Inject google analytics
$pagelet = new Pagelet('google_analytics');
$pagelet->add_javascript_code(get_google_analytics_script('UA-18754626-1'));
echo $pagelet;
// Test with a pagelet which contains additional javascript payload
$pagelet = new Pagelet('final_ok');
$pagelet->add_javascript("test.js");
$pagelet->add_javascript_code("$('header').innerHTML = 'All done';", 12);
echo $pagelet;
echo "</body>\n";
BigPipe::render();