getInitialProps request stability improvements

This commit is contained in:
tmshv committed 2019-10-12 06:21:02 +03:00
1 parent 95c8369df2
commit 7ab68f5a8e
13 files changed
+65 -55

No files matched your search

+27 -1
View File
@@ -1,7 +1,33 @@
import absoluteUrl from 'next-absolute-url'
export async function requestGet(url, defaultResponse) {
try {
const res = await axios.get(url)
return res.data
} catch (e) {
return defaultResponse
}
}
export function createApiUrl(req, path) {
const { origin } = absoluteUrl(req, 'localhost:3000')
return `${origin}${path}`
}
function absoluteUrl(req, defaultLocalhost) {
var protocol = 'https:';
var host = req
? (req.headers['x-forwarded-host'] || req.headers['host'])
: window.location.host;
host = host || defaultLocalhost
if (host.indexOf('localhost') > -1) {
if (defaultLocalhost) host = defaultLocalhost;
protocol = 'http:';
}
return {
protocol: protocol,
host: host,
origin: protocol + '//' + host
};
}