Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Last active July 25, 2022 16:51
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save james2doyle/d9f1a4509b68077bd72c78a3b58fdeca to your computer and use it in GitHub Desktop.
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
<?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);
}
}
@gilles6
Copy link

gilles6 commented May 30, 2020

Does this configuration allow to have for example Laravel on backend.test and the Nuxt app on subdomain nuxt.backend.test ? But then, how about npm run dev which serves the Nuxt app on localhost:3000 ? Also, where do you install this file ?

@james2doyle
Copy link
Author

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

@mehrancodes
Copy link

@james2doyle what about Nuxt slug pages?
it can't detect this:
'/posts/:slug' => '/post/my-new-tutorial'

@james2doyle
Copy link
Author

@mehrencodes it should be able to detect that as long as the pages have been prerendered with nuxt generate

@glennraya
Copy link

Hi, does this work on when I run nuxt on development (npm run dev)?

@james2doyle
Copy link
Author

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