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

fix the dev server issue when proxy server returns 304 while redirect status is force set to 200 #865

Closed

Conversation

bencao
Copy link

@bencao bencao commented Apr 27, 2020

- Summary

currently if we have a redirect rule in netlify.toml with
force = true and status = 200
the response from proxy server will always be 200
this behavior is problematic when the proxy returns 304 with empty body
the browser will always render a blank page
the change proposed in this PR is to leave 3xx code back to the browser and never override

- Test plan

Before: Page Request return Status 200 with empty Body

Screen Shot 2020-04-27 at 1 43 26 PM

Screen Shot 2020-04-27 at 1 43 34 PM

After: Page Request return Status 304

Screen Shot 2020-04-27 at 1 42 10 PM

- Description for the changelog

when proxy server returns status code between 300 and 399, the return status will honor proxy server status instead of using the one designated in redirection rule.

currently if we have a redirect rule in netlify.toml with
force = true and status = 200
the response from proxy server will always be 200
this behavior is problematic when the proxy returns 304 with empty body
the browser will always render a blank page

the fix is to leave 3xx code back to the browser and never override
@bencao bencao changed the title fix the issue when proxy server returns 304 while redirect status is force set to 200 fix the dev server issue when proxy server returns 304 while redirect status is force set to 200 Apr 27, 2020
@@ -132,7 +132,12 @@ function initializeProxy(port, distDir, projectDir) {
if (!isEmpty(pathHeaderRules)) {
Object.entries(pathHeaderRules).forEach(([key, val]) => res.setHeader(key, val))
}
res.writeHead(req.proxyOptions.status || proxyRes.statusCode, proxyRes.headers)
// leave 3xx redirects handling to browser
if (proxyRes.statusCode >= 300 && proxyRes.statusCode < 400) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I'm considering we probably should always return the proxyRes.statusCode, no matter it is 3xx or 4xx or 5xx.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening this PR @bencao. The reason we have req.proxyOptions.status is to force a certain status code, for example, 200! or 404!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configuration like this is a bit vague, what if the api server returns http 500?

[[redirects]]
  from = "/search"
  to = "https://api.mysearch.com"
  status = 200
  force = true

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a user, my actual intention is basically requesting a server side transparent proxy, not enforcing the status code to 200

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would still enforce 200 as status

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.. do you have some ideas about how we can gracefully solve the above proxy server 304 resulted in blank page issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try out this branch #843 and see if that fixes the problem for you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the branch, it seems to be a bit too complicated to solve my very specific problem..

@bencao
Copy link
Author

bencao commented Apr 30, 2020

close and waiting for a better approach

@bencao bencao closed this Apr 30, 2020
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.

2 participants