新增sitemap

This commit is contained in:
2022-03-16 11:36:39 +08:00
parent 3ab345f41c
commit aacc99aa98
2 changed files with 28 additions and 4 deletions

View File

@@ -10,10 +10,34 @@
// +----------------------------------------------------------------------
use app\common\tools\Sitemap;
use app\Request;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Route;
Route::rule('/site.xml', function () {
Route::rule('/site.xml', function (Request $request) {
$cache_key = 'sitemap_last_etag';
$last_etag = Cache::get($cache_key);
if (!empty($cache_key)) {
$if_not_match = $request->header('If-None-Match');
if (!empty($if_not_match)) {
if ($if_not_match == $last_etag) {
Log::debug('sitemap go for etag cache');
return xml('', 304)->eTag($last_etag);
}
}
}
$content = Sitemap::init();
return xml($content);
$last_etag = md5($content);
Cache::set($cache_key, $last_etag);
return xml($content)->eTag($last_etag);
});