mirror of
https://github.com/trambarhq/relaks-wordpress-example.git
synced 2025-09-03 05:02:34 +02:00
Using keepalive connection when fetching.
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
const CrossFetch = require('cross-fetch');
|
const CrossFetch = require('cross-fetch');
|
||||||
|
|
||||||
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
|
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
|
||||||
|
const WORDPRESS_PROTOCOL = 'http' + (/^https:/.test(WORDPRESS_HOST) ? 's' : '');
|
||||||
|
|
||||||
|
let agent = new require(WORDPRESS_PROTOCOL).Agent({ keepAlive: true });
|
||||||
|
|
||||||
async function fetch(path) {
|
async function fetch(path) {
|
||||||
console.log(`Retrieving data: ${path}`);
|
console.log(`Retrieving data: ${path}`);
|
||||||
let url = `${WORDPRESS_HOST}${path}`;
|
let url = `${WORDPRESS_HOST}${path}`;
|
||||||
let res = await CrossFetch(url);
|
let res = await CrossFetch(url, { agent });
|
||||||
let resText = await res.text();
|
let resText = await res.text();
|
||||||
let object;
|
let object;
|
||||||
try {
|
try {
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
const Bluebird = require('bluebird');
|
const Bluebird = require('bluebird');
|
||||||
const FS = Bluebird.promisifyAll(require('fs'));
|
const FS = Bluebird.promisifyAll(require('fs'));
|
||||||
const ReactDOMServer = require('react-dom/server');
|
const ReactDOMServer = require('react-dom/server');
|
||||||
|
const HTTP = require('http');
|
||||||
const CrossFetch = require('cross-fetch');
|
const CrossFetch = require('cross-fetch');
|
||||||
const FrontEnd = require('./client/front-end');
|
const FrontEnd = require('./client/front-end');
|
||||||
|
|
||||||
@@ -15,10 +16,12 @@ async function generate(path, target) {
|
|||||||
let host = NGINX_HOST;
|
let host = NGINX_HOST;
|
||||||
// create a fetch() that remembers the URLs used
|
// create a fetch() that remembers the URLs used
|
||||||
let sourceURLs = [];
|
let sourceURLs = [];
|
||||||
|
let agent = new HTTP.Agent({ keepAlive: true });
|
||||||
let fetch = (url, options) => {
|
let fetch = (url, options) => {
|
||||||
if (url.startsWith(host)) {
|
if (url.startsWith(host)) {
|
||||||
sourceURLs.push(url.substr(host.length));
|
sourceURLs.push(url.substr(host.length));
|
||||||
options = addHostHeader(options);
|
options = addHostHeader(options);
|
||||||
|
options.agent = agent;
|
||||||
}
|
}
|
||||||
return CrossFetch(url, options);
|
return CrossFetch(url, options);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user