namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CacheToR2
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
// Hàm terminate chạy ngầm sau khi server đã trả HTML cho khách
public function terminate($request, $response)
{
// Chỉ bắt request GET, status 200 và là file HTML
if ($request->isMethod('GET')
&& $response->status() == 200
&& str_contains($response->headers->get('Content-Type'), 'text/html')
) {
$path = $request->path();
// Danh sách các route bỏ qua không lưu cache
if ($request->is('admin/*') || $request->is('api/*') || $request->is('login')) {
return;
}
// Đặt tên file giống với logic của Worker
$r2Path = $path === '/' ? 'index.html' : $path . '.html';
// Lưu ngầm lên R2
try {
Storage::disk('r2')->put($r2Path, $response->getContent(), [
'ContentType' => 'text/html; charset=utf-8',
'CacheControl' => 'max-age=31536000'
]);
} catch (\Exception $e) {
// Ghi log nếu có lỗi (chưa config đúng S3/R2 chẳng hạn)
\Log::error('R2 Cache Error: ' . $e->getMessage());
}
}
}
}namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CacheToR2
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
// Hàm terminate chạy ngầm sau khi server đã trả HTML cho khách
public function terminate($request, $response)
{
// Chỉ bắt request GET, status 200 và là file HTML
if ($request->isMethod('GET')
&& $response->status() == 200
&& str_contains($response->headers->get('Content-Type'), 'text/html')
) {
$path = $request->path();
// Danh sách các route bỏ qua không lưu cache
if ($request->is('admin/*') || $request->is('api/*') || $request->is('login')) {
return;
}
// Đặt tên file giống với logic của Worker
$r2Path = $path === '/' ? 'index.html' : $path . '.html';
// Lưu ngầm lên R2
try {
Storage::disk('r2')->put($r2Path, $response->getContent(), [
'ContentType' => 'text/html; charset=utf-8',
'CacheControl' => 'max-age=31536000'
]);
} catch (\Exception $e) {
// Ghi log nếu có lỗi (chưa config đúng S3/R2 chẳng hạn)
\Log::error('R2 Cache Error: ' . $e->getMessage());
}
}
}
}namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CacheToR2
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
// Hàm terminate chạy ngầm sau khi server đã trả HTML cho khách
public function terminate($request, $response)
{
// Chỉ bắt request GET, status 200 và là file HTML
if ($request->isMethod('GET')
&& $response->status() == 200
&& str_contains($response->headers->get('Content-Type'), 'text/html')
) {
$path = $request->path();
// Danh sách các route bỏ qua không lưu cache
if ($request->is('admin/*') || $request->is('api/*') || $request->is('login')) {
return;
}
// Đặt tên file giống với logic của Worker
$r2Path = $path === '/' ? 'index.html' : $path . '.html';
// Lưu ngầm lên R2
try {
Storage::disk('r2')->put($r2Path, $response->getContent(), [
'ContentType' => 'text/html; charset=utf-8',
'CacheControl' => 'max-age=31536000'
]);
} catch (\Exception $e) {
// Ghi log nếu có lỗi (chưa config đúng S3/R2 chẳng hạn)
\Log::error('R2 Cache Error: ' . $e->getMessage());
}
}
}
}
Server Error
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CacheToR2
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
// Hàm terminate chạy ngầm sau khi server đã trả HTML cho khách
public function terminate($request, $response)
{
// Chỉ bắt request GET, status 200 và là file HTML
if ($request->isMethod('GET')
&& $response->status() == 200
&& str_contains($response->headers->get('Content-Type'), 'text/html')
) {
$path = $request->path();
// Danh sách các route bỏ qua không lưu cache
if ($request->is('admin/*') || $request->is('api/*') || $request->is('login')) {
return;
}
// Đặt tên file giống với logic của Worker
$r2Path = $path === '/' ? 'index.html' : $path . '.html';
// Lưu ngầm lên R2
try {
Storage::disk('r2')->put($r2Path, $response->getContent(), [
'ContentType' => 'text/html; charset=utf-8',
'CacheControl' => 'max-age=31536000'
]);
} catch (\Exception $e) {
// Ghi log nếu có lỗi (chưa config đúng S3/R2 chẳng hạn)
\Log::error('R2 Cache Error: ' . $e->getMessage());
}
}
}
}namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CacheToR2
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
// Hàm terminate chạy ngầm sau khi server đã trả HTML cho khách
public function terminate($request, $response)
{
// Chỉ bắt request GET, status 200 và là file HTML
if ($request->isMethod('GET')
&& $response->status() == 200
&& str_contains($response->headers->get('Content-Type'), 'text/html')
) {
$path = $request->path();
// Danh sách các route bỏ qua không lưu cache
if ($request->is('admin/*') || $request->is('api/*') || $request->is('login')) {
return;
}
// Đặt tên file giống với logic của Worker
$r2Path = $path === '/' ? 'index.html' : $path . '.html';
// Lưu ngầm lên R2
try {
Storage::disk('r2')->put($r2Path, $response->getContent(), [
'ContentType' => 'text/html; charset=utf-8',
'CacheControl' => 'max-age=31536000'
]);
} catch (\Exception $e) {
// Ghi log nếu có lỗi (chưa config đúng S3/R2 chẳng hạn)
\Log::error('R2 Cache Error: ' . $e->getMessage());
}
}
}
}namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CacheToR2
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
// Hàm terminate chạy ngầm sau khi server đã trả HTML cho khách
public function terminate($request, $response)
{
// Chỉ bắt request GET, status 200 và là file HTML
if ($request->isMethod('GET')
&& $response->status() == 200
&& str_contains($response->headers->get('Content-Type'), 'text/html')
) {
$path = $request->path();
// Danh sách các route bỏ qua không lưu cache
if ($request->is('admin/*') || $request->is('api/*') || $request->is('login')) {
return;
}
// Đặt tên file giống với logic của Worker
$r2Path = $path === '/' ? 'index.html' : $path . '.html';
// Lưu ngầm lên R2
try {
Storage::disk('r2')->put($r2Path, $response->getContent(), [
'ContentType' => 'text/html; charset=utf-8',
'CacheControl' => 'max-age=31536000'
]);
} catch (\Exception $e) {
// Ghi log nếu có lỗi (chưa config đúng S3/R2 chẳng hạn)
\Log::error('R2 Cache Error: ' . $e->getMessage());
}
}
}
}
Server Error