PHPackages                             hugsbrugs/php-prerender-spa - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hugsbrugs/php-prerender-spa

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hugsbrugs/php-prerender-spa
===========================

PHP Prerender Single Page Application utilities

221HTML

Since Mar 31Pushed 9y ago1 watchersCompare

[ Source](https://github.com/hugsbrugs/php-prerender-spa)[ Packagist](https://packagist.org/packages/hugsbrugs/php-prerender-spa)[ RSS](/packages/hugsbrugs-php-prerender-spa/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

php-prerender-spa
=================

[](#php-prerender-spa)

This librairy provides utilities function to serve Search Engine Crawlers Website Snapshots

[![Build Status](https://camo.githubusercontent.com/51377498c8f458feab24f8d84602f8f5dd4d7408fba76cfe91926403e1bbef53/68747470733a2f2f7472617669732d63692e6f72672f6875677362727567732f7068702d70726572656e6465722d7370612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/hugsbrugs/php-prerender-spa)[![Coverage Status](https://camo.githubusercontent.com/9ba3353825b16e8eb04cf7e2d2052077dd42b20f4ebc01bd8bbcd767131494eb/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6875677362727567732f7068702d70726572656e6465722d7370612f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/hugsbrugs/php-prerender-spa?branch=master)

Install
-------

[](#install)

Install package with composer

```
composer require hugsbrugs/php-prerender-spa

```

In your PHP code, load library

```
require_once __DIR__ . '/../vendor/autoload.php';
use Hug\PrerenderSpa\PrerenderSpa as PrerenderSpa;
```

Usage
-----

[](#usage)

### First Step : Generate Your Webpages Snapshots

[](#first-step--generate-your-webpages-snapshots)

```
$prerender_url = 'http://123.123.123.123:3000/';
$prerender_auth = 'USER:PASSWORD'; // or null
$output = __DIR__ .'/../data/snapshots/'; // directory where to store snapshot (must be writable)

# Load Urls From Sitemap
$urls = PrerenderSpa::get_sitemap_urls('/path/to/sitemap.xml');

# Or Set Urls manually
// $urls = ['https://hugo.maugey.fr/developeur-web/HTML5'];

# Instanciate PrerenderSpa
$PrerenderSpa = new PrerenderSpa($urls, $output, $prerender_url, $prerender_auth);
#  Launch Snapshot Generation
$PrerenderSpa->prerender();
# Wait ....
# Print Report
error_log(print_r($PrerenderSpa->report, true));
# 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

[](#second-step--serve-webpage-snapshots-to-web-crawlers)

Redirect search engine crawlers to prerender.php service

```

    RewriteCond %{HTTP_USER_AGENT} googlebot|yahoo|bingbot|baiduspider [NC,OR]
    RewriteCond %{QUERY_STRING} _escaped_fragment_
    RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(index\.php)?(.*) /prerender.php/https://hugo.maugey.fr/$3 [P,L]

```

Have a look at [prerender.php](example/prerender.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

[](#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.

Why This Service ?
------------------

[](#why-this-service-)

I've been using Prerender service hosted on my servers for a while now and was using brenett extension to cache snapshots but that solution was not satisfying me anymore because of a fixed cache time. So snapshots were regenerated even if they didn't needed to be. And time for serving snapshots, which is critical from an SEO point of view was just an abomination because of the lack of pre-prendering after cache expires and before crawlers were asking for content. This solution is not perfect because you need to manually list files you want to regenerate snapshots for but it suit my needs ;)

Unit Tests
----------

[](#unit-tests)

```
composer exec phpunit

```

To Do
-----

[](#to-do)

Compress gzip saved HTML Archive snapshots

Author
------

[](#author)

Hugo Maugey [visit my website ;)](https://hugo.maugey.fr)

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/cc04033020c3e5219ac30edf7d7362952e88ea64a0de2f41b89559f849f09901?d=identicon)[hugsbrugs](/maintainers/hugsbrugs)

---

Top Contributors

[![hugsbrugs](https://avatars.githubusercontent.com/u/4385090?v=4)](https://github.com/hugsbrugs "hugsbrugs (8 commits)")

### Embed Badge

![Health badge](/badges/hugsbrugs-php-prerender-spa/health.svg)

```
[![Health](https://phpackages.com/badges/hugsbrugs-php-prerender-spa/health.svg)](https://phpackages.com/packages/hugsbrugs-php-prerender-spa)
```

###  Alternatives

[robinvdvleuten/ulid

Universally Unique Lexicographically Sortable Identifier (ULID) implementation for PHP.

4573.9M31](/packages/robinvdvleuten-ulid)[mpratt/embera

Oembed consumer library. Converts urls into their html embed code. Supports 150+ sites, such as Youtube, Twitter, vimeo, Instagram etc.

3535.8M25](/packages/mpratt-embera)[phpro/grumphp-shim

GrumPHP Phar distribution

284.6M333](/packages/phpro-grumphp-shim)[platformsh/config-reader

Small helper to access Platform.sh environment variables

225.3M26](/packages/platformsh-config-reader)[typo3/cms-redirects

TYPO3 CMS Redirects - Create manual redirects, list existing redirects and automatically createredirects on slug changes.

167.3M74](/packages/typo3-cms-redirects)[saloonphp/pagination-plugin

Official Pagination Plugin For Saloon v3

101.9M98](/packages/saloonphp-pagination-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
