Skip to content

Commit

Permalink
Fixes for XML types, such as the sitemap.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdoms committed Apr 8, 2020
1 parent 842a922 commit b4d5b87
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions controllers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ def securityHeaders(self):
self.set_header('Content-Security-Policy', CSP)

def renderTemplate(self, filename, **kwargs):
if filename.endswith('.xml'):
self.set_header('Content-Type', 'application/xml')

if self.request.method != 'HEAD':
self.securityHeaders()
# could have compile call `self.render` but it looks like a lot of extra processing we don't need
Expand Down
2 changes: 1 addition & 1 deletion controllers/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class SitemapController(BaseController):
""" handles generating a sitemap """

@helpers.cacheAndRender()
@helpers.cacheAndRender(content_type='application/xml')
async def get(self):
# FYI: sitemaps can only have a max of 50,000 URLs or be 10 MB each
base_url = self.request.protocol + "://" + self.request.host
Expand Down
5 changes: 4 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ async def decorate(*args, **kwargs):
await action(*args, **kwargs)
html = b"".join(controller._write_buffer).decode()

html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True)
remove_quotes = content_type != 'application/xml'

html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True,
remove_optional_attribute_quotes=remove_quotes)

if not controller.debug:
cache(key, lambda: html)
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Trestle

Copyright © 2019, [Brendan Doms](https://www.bdoms.com/)
Copyright © 2019-2020, [Brendan Doms](https://www.bdoms.com/)
Licensed under the [MIT license](http://www.opensource.org/licenses/MIT)

A jumping off point for building modern web apps.

Uses Python 3, Tornado, and Peewee on the back end, with Vue, Svelte, or no JaaScript on the front end.
Uses Python 3, Tornado, and Peewee on the back end, with Vue, Svelte, or no JavaScript on the front end.

## Setup

Expand Down Expand Up @@ -68,7 +68,7 @@ yarn install
Make sure postgres is installed (the last dependency on here is for psycopg2 support):

```bash
sudo apt install postgresql nginx supervisor libpq-dev
sudo apt install postgresql libpq-dev
```

Then get into postgres:
Expand Down
1 change: 1 addition & 0 deletions tests/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ class TestSitemap(BaseTestController):
def test_sitemap(self):
response = self.fetch('/sitemap.xml')
assert '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' in response.body_string
assert response.headers.get('Content-Type') == 'application/xml'


class TestStatic(BaseTestController):
Expand Down

0 comments on commit b4d5b87

Please sign in to comment.