question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `hostname` parameter to getInitialProps

See original GitHub issue

Feature request

Add hostname parameter in ctx which returns current hostname on both server and client.

Is your feature request related to a problem? Please describe.

We have 2 web apps - Next.js frontend and Django API. We use Nginx to proxy routes so both are using the same host but API is hosted under /api. And because we deliver our app to VPS we don’t know the current host so we can’t just use localhost.

I have to use custom _app without prerendering so my page only renders on server.

Here’s my _app:

import React from 'react'
import App from 'next/app'

class CustomApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let host

    if (ctx.req) {
      host = ctx.req.headers.host
    } else {
      host = location.hostname
    }

    let pageProps = {}

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps({ ...ctx, host })
    }
    return { pageProps }
  }
  render() {
    const { Component, pageProps } = this.props
    return <Component {...pageProps} />
  }
}
export default CustomApp

Describe the solution you’d like

I can make a PR with this custom getInitialProps argument so it will be accessible via ctx.hostname:

Page.getInitialProps = async ({ hostname }) => {
  const res = await fetch(`${hostname}/some-proxied-api/data`)
}

Describe alternatives you’ve considered

Write a function getHost and call it in every getInitialProps. Not very suitable especially when you have tons of pages with using getInitialProps.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
thief-stycommented, Jan 2, 2020

May I suggest reviving this? ctx.hostname appears to me as the only way to retrieve the hostname inside getInitialProps when ctx.req does not exist because the user followed a <Link>. Another way of passing info to getInitialProps such as a ctx.state could be an alternative too.

If I actually didn’t see a way of doing this that already exists, please let me know.

7reactions
Timercommented, Sep 10, 2019

This is unnecessary and can be handled by creating your own fetch wrapper. We typically don’t increase the API surface for features easily/better handled in user-land.

Thanks for the suggestion!


By the way, you don’t need hostname on the client.

This should be sufficient:

fetch(`${req ? req.headers.host : ''}/some-proxied-api/data`)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get page URL or hostname in NextJs Project?
In my case I wanted to dynamically set the og:url of my webpage. This is what I did. We have router.pathname as a...
Read more >
getInitialProps - Data Fetching - Next.js
getInitialProps receives a single argument called context , it's an object with the following properties: pathname - Current route. That is the path...
Read more >
How to get current route in Next.js ? - GeeksforGeeks
Using Context prop in getInitialProps. Let's understand all three methods with the implementation. Method 1: Using useRouter() Method: In NextJs ...
Read more >
next - npm
and add a script to your package.json like this: ... opt-out of static optimization by capturing the query parameter in getInitialProps .
Read more >
How to get page URL or hostname in NextJs Project?-Reactjs
You need to ensure your access to window.location.hostname happens on the client-side only, and not during server-side rendering (where window does not exist)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found