Skip to content

Instantly share code, notes, and snippets.

@abdessamadely
Created August 10, 2025 11:59
Show Gist options
  • Select an option

  • Save abdessamadely/1b4f945641dfcef872ee8c6905b126b3 to your computer and use it in GitHub Desktop.

Select an option

Save abdessamadely/1b4f945641dfcef872ee8c6905b126b3 to your computer and use it in GitHub Desktop.

Revisions

  1. abdessamadely created this gist Aug 10, 2025.
    73 changes: 73 additions & 0 deletions NextJsBackendValetDriver.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?php

    namespace Valet\Drivers\Custom;

    use Valet\Drivers\ValetDriver;

    /**
    * A Valet driver to proxy a sub uri to a Next.js application
    * ~/.config/valet/Drivers/NextJsBackendValetDriver.php
    */
    class NextJsBackendValetDriver extends ValetDriver
    {
    /**
    * Check if a given URI string corresponds to a static asset file.
    */
    function isStaticAsset(string $uri): bool
    {
    return in_array($uri, [
    '/next.svg',
    '/vercel.svg'
    ]);
    }

    /**
    * Check if a given URI string corresponds to a Next.js route.
    */
    function isNextJsRoute(string $uri): bool
    {
    return in_array($uri, [
    '/hello'
    ]);
    }

    /**
    * Determine if the driver serves the request.
    */
    public function serves(string $sitePath, string $siteName, string $uri): bool
    {
    return $siteName === 'abdessamadely' && (
    str_starts_with($uri, '/backend') ||
    str_starts_with($uri, '/static') ||
    str_starts_with($uri, '/_next') ||
    $this->isStaticAsset($uri) ||
    $this->isNextJsRoute($uri)
    );
    }

    /**
    * Determine if the incoming request is for a static file.
    */
    public function isStaticFile(string $sitePath, string $siteName, string $uri)/* : string|false */
    {
    $sitePath = dirname($sitePath) . DIRECTORY_SEPARATOR . 'backend';

    if (str_starts_with($uri, '/_next') && file_exists($staticFilePath = $sitePath . str_replace('_next', '.next', $uri))) {
    return $staticFilePath;
    }

    if ($this->isStaticAsset($uri) && file_exists($staticFilePath = $sitePath . '/public' . $uri)) {
    return $staticFilePath;
    }

    return false;
    }

    /**
    * Get the fully resolved path to the application's front controller.
    */
    public function frontControllerPath(string $sitePath, string $siteName, string $uri): ?string
    {
    return dirname($sitePath) . '/backend/index.php';
    }
    }