Last active
July 25, 2022 16:51
-
-
Save james2doyle/d9f1a4509b68077bd72c78a3b58fdeca to your computer and use it in GitHub Desktop.
A Laravel Valet driver for running generated Nuxt.js sites. This driver assumes you have not changed the default public path (/dist) in the nuxt.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* NuxtValetDriver for running compiled nuxt.js sites | |
*/ | |
class NuxtValetDriver 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 . '/dist/_nuxt'); | |
} | |
/** | |
* 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, '/dist' . $uri); | |
} | |
public function isStaticFile($sitePath, $siteName, $uri) | |
{ | |
// handle subfolder index pages | |
$try_html = $sitePath . '/dist' . $uri . '/index.html'; | |
if (file_exists($try_html)) { | |
return $try_html; | |
} | |
// handle public static files | |
$try_uri = $sitePath . '/dist' . $uri; | |
if (file_exists($try_uri)) { | |
return $try_uri; | |
} | |
// handle the component files | |
$path = '_nuxt/'; | |
if (false !== ($pos = stripos($uri, '/' . $path))) { | |
$new_uri = '/dist' . substr($uri, $pos); | |
if (file_exists($sitePath . $new_uri)) { | |
return $sitePath . $new_uri; | |
} | |
} | |
return parent::isStaticFile($sitePath, $siteName, $uri); | |
} | |
} |
@james2doyle what about Nuxt slug pages?
it can't detect this:
'/posts/:slug' => '/post/my-new-tutorial'
@mehrencodes it should be able to detect that as long as the pages have been prerendered with nuxt generate
Hi, does this work on when I run nuxt on development (npm run dev)?
If you use the Valet URL with the port of the dev server it should work fine. Your dev server needs to be running on localhost. I am using this feature now on a react project. I have a dev server running on port 3000 and I am accessing that through myvaleturl.localhost:3000
since valet just takes over localhost
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This allows you to run a nuxt site by either having the folder your parked domains list or by using
valet link
.You should looking into
valet link
. It should give you what you want