Return 204 instead of 200 for CDN cache

This commit is contained in:
Pig Fang 2018-07-22 16:42:58 +08:00
parent e30e538d5f
commit 5e3e2bf688
7 changed files with 14 additions and 22 deletions

View File

@ -228,7 +228,7 @@ class AdminController extends Controller
{
$form->checkbox('force_ssl')->label()->hint();
$form->checkbox('auto_detect_asset_url')->label()->description();
$form->checkbox('return_200_when_notfound')->label()->description();
$form->checkbox('return_204_when_notfound')->label()->description();
$form->text('cache_expire_time')->hint(OptionForm::AUTO_DETECT);

View File

@ -38,12 +38,10 @@ class CheckPlayerExist
if (! Player::where('player_name', $player_name)->get()->isEmpty())
return $next($request);
if (option('return_200_when_notfound')) {
return json([
'player_name' => $player_name,
'errno' => 404,
'msg' => 'Player Not Found.'
])->header('Cache-Control', 'public, max-age='.option('cache_expire_time'));
if (option('return_204_when_notfound')) {
return response('', 204, [
'Cache-Control' => 'public, max-age='.option('cache_expire_time')
]);
} else {
return abort(404, trans('general.unexistent-player'));
}

View File

@ -34,7 +34,7 @@ return [
'auto_del_invalid_texture' => 'false',
'allow_downloading_texture' => 'true',
'texture_name_regexp' => '',
'return_200_when_notfound' => 'false',
'return_204_when_notfound' => 'false',
'cache_expire_time' => '31536000',
'max_upload_file_size' => '1024',
'force_ssl' => 'false',

View File

@ -136,9 +136,9 @@ resources:
title: Assets URL
label: Determine assets url automatically.
description: Load asset files according to current URL. The site url will be used if this is not enabled. Please unable this if requests don't go through CDN.
return_200_when_notfound:
return_204_when_notfound:
title: HTTP Response Code
label: Return 200 instead of 404 when requesting un-existent player.
label: Return 204 instead of 404 when requesting un-existent player.
description: If your CDN doesn't cache 404 pages, please turn this on. A flood of requests to un-existent players will greatly slow down the site.
cache_expire_time:
title: Cache Exipre Time

View File

@ -136,9 +136,9 @@ resources:
title: 资源地址
label: 自动判断资源文件地址
description: 根据当前 URL 自动加载资源文件,如果关闭则将根据「站点地址」填写的内容加载。如果出现 CDN 回源问题请关闭
return_200_when_notfound:
return_204_when_notfound:
title: HTTP 响应码
label: 请求不存在的角色时返回 200 而不是 404
label: 请求不存在的角色时返回 204 而不是 404
description: 如果你的 CDN 不缓存 404 页面,请打开此项。否则大量对不存在角色的 Profile 请求会加重站点负载。
cache_expire_time:
title: 缓存失效时间

View File

@ -140,12 +140,12 @@ class AdminControllerTest extends BrowserKitTestCase
$this->visit('/admin/options')
->check('force_ssl')
->uncheck('auto_detect_asset_url')
->check('return_200_when_notfound')
->check('return_204_when_notfound')
->type('0', 'cache_expire_time')
->press('submit_resources');
$this->assertTrue(option('force_ssl'));
$this->assertFalse(option('auto_detect_asset_url'));
$this->assertTrue(option('return_200_when_notfound'));
$this->assertTrue(option('return_204_when_notfound'));
$this->assertEquals('0', option('cache_expire_time'));
}

View File

@ -95,14 +95,8 @@ class MiddlewareTest extends TestCase
->assertStatus(404)
->assertSee('Un-existent player');
Option::set('return_200_when_notfound', true);
$this->getJson('/nope.json')
->assertSuccessful()
->assertJson([
'player_name' => 'nope',
'errno' => 404,
'msg' => 'Player Not Found.'
]);
Option::set('return_204_when_notfound', true);
$this->getJson('/nope.json')->assertStatus(204);
$player = factory(App\Models\Player::class)->create();
$this->getJson("/{$player->player_name}.json")