create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

local proxy is not working with href elements

Open bidorboocrew opened this issue 7 years ago • 6 comments

Is this a bug report?

Yes - local development proxy

Steps to Reproduce

  1. I was using an older v of create react app with scripts "react-scripts": "1",
  2. I decided to upgrade and use the latest "react-scripts": "^2.1.1",
  3. After the upgrade it seems that the new dev proxy is no longer works with my existing hrefs that used to work with the older dev proxy

for example I hade an href button that used to trigger google authentication flow : <a rel="noopener noreferrer" href={'/api/auth/google'}>Google login </a>

clicking this href used to hit my end point as expected : and I use passport/nodejs express on the backend and used to and that used to work in development mode and hit my end point app.get('/api/auth/google', (req, res, next) => { ....

but after the upgrade , which brought in a new v of the dev proxy . these hrefs are no longer hit the end point but rather staying on the same page

here is my proxy change in my package.json after the upgrade of my react scripts image

Expected Behavior

clicking the href tag should behave like it did before upgrading the scripts / the dev proxy . meaning it should be able to interpert the href /api .... and redirect just as it would with fetch requests

Actual Behavior

in development mode clicking the href doesnt get redirected to the backend server but rather stays on the client side and the react router tries to interpert it causing it to fallback to my catch all route '/'

bidorboocrew avatar Dec 13 '18 09:12 bidorboocrew

I have never seen proxy affecting anchor elements. Could you provide a documentation link for that?

miraage avatar Dec 13 '18 12:12 miraage

Actually After further investigation I think I know WHY is this happening :

when I click on the href Here is the get request from the network tab : image

if you notice Href click issues a Get request with Accept header set to text/html

and in the new create-react-app the PROXY implementation says : https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development image

This was NOT the case with the older implementation of the development proxy ,

so unfortunately I think I will need to change my hrefs and figure a way to do get requests to get the express/passport google/facebook auth flow working again

do you have any better alternative or suggestion on what may I do to handle this without needing to change the code much ?

bidorboocrew avatar Dec 13 '18 21:12 bidorboocrew

Same error here, it still routes to localhost:3000/api/auth/google, my CRA version is 2.1.3 It seems http-proxy-middleware is the only working way. I have to Configuring the Proxy Manually

zyh41 avatar Jan 09 '19 08:01 zyh41

Had the same issue with href element while it was working fine with other axios request using string proxy notation in the package.json. However, using setupProxy.js was able to fix the proxy issue for href elements too.

Sine99 avatar Feb 01 '19 20:02 Sine99

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

stale[bot] avatar Mar 03 '19 20:03 stale[bot]

This issue has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue.

stale[bot] avatar Mar 08 '19 21:03 stale[bot]