I am making a whatwg-fetch request from my React application served up at 111.111.111.111 like so:
fetch('http://222.222.222.222/search.php', {
credentials: 'include',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
SearchValue: this.props.SearchValue,
SearchOption: this.props.SearchOption,
})
})
.then(response => response.json())
.then(data => {
console.log(data);
})
On the other end (at 222.222.222.222) I have this:
<?php
require_once('initialize.php');
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://111.111.111.111");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: content-type");
header("Access-Control-Max-Age: 86400");
/* Prepared Statement (not very complicated, returns 5 results) */
For some reason the server takes 15 seconds to respond to my fetch request. It takes 15 seconds (most of the time) just to get the first byte back from the responding server. What am I doing wrong? How do I use fetch to get data from my database in JSON format?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Please make sure you are not making any external requests which might be slowing down the application.
One other suggestion I have after some research and it seems the problem is related with the way Chrome uses connections, if you are checking this with a Chrome browser that might be reason behind it.
When dealing with a Time To First Byte (TTFB) as high as 15 seconds consistently, the issue often lies in server-side processing, network delays, or configuration issues rather than the fetch request itself. Here’s a step-by-step approach to troubleshoot and potentially resolve the high TTFB issue:
The first step is to check the server-side script handling the request. Since you’re using PHP and making database queries:
Debug the PHP Script:
$time_start = microtime(true);
// Your database query
$result = $db->query('SELECT * FROM table WHERE condition');
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
error_log('Query Time: ' . $execution_time);
Optimize Database Interaction:
ping or traceroute from the server to the database if they are separate, and from your client to the server to check for any network delays.header("Access-Control-Allow-Origin: *");
Check Browser Network Panel:
JavaScript Execution Delays:
By methodically checking these areas, you should be able to pinpoint the cause of the high TTFB and address it accordingly. If after these checks the issue persists, consider reaching out to your hosting provider or a network expert for further analysis.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.