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

ky sending empty request when evaluated by PageSpeed #490

Closed
gajus opened this issue Feb 21, 2023 · 2 comments
Closed

ky sending empty request when evaluated by PageSpeed #490

gajus opened this issue Feb 21, 2023 · 2 comments

Comments

@gajus
Copy link

gajus commented Feb 21, 2023

This has been one of the more confusing debugging experiences.

I will provide context as it may help others.

We tried using https://pagespeed.web.dev/ to measure our page performance.

Our website produces good Lighthouse score when tested using Lighthouse built into Chrome, but we were tipped that it is failing PageSpeed test.

Upon investigating, we discovered that PageSpeed is failing with an error

Screen Shot 2023-02-21 at 11 02 47 AM

However, nothing immediately obvious surfaced as to why it would be failing, i.e. nothing in Sentry other than the request is failing.

Then we noticed that around the same time that we run PageSpeed test, our load balancer is returning 400 error.

POST /api/                                                   400 Bad Request
GET  /static/assets/GlassyContraLogo.37dd3240.webp           200 OK
POST /api/                                                   400 Bad Request

Further investigation led to discover that ky is sending an empty request to our server.

However,

  • no errors are surfaced to the application
  • our app worked fine when using legacy browsers (all the way down to Chrome 78)
  • Ligthhouse built into browser did not produce the same behavior

The request itself is pretty simple:

const headers: Record<string, string> = {
  accept: 'application/json',
  'content-type': 'application/json',
  'x-contra-client-url': window.location.href,
  'x-contra-release-version': releaseVersion ?? '',
};

if (window.posthogSessionId) {
  headers['x-posthog-session-id'] = window.posthogSessionId;
}

const response = await ky(apiUrl, {
  body: safeStringify({
    query: operation.text,
    variables: clonedVariables,
  }),
  credentials: 'include',
  headers,
  method: 'POST',
});

const json = await response.json<GraphQLResponse>();

We ended up replacing ky with fetch and it started working again.

TLDLR something is quietly failing when PageSpeed (whatever client they are using) is running code that uses ky.

@sholladay
Copy link
Collaborator

Is this still happening with latest Ky?

@sholladay
Copy link
Collaborator

My best guess is that this is related to request.clone(), which tends to be problematic for non-browser environments.

PR #601 released in Ky 1.4.0 attempts to address this to some degree by reducing our reliance on request.clone() and hopefully that has improved the situation. Going to close this for now. Please let me know if anyone else runs into this.

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

No branches or pull requests

2 participants