Created
August 10, 2025 11:59
-
-
Save abdessamadely/1b4f945641dfcef872ee8c6905b126b3 to your computer and use it in GitHub Desktop.
A way to serve two applications — a Next.js app and a custom PHP static site builder — under the same domain name without using subdomains, while keeping both projects within a single repository
This file contains hidden or 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 | |
| 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'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment