37 lines
1.8 KiB
PHP
37 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
class StellarDevAssets
|
|
{
|
|
public static function apply(Request $request, string $html): string
|
|
{
|
|
// Explicit local opt-in: never select dev assets for a public hostname.
|
|
$marker = storage_path('framework/stellar-vite.hot');
|
|
$origin = 'http://127.0.0.1:5173';
|
|
if (!in_array($request->getHost(), ['127.0.0.1', 'localhost', '::1'], true)
|
|
|| !is_file($marker) || trim(file_get_contents($marker)) !== $origin) {
|
|
return $html;
|
|
}
|
|
// A stale marker after an interrupted process falls back to built assets.
|
|
$socket = @fsockopen('127.0.0.1', 5173, $error, $message, 0.1);
|
|
if (!$socket) return $html;
|
|
fclose($socket);
|
|
|
|
$html = preg_replace('#<script\s+type="module"[^>]*src="/theme/Stellar/assets/[^"]+"[^>]*></script>#', '', $html);
|
|
$html = preg_replace('#<link\s+rel="stylesheet"[^>]*href="/theme/Stellar/assets/[^"]+"[^>]*>#', '', $html);
|
|
$html = str_replace('src="/theme/Stellar/env.js"', 'src="'.$origin.'/env.js"', $html);
|
|
// Allow only this loopback dev origin; production HTML is left unchanged.
|
|
$html = str_replace("script-src 'self'", "script-src 'self' ".$origin, $html);
|
|
$html = str_replace("style-src 'self'", "style-src 'self' ".$origin, $html);
|
|
$html = str_replace("connect-src 'self'", "connect-src 'self' ".$origin." ws://127.0.0.1:5173", $html);
|
|
$html = str_replace("img-src 'self'", "img-src 'self' ".$origin, $html);
|
|
$html = str_replace("font-src 'self'", "font-src 'self' ".$origin, $html);
|
|
$scripts = '<script type="module" src="'.$origin.'/@vite/client"></script>'
|
|
.'<script type="module" src="'.$origin.'/src/main.ts"></script>';
|
|
return str_replace('</head>', $scripts.'</head>', $html);
|
|
}
|
|
}
|