Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to get subdomain #8936

Closed
poupryc opened this issue Oct 2, 2019 · 3 comments
Closed

Add a way to get subdomain #8936

poupryc opened this issue Oct 2, 2019 · 3 comments

Comments

@poupryc
Copy link

poupryc commented Oct 2, 2019

Feature request

I'm interested in the latest functionality of Now, the wildcard domain, but even with the static fun example, no simple example of how to use and recover a subdomain is available.

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

My use case is as follows: I use the with-apollo example and my goal is to include a variable containing the current subdomain

import { useQuery } from '@apollo/react-hooks'

import { withApollo } from '../../lib'

const GET_PAGE_WITH_SUBDOMAIN = gql``

const Page = () => {
  const subdomain = useSubdomain() // <- here is the problem
  const { data, loading, error } = useQuery(GET_PAGE_WITH_SUBDOMAIN, {
    variables: { branch: 'en'}
  })

  return (
    <>
      <h1>{page.title}</h1>
      <p>{page.content}</p>
    </>
  )
}

export default withApollo(Page)

Describe the solution you'd like

The current router does not offer the possibility of recovering subdomains. A good thing would be to be able to access this information simply.

Describe alternatives you've considered

I don't know if you can use a getInitialProps and if there is a simpler way to do it

Thanks,

@JClackett
Copy link
Contributor

i use something like this in getInitialProps where req is the req from context

export function getSubdomain(req) {
  let host
  let sub
  if (req && req.headers.host) {
    host = req.headers.host
  }
  if (typeof window !== "undefined") {
    host = window.location.host
  }
  if (host) {
    sub = host.split(localhost:3000)[0]
    if (sub) {
      return sub.split(".")[0]
    }
  }
}

@timneutkens
Copy link
Member

There is no way for us to reliably handle this as subdomains can come in many forms and parsing them should be done by the user to avoid security issues in Next.js. For example Next.js also doesn't handle the host part. Reason for this is that the host header is considered not secure depending on where you're hosting as users would be able to provide the host header in the request on some platforms (not on Now).

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants