{{-- 页面加载完成后 opportunistic 触发本地 ping；失败静默，不阻塞首屏。 --}}
<script>
(function () {
    const pingUrl = @json($pingUrl);
    const timeoutMs = @json($timeoutMs);

    function sendPing() {
        const controller = new AbortController();
        const timer = window.setTimeout(function () {
            controller.abort();
        }, timeoutMs);

        fetch(pingUrl, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-CSRF-TOKEN': @json(csrf_token()),
                'X-Requested-With': 'XMLHttpRequest',
            },
            credentials: 'same-origin',
            signal: controller.signal,
            keepalive: true,
            body: '{}',
        }).catch(function () {}).finally(function () {
            window.clearTimeout(timer);
        });
    }

    if (typeof window.requestIdleCallback === 'function') {
        window.requestIdleCallback(sendPing, { timeout: 2500 });
    } else {
        window.setTimeout(sendPing, 800);
    }
})();
</script>
