Skip to content

Instantly share code, notes, and snippets.

@freekrai
Forked from james2doyle/NextValetDriver.php
Created February 23, 2019 06:00
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 freekrai/8b04f4b3378573b8fb42711db0b08c33 to your computer and use it in GitHub Desktop.
Save freekrai/8b04f4b3378573b8fb42711db0b08c33 to your computer and use it in GitHub Desktop.
A Laravel Valet driver for running generated Next.js sites. This driver assumes you have not changed the default public path (/out) in the next.config.js
<?php
/**
* NextValetDriver for running compiled next.js sites
*/
class NextValetDriver extends BasicValetDriver
{
/**
* Determine if the driver serves the request.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return bool
*/
public function serves($sitePath, $siteName, $uri)
{
return is_dir($sitePath . '/out/_next');
}
/**
* Get the fully resolved path to the application's front controller.
*
* @param string $sitePath
* @param string $siteName
* @param string $uri
* @return string
*/
public function frontControllerPath($sitePath, $siteName, $uri)
{
$_SERVER['PHP_SELF'] = $uri;
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
return parent::frontControllerPath($sitePath, $siteName, '/out' . $uri);
}
public function isStaticFile($sitePath, $siteName, $uri)
{
// handle subfolder index pages
$try_html = $sitePath . '/out' . $uri . '/index.html';
if (file_exists($try_html)) {
return $try_html;
}
// handle public static files
$try_uri = $sitePath . '/out' . $uri;
if (file_exists($try_uri)) {
return $try_uri;
}
// handle the component files
$path = '_next/';
if (false !== ($pos = stripos($uri, '/' . $path))) {
$new_uri = '/out' . substr($uri, $pos);
if (file_exists($sitePath . $new_uri)) {
return $sitePath . $new_uri;
}
}
return parent::isStaticFile($sitePath, $siteName, $uri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment