nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Built-in proxy support

Open pi0 opened this issue 2 years ago • 3 comments

Adding as a feature to route rules.

pi0 avatar Apr 14 '22 18:04 pi0

@pi0, would you be able to give any ETA on this one? 🙏 Would be super useful to have it working in Nuxt 3.

DamianGlowala avatar Jun 24 '22 15:06 DamianGlowala

Will work on it ASAP. In the meantime you can use https://www.npmjs.com/package/nuxt-proxy for Nuxt 3 apps only keep in mind some options are probably going to be simplified compating to http-proxy so avoid using options when possible to reduce breaking change chances.

Alternatively you can define a nitro route handler that uses fetch to upstream for proxy effect. This gives full control over headers.

pi0 avatar Jul 11 '22 16:07 pi0

can't use nuxt3@latest because proxy issues, non module working rn. is nitro proxy ready yet?

ironytr avatar Sep 26 '22 13:09 ironytr

devProxy doesn't seem to be supporting relative URL in fetch, is this by design? (nitro 0.6.1)

I.e.

// config:
export default defineNuxtConfig({
    nitro: {
        devProxy: {
            '/api/': {
                target: process.env.API_TARGET,
                changeOrigin: true
            }
        }
    }
})

// ======================
// later, on server side:
// ======================

const r = await (await fetch('/api/test')).json();
// => 500 Invalid URL

makkarpov avatar Nov 09 '22 13:11 makkarpov

@makkarpov Yes. You need to pass full URLs because dev proxy is outside of worker and needs to make an actual request.

pi0 avatar Nov 10 '22 09:11 pi0

Any indication when it will be fully implemented?

frasza avatar Nov 24 '22 21:11 frasza

@pi0

Yes. You need to pass full URLs because dev proxy is outside of worker and needs to make an actual request.

But this breaks code unification between client and server. In browser, when I make a /api/xxx request, browser will resolve it to my domain automatically. So it's natural to assume same behavior in SSR context - relative requests should finally end up being proxied. Or resolved to target URLs via proxy config and sent directly to backend.

Otherwise, you either need to add another layer ontop of fetch, or you will end up with a lot of boilerplate for converting URLs to absolute.

makkarpov avatar Nov 24 '22 21:11 makkarpov

It's also desirable (but far less annoying) to proxy WebSocket requests for same reason. This time things are even more complicated: you have to invent ad-hoc logic on client. This means, you need to put all your backend paths into a runtimeConfig, check if it's dev or prod, use relative paths on prod (nginx is completely fine with websockets), match correct backend on dev, build an URL.

makkarpov avatar Nov 24 '22 21:11 makkarpov

Web socket layer and runtime proxy support (with direct fetch ability) are on the way :)

pi0 avatar Nov 25 '22 09:11 pi0

Web socket layer and runtime proxy support (with direct fetch ability) are on the way :)

Available yet?

PavlosMac avatar Dec 06 '22 19:12 PavlosMac

Will this be available soon? I can't send auth requests to my backend because of it. Before this is integrated, I can't use Nuxt 3.

Arturexe avatar Dec 08 '22 08:12 Arturexe

Some ETA would me much appreciated. Our team is also waiting for this.

bogumilkorek avatar Dec 09 '22 22:12 bogumilkorek

Any news on it?

paulvonber avatar Dec 11 '22 12:12 paulvonber

bumping this every day certainly will not make it faster, plus it annoys everyone who subscribed to this issue for actual updates, so please, just don't.

MiniDigger avatar Dec 12 '22 09:12 MiniDigger

While you wait, you can follow pi0's earlier advice: https://github.com/unjs/nitro/issues/113#issuecomment-1180621076

In the meantime you can use https://www.npmjs.com/package/nuxt-proxy for Nuxt 3 apps only keep in mind some options are probably going to be simplified compating to http-proxy so avoid using options when possible to reduce breaking change chances.

scherii avatar Dec 13 '22 08:12 scherii

Commenting here in case this info is useful to anyone. For some reason, I couldn't get vite proxy to match any of the regex I was writing.

After investigation, it seems like all the routes tested by vite are prefixed with __url. This means that if I want to proxy ^/vegetables/.*, I actually need to write the following route in the vite server proxy options: ^/__url/vegetables/.*.

For context, the configured root route on my project is a catch-all, (pages/[...slug].ts).

michauds avatar Jan 19 '23 14:01 michauds

Runtime proxy support is finally possible via https://github.com/unjs/nitro/pull/926 (in nitropack-edge and upcoming 2.1.0 release). Cheers!

pi0 avatar Feb 08 '23 09:02 pi0