Skip to content

Commit

Permalink
Add log adn 404 500
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsbrugs committed Mar 31, 2017
1 parent 06a184d commit ff0081f
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 31 deletions.
58 changes: 45 additions & 13 deletions example/prerender.php
Expand Up @@ -5,23 +5,55 @@
use Hug\PrerenderSpa\PrerenderSpa as PrerenderSpa;
use Hug\Http\Http as Http;

$url = $_REQUEST['URL'];
error_log('url : ' . $url);

# Where do you store prerender filesystem
$output = __DIR__ . '/../data/';
$log = true;


# .htaccess http://prerender.io/URL_TO_SNAP
$url = $_REQUEST['URL'];
# .htaccess http://prerender?URL_TO_SNAP
// $url = $_SERVER['QUERY_STRING'];

# In special case you have to rewrite home URL
// if($url==='https://hugo.maugey.fr/index.php')
// $url = 'https://hugo.maugey.fr/index';

# Get Snapshot
// $url = 'https://hugo.maugey.fr/developeur-web/HTML5';
if(false !== $snapshot = PrerenderSpa::get_snapshot($url, $output)
// error_log('Prerender URL : ' . $url);

$html = null;
$http_code = null;

try
{
Http::header_status(200);
# gzip ?
echo $snapshot;
# Get Snapshot
if(false !== $snapshot = PrerenderSpa::get_snapshot($url, $output)
{
$http_code = 200;
$html = $snapshot;
}
else
{
# Set Header status 404 Not Found
$http_code = 404;
$html = PrerenderSpa::get_404($output);
}

# Log Snapshot Request to analyse traffic
if($log)
{
$ip = $_REQUEST['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'];
PrerenderSpa::log_snapshot($ip, $ua, $url, $http_code, $output);
}

}
else
catch(\Exception $e)
{
# Set Header status 404 Not Found
Http::header_status(404);
# include real 404
echo '404';
$http_code = 500;
$html = PrerenderSpa::get_500($output);
}

Http::header_status($http_code);
echo $html;
26 changes: 26 additions & 0 deletions example/test.php
Expand Up @@ -8,6 +8,7 @@
$prerender_auth = 'USER:PASS';
$output = __DIR__ .'/../data/';


# Load Sitemap Urls
// $urls = PrerenderSpa::get_sitemap_urls(__DIR__ . '/../data/sitemap.xml');

Expand All @@ -26,3 +27,28 @@
error_log(print_r($PrerenderSpa->report, true));


# Get Your Personnalized 404
// $page = PrerenderSpa::get_404($output);
// echo $page;

# Set Your Personnalized 404
// $html = 'coucou';
// $set = PrerenderSpa::set_404($html, $output);
// echo var_dump($set);

# Get Your Personnalized 500
// $page = PrerenderSpa::get_500($output);
// echo $page;

# Set Your Personnalized 500
// $html = 'coucou';
// $set = PrerenderSpa::set_500($html, $output);
// echo var_dump($set);

# Log Snapshot Request
// $ip = '123.123.123.123';
// $ua = 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
// $url = 'http://test.com';
// $http_code = 200;
// $log = PrerenderSpa::log_snapshot($ip, $ua, $url, $http_code, $output);
// echo var_dump($log);
46 changes: 44 additions & 2 deletions readme.md
Expand Up @@ -39,7 +39,12 @@ $PrerenderSpa->prerender();
# Wait ....
# Print Report
error_log(print_r($PrerenderSpa->report, true));
# Checkout for 404
# Set Your Personnalized 404
$html = 'My Personalized 404';
PrerenderSpa::set_404($html, $output);
# Set Your Personnalized 500
$html = 'My Personalized 500';
PrerenderSpa::set_500($html, $output);
```

### Second Step : Serve Webpage Snapshots to web crawlers
Expand All @@ -52,6 +57,44 @@ Redirect search engine crawlers to prerender.php service
</IfModule>
```

Have a look at [prerender.php](example/prerender.php)
```php
# Where do you store prerender filesystem
$output = __DIR__ . '/../data/';

# .htaccess http://prerender.io/URL_TO_SNAP
$url = $_REQUEST['URL'];

$html = null;
$http_code = null;

try
{
# Get Snapshot
if(false !== $snapshot = PrerenderSpa::get_snapshot($url, $output)
{
$http_code = 200;
$html = $snapshot;
}
else
{
# Set Header status 404 Not Found
$http_code = 404;
$html = PrerenderSpa::get_404($output);
}

}
catch(\Exception $e)
{
$http_code = 500;
$html = PrerenderSpa::get_500($output);
}

Http::header_status($http_code);
echo $html;
```


### Third Step : Generate Snapshot On Demand
Repeat First Step by providing only URLs whose content has changed to optimize server from running headless browser snapshot service for nothing.

Expand All @@ -69,7 +112,6 @@ composer exec phpunit

Compress gzip saved HTML
Archive snapshots
Log search engine visits

## Author

Expand Down

0 comments on commit ff0081f

Please sign in to comment.