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

feat: support runtime proxy using route rules #926

Merged
merged 5 commits into from
Feb 8, 2023
Merged

feat: support runtime proxy using route rules #926

merged 5 commits into from
Feb 8, 2023

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Feb 8, 2023

πŸ”— Linked issue

#113

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Support runtime proxy using route rules and h3.proxyRequest (universally works with fetch)

{
  routeRules: {
    '/proxy/example': { proxy: 'https://example.com' },
    "/proxy/**": { proxy: '/api/**' },
  }
}

Local support is pending unjs/h3#321

For more advance usages, users can always create a route like routes/proxy/[...path].ts and directly use return proxyRequest(path, { target }) and have full control over path handling, etc.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Feb 8, 2023

Codecov Report

Merging #926 (9506781) into main (aa5a2f4) will increase coverage by 0.32%.
The diff coverage is 54.54%.

@@            Coverage Diff             @@
##             main     #926      +/-   ##
==========================================
+ Coverage   67.40%   67.73%   +0.32%     
==========================================
  Files          59       59              
  Lines        5968     5979      +11     
  Branches      668      678      +10     
==========================================
+ Hits         4023     4050      +27     
+ Misses       1936     1920      -16     
  Partials        9        9              
Impacted Files Coverage Ξ”
src/options.ts 87.74% <37.50%> (-1.18%) ⬇️
src/types/nitro.ts 100.00% <100.00%> (ΓΈ)
src/rollup/config.ts 92.59% <0.00%> (+0.46%) ⬆️
src/rollup/plugins/externals.ts 94.32% <0.00%> (+1.17%) ⬆️
src/utils/index.ts 54.22% <0.00%> (+6.46%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pi0 pi0 merged commit 4d8cda3 into main Feb 8, 2023
@pi0 pi0 deleted the feat/proxy-rule branch February 8, 2023 09:29
@pi0 pi0 mentioned this pull request Feb 8, 2023
@yancyhuang
Copy link

Hi @pi0

Very wonderful feature!
BTW, does it support proxy web socket now? In ticket #113 you mentioned web socket proxy feature is on the way, not sure if it's released or not.

Thank you,
Yancy

@pi0
Copy link
Member Author

pi0 commented Feb 24, 2023

@yancyhuang It is partially with devProxy options. We still need a unified layer for WS support in servers to make it happen.

@antlionguard
Copy link

Will path rewrite support come? Like devProxy.

rewrite: (path) => path.replace(/^\/api/, '')

or i'm missing something about this topic?

@pi0
Copy link
Member Author

pi0 commented May 16, 2023

@antlionguard Something like this has same effect in route rules: "/proxied/api/**": "https://backend/**.

@jonkri
Copy link

jonkri commented May 16, 2023

We still need a unified layer for WS support in servers to make it happen.

Hi! πŸ‘‹ Is there an issue that tracks this?

@blowsie
Copy link

blowsie commented Dec 6, 2023

@pi0 , Thanks for this.
Unless I missed something, whilst his feat is called "support runtime proxy using route rules", it only actually supports rules that are defined at build time.

Would it be possible to acheive something like this using variables?

export default defineNuxtConfig({
  routeRules: {
    '/api': { proxy: NUXT_API_PROXY },
  }
})

@pi0
Copy link
Member Author

pi0 commented Dec 6, 2023

@blowsie For dynamic target, you should make a custom route handler and use sendProxy(event) utility. (please make an issue if wanted to know how, i might forget to respond here)

@imzedi
Copy link

imzedi commented Jan 12, 2024

@pi0, any way I can use the same for static build? (by ssr: false and "nuxt build --prerender)

@manniL
Copy link
Contributor

manniL commented Jan 12, 2024

@imzedi With a static build, you platform or reverse proxy has to handle that as no "nuxt/nitro" server is running ☺️

@pi0
Copy link
Member Author

pi0 commented Jan 12, 2024

BTW depends on the static deployment platform. One of proxy via route rule advantages is that we can technically generate platform (or reverse proxy) config.

@manniL
Copy link
Contributor

manniL commented Jan 12, 2024

@pi0 e.g. for netlify you mean?

@pi0
Copy link
Member Author

pi0 commented Jan 12, 2024

Yes like this we can use for static presets.

@imzedi
Copy link

imzedi commented Jan 12, 2024

Thanks @pi0 @manniL .
Got this whole point.
Anyhow we will need a server whether its CDN or serverless function provided by deployment platform or we manage our own.

@JoniLieh
Copy link

Hello, thanks for maintaining such an awesome library!
Is there support to proxy websockets like socket.io?
I'm using nuxt and this works, but not for websockets (even with ws: true)

"/socket.io/**": {
        proxy: {
          to: "http://backend:3001/**",
          headers: {
            "X-API-KEY": NUXT_API_SECRET,
          },
}

Thanks!

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

Successfully merging this pull request may close these issues.

8 participants