Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Conversation

@escattone
Copy link
Contributor

This is the first of a series of PR's that add the appropriate caching headers and related tests to all Kuma endpoints as part of the effort of placing a CDN in front of MDN. This PR adds caching headers and tests for the kuma.dashboards.urls.

  • add shared_cache_control decorator as a thin wrapper around Django's cache_control decorator to encapsulate default settings for shared caching
  • add tests for shared_cache_control decorator
  • delete custom never_cache decorator, and replace with Django's
  • delete tests for custom never_cache decorator
  • decorate the views for kuma.dashboards.urls and add tests

@escattone escattone requested a review from jwhitlock February 22, 2018 20:54
* add "shared_cache_control" decorator as a thin wrapper around
  Django's "cache_control" to encapsulate default settings for
  shared caching
* add tests for "shared_cache_control" decorator
* delete custom "never_cache" decorator, and replace with Django's
* delete tests for custom "never_cache" decorator
* decorate the dashboards views and add tests
@escattone
Copy link
Contributor Author

I rebased with fixes for import ordering.

@escattone
Copy link
Contributor Author

I forgot to add in the introduction, that this PR only affects public caching (e.g., CDN's) not browser caching since by default it uses s-maxage instead of max-age.

Copy link
Contributor

@jwhitlock jwhitlock left a comment

Choose a reason for hiding this comment

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

Thanks @escattone, this looks good and works well in my development environment.

I identified a nit on import order, that we discussed in IRC. The plan is to get these changes merged, then use a flake8 plugin to standardize and enforce import order (we both seem to like the cryptography style).

There's a few more style nits, but I think this is ready to go

assert '429.html' in [t.name for t in response.templates]
assert response['Retry-After'] == '60'
assert response['Cache-Control'] == 'no-cache, no-store, must-revalidate'
assert 'Cache-Control' in response
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: if Cache-Control isn't in the response, then the next assertion will fail with a KeyError, so it isn't required to test separately, and a line can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I did this everywhere thinking it would make for a nicer error message than KeyError, but I'm happy to remove it as well.

# https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ
# Fixed in Django 1.9:
# https://docs.djangoproject.com/en/1.9/topics/http/decorators/#caching
def never_cache(view_func):
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is out-of-date, the @never_cache header was updated in Django 1.8.8, so we have Django's fixed @never_cache. Glad to take this off my to-do list ✅

permission_required, shared_cache_control)

from kuma.core.tests import KumaTestCase, eq_, ok_
from kuma.core.tests import KumaTestCase, eq_
Copy link
Contributor

Choose a reason for hiding this comment

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

Another _ok gone! 🎉



@shared_cache_control
@vary_on_headers('X-Requested-With')
Copy link
Contributor

Choose a reason for hiding this comment

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

good catch on X-Requested-With, I would have missed that



@pytest.fixture
def wiki_user_2(db, django_user_model):
Copy link
Contributor

Choose a reason for hiding this comment

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

Time to graduate to the globals! 🎓

shared_cache_control,
skip_in_maintenance_mode)

from kuma.core.tests import KumaTestCase, eq_, ok_
Copy link
Contributor

Choose a reason for hiding this comment

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

Another ok_ gone! 🎉

from .urlresolvers import reverse


def shared_cache_control(func=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: The name "shared cache" confused me a bit. Is the intent is to save a copy in the proxy, but to tell the browser to not cache the results? If so, a better name doesn't come to mind yet...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was the intent. I'm happy to use whatever name you'd prefer, as I had a difficult time coming up with something better.

Copy link
Contributor

Choose a reason for hiding this comment

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

proxy_cache_yes_browser_cache_no_with_overrides is my current best choice, which is so awful. Let's merge with this name and revisit in a few weeks when we're smarter.

@codecov-io
Copy link

Codecov Report

Merging #4676 into master will increase coverage by 0.01%.
The diff coverage is 98.54%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4676      +/-   ##
==========================================
+ Coverage   95.27%   95.29%   +0.01%     
==========================================
  Files         261      261              
  Lines       23560    23632      +72     
  Branches     1691     1698       +7     
==========================================
+ Hits        22447    22519      +72     
  Misses        902      902              
  Partials      211      211
Impacted Files Coverage Δ
kuma/wiki/tests/conftest.py 100% <ø> (ø) ⬆️
kuma/users/views.py 96.82% <100%> (+0.01%) ⬆️
kuma/core/tests/test_views.py 100% <100%> (ø) ⬆️
kuma/settings/common.py 92.6% <100%> (+0.02%) ⬆️
kuma/wiki/views/create.py 92.2% <100%> (+0.1%) ⬆️
kuma/core/views.py 95.45% <100%> (ø) ⬆️
kuma/wiki/views/edit.py 70.34% <100%> (+0.2%) ⬆️
kuma/core/decorators.py 93.61% <100%> (ø) ⬆️
kuma/conftest.py 100% <100%> (ø) ⬆️
kuma/dashboards/urls.py 100% <100%> (ø) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a29dc0a...afc82b8. Read the comment docs.

@escattone escattone merged commit 7839d19 into mdn:master Feb 23, 2018
@escattone escattone deleted the cache-control-headers-dashboards-1431259 branch February 23, 2018 17:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants