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

Add resource hints for reCAPTCHA #10616

Merged
merged 2 commits into from
May 14, 2024
Merged

Add resource hints for reCAPTCHA #10616

merged 2 commits into from
May 14, 2024

Conversation

aduth
Copy link
Contributor

@aduth aduth commented May 13, 2024

🛠 Summary of changes

Adds resource hints to the response headers when loading reCAPTCHA, to improve load times.

Related documentation: https://cloud.google.com/recaptcha-enterprise/docs/using-features#instrument-site

(Optional) To improve the loading performance of reCAPTCHA Enterprise, do the following:
[...]
2. Add the following resource hints in the <head> tag of pages that load enterprise.js:

  • <link rel="preconnect" href="https://www.google.com">
  • <link rel="preconnect" href="https://www.gstatic.com" crossorigin>

The changes here add the resource hints as a response header, which allow the browser to resolve the hint before parsing page markup.

📜 Testing Plan

Prerequisite: You'll need valid reCAPTCHA keys. In local development, you can use keys generated yourself with the free version of reCAPTCHA:

  1. Go to https://www.google.com/recaptcha/admin
  2. Create keys for local development as a "Score based (v3)" type
  3. Update configuration in config/application.yml, adding your site and secret keys
    recaptcha_site_key: '[insert-v3-site-key]'
    recaptcha_secret_key: '[insert-v3-secret-key]'
    

Verify response header is present on pages where reCAPTCHA is loaded:

  1. Go to http://localhost:3000
  2. Sign in
  3. From account dashboard, click "Add phone number"
  4. Use browser developer tools network tab to inspect response headers for the page ("document") request (see Chrome DevTools reference)
  5. See "link:" response header includes <https://www.google.com> and <https://www.gstatic.com> entries

changelog: Internal, Spam Mitigation, Add resource hints to improve load speed for reCAPTCHA
Co-authored-by: Zach Margolis <zachmargolis@users.noreply.github.com>
Comment on lines +17 to +18
'<https://www.google.com>;rel=preconnect',
'<https://www.gstatic.com>;rel=preconnect;crossorigin',
Copy link
Contributor Author

@aduth aduth May 14, 2024

Choose a reason for hiding this comment

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

One thing I didn't check previously is whether it's valid to omit the quotes on the values here, since both Rails and related MDN resources include them.

I confirmed this in the relevant spec: https://httpwg.org/specs/rfc8288.html#header

The ABNF for the field value is:

 Link       = #link-value
 link-value = "<" URI-Reference ">" *( OWS ";" OWS link-param )
 link-param = token BWS [ "=" BWS ( token / quoted-string ) ]

Note that any link-param can be generated with values using either the token or the quoted-string syntax; therefore, recipients MUST be able to parse both forms. In other words, the following parameters are equivalent:

 x=y
 x="y"

Adding on to my comment at #10612 (comment), this could be another micro-optimization if we wanted to fully control the resource hint header and optimize its size. Edit: Turns out, Rails omits them too!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also validated them as working in Safari (related blog post):

image

@aduth aduth merged commit b6ef0cd into main May 14, 2024
2 checks passed
@aduth aduth deleted the aduth-preconnect-recaptcha branch May 14, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants