Nuxt Detect Server Base Url

Nov 1, 2020
Get hostname at Server Side Rendering
function detectServerBaseUrl(req) {  if (req.headers.referer) {    const url = new URL(req.headers.referer)    return `${url.protocol}//${url.host}`  }  else if (req.headers.host) {    const protocol = req.connection.encrypted ? 'https' : 'http:'    return `${protocol}//${req.headers.host}`  }  else if (req.connection.remoteAddress) {    const protocol = req.connection.encrypted ? 'https' : 'http:'    return `${protocol}//${req.connection.localAddress}:${req.connection.localPort}`  }  return undefined}

NOTE: Refer https://nuxtjs.org/docs/2.x/internals-glossary/context#req

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.