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

Permit CORS preflight caching #246

Merged
merged 1 commit into from
Apr 27, 2021
Merged

Permit CORS preflight caching #246

merged 1 commit into from
Apr 27, 2021

Conversation

benlangfeld
Copy link
Collaborator

With this argument, an incrementing query parameter is added to the URL, which prevents a browser from utilising the CORS preflight cache.

The jQuery documentation states:

If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

The message_bus middleware implementation sets the correct headers to prevent the browser caching the actual response so it is unnecessary for the JavaScript client to also force a cache miss in a way that doubles request throughput in a use-case which requires CORS.

Fixes #245

With this argument, an incrementing query parameter is added to the URL, which prevents a browser from utilising the CORS preflight cache.

The jQuery documentation states:

> If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

The message_bus middleware implementation [sets the correct headers to prevent the browser caching the actual response](https://github.com/discourse/message_bus/blob/master/lib/message_bus/rack/middleware.rb#L115-L118) so it is unnecessary for the JavaScript client to also force a cache miss in a way that doubles request throughput in a use-case which requires CORS.

Fixes #245
@SamSaffron SamSaffron merged commit 5c01715 into master Apr 27, 2021
@benlangfeld benlangfeld deleted the benlangfeld-patch-1 branch April 27, 2021 11:22
benlangfeld added a commit that referenced this pull request Apr 27, 2021
#246 intended to prevent this from happening, but we only tested with jQuery, whose default for this value is `true`. Our shim for `$.ajax` defaults to `false`, so in that context the desired change didn't take effect. This shim is supposed to be lightweight to address only MessageBus' requirements, so here we remove support for the now unused `cache` option entirely, rather than flipping the default.
SamSaffron pushed a commit that referenced this pull request Apr 28, 2021
#246 intended to prevent this from happening, but we only tested with jQuery, whose default for this value is `true`. Our shim for `$.ajax` defaults to `false`, so in that context the desired change didn't take effect. This shim is supposed to be lightweight to address only MessageBus' requirements, so here we remove support for the now unused `cache` option entirely, rather than flipping the default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

CORS preflight caching impossible
2 participants