Skip to content

Replace advanced proxy with user provided proxy.js (#3366) #3845

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

Closed
wants to merge 1 commit into from

Conversation

iansu
Copy link
Contributor

@iansu iansu commented Jan 17, 2018

Remove "advanced proxy" object from package.json, replace with user provided proxy.js.

Closes #3366

@iansu
Copy link
Contributor Author

iansu commented Jan 17, 2018

Work in progress on new proxy.

TODO

  1. Figure out what exactly needs to be passed to user function to generate proxy config. Everything being passed now might not be necessary or there might be a better way to pass it (config object?)
  2. Possibly reload app when proxy.js changes (this is probably very difficult as the proxy is part of the webpack dev server config
  3. Clean up comments and error messages
  4. Test various proxy configs

@iansu
Copy link
Contributor Author

iansu commented Jan 17, 2018

This is the proxy.js I'm using for testing:

module.exports = function(paths, resolveLoopback, mayProxy, onProxyError) {
  let target;
  if (process.platform === 'win32') {
    target = resolveLoopback('http://localhost:8000');
  } else {
    target = 'http://localhost:8000'
  }

  return [
    {
      path: '/api/**/*',
      target,
      logLevel: 'silent',
      context: function(pathname, req) {
        return (
          req.method !== 'GET' ||
          (mayProxy(pathname) &&
            req.headers.accept &&
            req.headers.accept.indexOf('text/html') === -1)
        );
      },
      onProxyReq: proxyReq => {
        // Browers may send Origin headers even with same-origin
        // requests. To prevent CORS issues, we have to change
        // the Origin to match the target URL.
        if (proxyReq.getHeader('origin')) {
          proxyReq.setHeader('origin', target);
        }
      },
      onError: onProxyError(target),
      secure: false,
      changeOrigin: true,
      ws: true,
      xfwd: true
    }
  ];
};

@FezVrasta
Copy link
Contributor

FezVrasta commented Jan 19, 2018

I don't think this allows to proxy the root path, right?

I'd like to proxy the root path and serve the CRA from a sub directory

I have described how to make it work here:
#3366 (comment)

@donverduyn
Copy link

Any progress on this?

@yyfearth
Copy link

yyfearth commented Mar 11, 2018

@iansu As I commented on the #3366
I suggest to put the filename proxy.js into the proxy config of the package.json.
So the filename do not have to be hardcoded, and user have the freedom to chose other names.

@ng-parth
Copy link

Any updates? Any idea if this PR is going to be accepted?

@iansu
Copy link
Contributor Author

iansu commented Oct 2, 2018

Closing this as it was superseded by #5073

@iansu iansu closed this Oct 2, 2018
@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
@iansu iansu deleted the proxy-js branch October 18, 2019 05:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants