Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Created June 29, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyletaylored/3d068b7ed51205ca3b198b9df8933710 to your computer and use it in GitHub Desktop.
Save kyletaylored/3d068b7ed51205ca3b198b9df8933710 to your computer and use it in GitHub Desktop.
Install Chrome into Pantheon Appserver using PHP and NodeJS
const { join } = require("path");
/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, ".cache", "puppeteer"),
};
{
"name": "pantheon-chrome/pantheon-chrome",
"description": "Load Chrome / Puppeteer into Pantheon",
"require": {
"pantheon-se/node-composer": "^2.1",
"spatie/lighthouse-php": "^1.0"
},
"config": {
"allow-plugins": {
"pantheon-se/node-composer": true
}
},
"scripts": {
"post-install-cmd": [
"@npm"
],
"post-update-cmd": [
"@npm"
],
"npm": "npm install && node node_modules/puppeteer/install.js",
}
}
<?php
require 'vendor/autoload.php';
use Spatie\Lighthouse\Lighthouse;
use Spatie\Lighthouse\Enums\Category;
// Used as php script
// php lighthouse.php https://example.com
// Get the URL parameter from the query string
if (!empty($argv[1])) {
$url = $argv[1];
// Chrome options
$chrome_options = [
'chromeFlags' => [
'--headless',
'--no-sandbox',
],
];
// Find path to Chrome
$baseDirectory = '/code/.cache/puppeteer/chrome/'; // Set the base directory path
$directoryPattern = $baseDirectory . 'linux-*'; // Pattern to match subdirectories
$subdirectory = glob($directoryPattern, GLOB_ONLYDIR);
if (!empty($subdirectory[0])) {
$chromeFilePath = $subdirectory[0] . '/chrome-linux64/chrome';
// Check if the "chrome" file exists
if (file_exists($chromeFilePath) && is_file($chromeFilePath)) {
// Found the "chrome" file
$chrome_options['chromePath'] = $chromeFilePath;
} else {
error_log("Chrome file not found.");
}
}
// returns an instance of Spatie\Lighthouse\LighthouseResult
$result = Lighthouse::url($url)
->withChromeOptions($chrome_options)
->run()
->saveHtml('/files/report.html');
return json_encode($result->scores(), JSON_PRETTY_PRINT);
}
echo "No argument supplied";
exit(1);
{
"name": "pantheon-chrome",
"dependencies": {
"chrome-launcher": "^0.15.2",
"lighthouse": "^10.3.0",
"puppeteer": "^20.7.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment