mirror of
https://github.com/trambarhq/relaks-wordpress-example.git
synced 2025-09-02 20:52:33 +02:00
Adding host header to requests.
This commit is contained in:
@@ -5,6 +5,7 @@ const CrossFetch = require('cross-fetch');
|
||||
const FrontEnd = require('./client/front-end');
|
||||
|
||||
const NGINX_HOST = process.env.NGINX_HOST;
|
||||
const EXTERNAL_HOST = process.env.EXTERNAL_HOST;
|
||||
const CACHE_SIZE = 500;
|
||||
const HTML_TEMPLATE = `${__dirname}/client/index.html`;
|
||||
|
||||
@@ -17,6 +18,7 @@ async function generate(path, target) {
|
||||
let fetch = (url, options) => {
|
||||
if (url.startsWith(host)) {
|
||||
sourceURLs.push(url.substr(host.length));
|
||||
options = addHostHeader(options);
|
||||
}
|
||||
return CrossFetch(url, options);
|
||||
};
|
||||
@@ -36,9 +38,21 @@ async function generate(path, target) {
|
||||
async function prefetch(path) {
|
||||
console.log(`Regenerating page: ${path}`);
|
||||
let url = NGINX_HOST + path;
|
||||
let options = addHostHeader({});
|
||||
return CrossFetch(url);
|
||||
}
|
||||
|
||||
function addHostHeader(options) {
|
||||
if (EXTERNAL_HOST) {
|
||||
let [ protocol, domain ] = EXTERNAL_HOST.split('://');
|
||||
let port = (protocol === 'https') ? 443 : 80;
|
||||
let host = domain + ':' + port;
|
||||
options = Object.assign({}, options);
|
||||
options.headers = Object.assign({ Host: host }, options.headers);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
generate,
|
||||
prefetch,
|
||||
|
Reference in New Issue
Block a user