<?php

/**
 * Override Laravel's default server router for `php artisan serve`.
 * Serves the service worker with the Service-Worker-Allowed header
 * so it can control the entire site scope from /build/sw.js.
 *
 * @see https://vite-pwa-org.netlify.app/frameworks/laravel.html
 */
$publicPath = getcwd();

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? ''
);

if ($uri !== '/' && $uri !== '/build/sw.js' && file_exists($publicPath.$uri)) {
    return false;
}

if ($uri === '/build/sw.js') {
    header('Service-Worker-Allowed: /');
    header('Content-Type: text/javascript');
    echo file_get_contents(__DIR__.'/public/build/sw.js');
    exit;
}

require_once $publicPath.'/index.php';
