Skip to content

add Rack::Request#prefetch? helper#2405

Merged
ioquatix merged 1 commit into
rack:mainfrom
glaszig:prefetch-helper
Nov 2, 2025
Merged

add Rack::Request#prefetch? helper#2405
ioquatix merged 1 commit into
rack:mainfrom
glaszig:prefetch-helper

Conversation

@glaszig

@glaszig glaszig commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

allows to effortlessly identify prefetch request as defined by WICG. inspects the Sec-Purpose header as defined in specification 0.

the usefulness is probably best explained by the following quote from the original fetch draft 1:

"The server can use this to adjust the caching expiry for prefetches, to disallow the prefetch, or to treat it differently when counting page visits."

also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Purpose

this header has quite a history. i thought about supporting more permutations but settled on what's in the spec.

@jeremyevans jeremyevans left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not in favor of supporting the X-Sec-Purpose header. Seems like you would only want that if you were using Turbo, since it is non-standard. Absent backwards compatibility issues (which we don't have here as this would be a new method), I'd rather Rack support only standard headers by default.

@glaszig glaszig force-pushed the prefetch-helper branch 2 times, most recently from d514856 to 47883ab Compare October 29, 2025 06:55
@glaszig

glaszig commented Oct 29, 2025

Copy link
Copy Markdown
Contributor Author

reasonable. can be mapped in a middleware. i removed the x- version.

for reference, the reason turbo uses x-sec-purpose is that brower's fetch() implementations don't allow to override sec-purpose: hotwired/turbo#1107.

@jeremyevans jeremyevans requested a review from ioquatix October 29, 2025 14:13
@glaszig

glaszig commented Oct 29, 2025

Copy link
Copy Markdown
Contributor Author

@rafaelfranca to get this working with turbo's x-sec-purpose where would be a good place?

  • rails itself via AD::Request#prefetch? override?
  • turbo-rails gem via new helper turbo_prefetch? which exclusively checks x-sec-purpose?
  • turbo-rails gem via mapping to sec-purpose in some middleware?

i personally sympathize with option 1 or 2.

@ioquatix

Copy link
Copy Markdown
Member

Can you please add a note to the changelog.

@glaszig

glaszig commented Nov 1, 2025

Copy link
Copy Markdown
Contributor Author

add a note to the changelog

done.

allows to effortlessly identify prefetch request as defined by WICG.
inspects the `Sec-Purpose` header as defined in specification [0].

the usefulness is probably best explained by the following quote
from the original fetch draft [1]:

"The server can use this to adjust the caching expiry for prefetches, to
disallow the prefetch, or to treat it differently when counting page
visits."

[0]: https://wicg.github.io/nav-speculation/prefetch.html#sec-purpose-header
[1]: https://fetch.spec.whatwg.org/#http-sec-purpose
also: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Purpose
@ioquatix ioquatix merged commit 0b17223 into rack:main Nov 2, 2025
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.

4 participants