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

getRequestURL in a server plugin, middleware, etc. does not consistently return the actual origin or hostname of the deployed site/worker #2388

Open
adamdehaven opened this issue Apr 23, 2024 · 1 comment

Comments

@adamdehaven
Copy link
Contributor

adamdehaven commented Apr 23, 2024

Environment

  • Nitro 2.9.6
  • Nuxt 3.11.2

Reproduction

server versus client values

Description URL
Deploy preview https://nitro_origin_repro.adamdehaven.workers.dev/
CodeSandbox https://codesandbox.io/p/github/adamdehaven/nitro-origin-repro/main
Code https://github.com/adamdehaven/nitro-origin-repro

Describe the bug

When utilizing getRequestURL(event) inside Nitro middleware (or inside a Nitro plugin, etc.) in a Nuxt project, I would expect the origin and hostname to return the deployed hostname and origin of the request rather than an "internal" server origin/host or something hidden behind a CNAME.

Additional context

I utilized a Nuxt app as a reproduction. You should be able to see where I am attempting to retrieve and store the origin and hostname via getRequestURL in /server/middleware/set-origin.ts

I'm fully aware I could be missing something obvious or intended, so if this is the case, I'd greatly appreciate some help in understanding what's going on and what is to be expected based on the code in the reproduction.


Problem I'm trying to solve (simplified):

I have a Nuxt application that will be serving a multi-tenant application whereby the Nuxt layers provide all functionality, and content is loaded at runtime per tenant.

We will be utilizing dynamic subdomains to determine which tenant's app is being requested, so I'd expect to be able to utilize any request event to identify app1.example.com or app2.example.com based on the origin or hostname extracted via getRequestURL(event).

Logs

No response

@TomKriek
Copy link

We solved it by wrapping useFetch and adding in a header.

Pseudo code as im not on company computer:

const useTenantFetch = (url, options) => {
    const opts = {
        ...options,
        headers : {
            'x-requester' : useRequestUrl().host
        }
    }
    return useFetch(url, opts);
}

Server side event handlers in nitro can then extract the header

const tenant = getRequestHeader(event, "x-requester");

Which should net you the original domain/host calling the nuxt app. (url in address bar).
We have a working MT setup in AWS this way;

useRequestUrl contains all data needed during SSR and can be forwarded to the server -> api routes locally this way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants