The only solution I've found is to restart the php fpm docker container every time this happens. I've tried various versions, upgrading to php8 as well, and the same issue still occurs. I've set a cron that restarts the container every 4 hours, but in some cases, users still experience issues. I'm really out of ideas.
Sporadic CORS errors
Hi, I have a weird situation that's been happening for about a year now. Every now and then, for some minutes, all requests who need a preflight request fail, because out of nowhere the 'access-control-allow-...' headers are missing.
I tried restarting the server, clearing the cache, messing around with the order and content of the cors middleware and the headers, and I've found no fix. The only solution is to just come back about 30 minutes later and send the requests again. Only then they start working again.
The only thing I've noticed, is that the headers are back again only if the origin doesn't match. For example, if I put some invalid domain in the 'Access-Control-Allow-Origin' header. But if the headers are correct and matching what needs to match, then they disappear, only to appear again some 30 minutes later.
Having such a sporadic behaviour, and most of the time working correctly, it's been very frustrating and hard to identify why this is happpening. Does anyone have any ideas? I'm using Laravel 7.3.0 as back-end and NuxtJs with Axios as front-end. I'm also using Cloudflare and Docker.
namespace App\Http\Middleware;
use Closure;
use Sentry\Laravel\Facade;
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
//In case of avatars, header() is not StreamedResponse function.
if ($response instanceof \Symfony\Component\HttpFoundation\StreamedResponse) {
return $response;
}
$IlluminateResponse = 'Illuminate\Http\Response';
$SymfonyResponse = 'Symfony\Component\HttpFoundation\Response';
$JsonResponse = 'Illuminate\Http\JsonResponse';
$headers = [
'Access-Control-Allow-Origin' => "*",
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers',
'Access-Control-Allow-Crendentials' => 'true'
];
if($response instanceof $IlluminateResponse || $response instanceof $JsonResponse) {
foreach ($headers as $key => $value) {
$response->header($key, $value);
}
return $response;
}
if($response instanceof $SymfonyResponse) {
foreach ($headers as $key => $value) {
$response->headers->set($key, $value);
}
return $response;
}
return $response;
}
}
Please or to participate in this conversation.