Add `hostname` parameter to getInitialProps
See original GitHub issueFeature 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:
- Created 4 years ago
- Reactions:3
- Comments:8 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
May I suggest reviving this?
ctx.hostname
appears to me as the only way to retrieve the hostname insidegetInitialProps
whenctx.req
does not exist because the user followed a<Link>
. Another way of passing info togetInitialProps
such as actx.state
could be an alternative too.If I actually didn’t see a way of doing this that already exists, please let me know.
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: