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

Serve static pages from app.yaml #6974

Merged
merged 22 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0bc53bb
create render template method
jameesjohn Jun 19, 2019
df266a2
address review comments
jameesjohn Jun 21, 2019
8b63d34
Merge branch 'develop' of https://github.com/oppia/oppia into static-…
jameesjohn Jun 21, 2019
9106197
address review comments
jameesjohn Jun 21, 2019
6d2cdb9
fix error due to merge
jameesjohn Jun 21, 2019
77fc41d
address review comments
jameesjohn Jun 22, 2019
e3f24b7
serve statics files from app.yaml
jameesjohn Jun 23, 2019
fff201a
address review comments
jameesjohn Jun 24, 2019
9baa459
more on review comments and test issue
jameesjohn Jun 24, 2019
3458a6b
Merge branch 'develop' of https://github.com/oppia/oppia into static-…
jameesjohn Jun 29, 2019
6df30d8
add hases to files in app.yaml
jameesjohn Jun 29, 2019
a209b4e
Merge branch 'develop' of https://github.com/oppia/oppia into static-…
jameesjohn Jul 2, 2019
7ccbf19
create dev app.yaml and prod app.yaml as well as write tests for the …
jameesjohn Jul 3, 2019
04bd338
address review comments and revert package-lock.json
jameesjohn Jul 3, 2019
f27d23b
address review comments
jameesjohn Jul 4, 2019
c6617b5
revert change in constants
jameesjohn Jul 4, 2019
81922b9
remove app.yaml
jameesjohn Jul 4, 2019
9104bca
work on failing e2e tests
jameesjohn Jul 5, 2019
abddd81
fix lint issues
jameesjohn Jul 5, 2019
33c6f79
revert package-lock.json
jameesjohn Jul 5, 2019
d99be7c
Merge branch 'develop' of https://github.com/oppia/oppia into static-…
jameesjohn Jul 6, 2019
bd7eca5
remove other variants of splash page
jameesjohn Jul 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
serve statics files from app.yaml
  • Loading branch information
jameesjohn committed Jun 23, 2019
commit e3f24b7030f824be174422ca116f01a8fed2e260
68 changes: 68 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,74 @@ handlers:
login: admin
script: main_mail.app
secure: always

# Serve static pages.
jameesjohn marked this conversation as resolved.
Show resolved Hide resolved
- url: /splash?c=at1
static_files: core/templates/dev/head/dist/splash_at1.html
upload: core/templates/dev/head/dist/splash_at1.html
http_headers:
Cache-Control: 'no-cache, must-revalidate'
Expires: 'Mon, 01 Jan 1990 00:00:00 GMT'
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can remove Cache-Control and Expires, because expiration sets it automatically, but please check it in your browser dev console.

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 they are set automatically, but not with the same values
Cache-Control is set to public while Expires is set to the current date

Copy link
Contributor

Choose a reason for hiding this comment

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

That's okay in this case. Since the pages are now static we can cache them for 0 seconds and save few KiBs by using cache validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

Choose a reason for hiding this comment

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

One question about this -- if we update the page, will the updates be displayed correctly to the user or will they get a cached version? I don't see any build hashes, so just wanted to double-check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll need to add the regex for that

Copy link
Member

Choose a reason for hiding this comment

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

(To clarify, my previous question was more for @vojtechjelinek)

Copy link
Contributor

Choose a reason for hiding this comment

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

Because the Expires will always be in the past, the browser will do a check if the file is the same as in the cache (by using hashes). If the file is different the new version will be sent, if the file is the same the cache version will be used.

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok, thanks! But that is the part I don't understand, since the URL is currently hardcoded. How would we use hashes in that case?

Also, I was just wondering -- why this is placed in the "dynamic" section of app.yaml when these are meant to be statically served?

Copy link
Contributor

Choose a reason for hiding this comment

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

Everything will be handled by the HTTP. To simplify it: if the cache is not expired the cached version is used, if the cache is expired the HTTP makes an check call, if the file is the same 302 is returned if the file is different 200 is returned together with the new file.

Pragma: no-cache
Strict-Transport-Security: 'max-age=31536000; includeSubDomains'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: '1; mode=block'
secure: always
expiration: "0"
- url: /splash?c=at0
static_files: core/templates/dev/head/dist/splash_at0.html
upload: core/templates/dev/head/dist/splash_at0.html
http_headers:
Cache-Control: 'no-cache, must-revalidate'
Expires: 'Mon, 01 Jan 1990 00:00:00 GMT'
Pragma: no-cache
Strict-Transport-Security: 'max-age=31536000; includeSubDomains'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: '1; mode=block'
secure: always
expiration: "0"
- url: /splash
static_files: core/templates/dev/head/dist/splash-page.mainpage.html
upload: core/templates/dev/head/dist/splash-page.mainpage.html
http_headers:
Cache-Control: 'no-cache, must-revalidate'
Expires: 'Mon, 01 Jan 1990 00:00:00 GMT'
Pragma: no-cache
Strict-Transport-Security: 'max-age=31536000; includeSubDomains'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: '1; mode=block'
secure: always
expiration: "0"
- url: /get_started
static_files: core/templates/dev/head/dist/get-started-page.mainpage.html
upload: core/templates/dev/head/dist/get-started-page.mainpage.html
http_headers:
Cache-Control: 'no-cache, must-revalidate'
Expires: 'Mon, 01 Jan 1990 00:00:00 GMT'
Pragma: no-cache
Strict-Transport-Security: 'max-age=31536000; includeSubDomains'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: '1; mode=block'
secure: always
expiration: "0"
- url: /teach
static_files: core/templates/dev/head/dist/teach-page.mainpage.html
upload: core/templates/dev/head/dist/teach-page.mainpage.html
http_headers:
Cache-Control: 'no-cache, must-revalidate'
Expires: 'Mon, 01 Jan 1990 00:00:00 GMT'
Pragma: no-cache
Strict-Transport-Security: 'max-age=31536000; includeSubDomains'
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: '1; mode=block'
secure: always
expiration: "0"
jameesjohn marked this conversation as resolved.
Show resolved Hide resolved
jameesjohn marked this conversation as resolved.
Show resolved Hide resolved

- url: /.*
script: main.app
secure: always
Expand Down
64 changes: 17 additions & 47 deletions core/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import utils

from google.appengine.api import users
from google.appengine.ext.webapp import template
import webapp2

app_identity_services = models.Registry.import_app_identity_services()
Expand Down Expand Up @@ -283,51 +282,6 @@ def render_downloadable_file(self, values, filename, content_type):
'attachment; filename=%s' % filename)
self.response.write(values)

def prepare_headers(self, iframe_restriction):
"""Prepares the headers for a HTML response.
Args:
iframe_restriction: str or None. Possible values are
'DENY' and 'SAMEORIGIN':

DENY: Strictly prevents the template to load in an iframe.
SAMEORIGIN: The template can only be displayed in a frame
on the same origin as the page itself.
"""
self.response.cache_control.no_cache = True
self.response.cache_control.must_revalidate = True
self.response.headers['Strict-Transport-Security'] = (
'max-age=31536000; includeSubDomains')
self.response.headers['X-Content-Type-Options'] = 'nosniff'
self.response.headers['X-Xss-Protection'] = '1; mode=block'

if iframe_restriction is not None:
if iframe_restriction in ['SAMEORIGIN', 'DENY']:
self.response.headers['X-Frame-Options'] = str(
iframe_restriction)
else:
raise Exception(
'Invalid X-Frame-Options: %s' % iframe_restriction)

self.response.expires = 'Mon, 01 Jan 1990 00:00:00 GMT'
self.response.pragma = 'no-cache'

def render_static_template(self, filepath, iframe_restriction='DENY'):
"""Prepares a Jinja-less HTML response to be sent to the client.
Args:
filepath: str. The template filepath.
iframe_restriction: str or None. Possible values are
'DENY' and 'SAMEORIGIN':

DENY: Strictly prevents the template to load in an iframe.
SAMEORIGIN: The template can only be displayed in a frame
on the same origin as the page itself.
"""
self.prepare_headers(iframe_restriction)

path = os.path.join(
feconf.FRONTEND_TEMPLATES_DIR, filepath)
self.response.write(template.render(path, None))

def render_template(self, filepath, iframe_restriction='DENY'):
"""Prepares an HTML response to be sent to the client.

Expand Down Expand Up @@ -377,7 +331,23 @@ def render_template(self, filepath, iframe_restriction='DENY'):
values['csrf_token'] = CsrfTokenManager.create_csrf_token(
self.user_id)

self.prepare_headers(iframe_restriction)
self.response.cache_control.no_cache = True
self.response.cache_control.must_revalidate = True
self.response.headers['Strict-Transport-Security'] = (
'max-age=31536000; includeSubDomains')
self.response.headers['X-Content-Type-Options'] = 'nosniff'
self.response.headers['X-Xss-Protection'] = '1; mode=block'

if iframe_restriction is not None:
if iframe_restriction in ['SAMEORIGIN', 'DENY']:
self.response.headers['X-Frame-Options'] = str(
iframe_restriction)
else:
raise Exception(
'Invalid X-Frame-Options: %s' % iframe_restriction)

self.response.expires = 'Mon, 01 Jan 1990 00:00:00 GMT'
self.response.pragma = 'no-cache'

self.response.write(
self.jinja2_env.get_template(filepath).render(**values))
Expand Down
46 changes: 0 additions & 46 deletions core/controllers/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,6 @@
import feconf


class SplashPage(base.BaseHandler):
"""Landing page for Oppia."""

@acl_decorators.open_access
def get(self):
"""Handles GET requests."""
c_value = self.request.get('c')
if not c_value:
self.render_static_template('dist/splash-page.mainpage.html')
else:
try:
self.render_static_template('dist/splash_%s.html' % c_value)
except Exception:
# Old c values may have been deprecated, in which case we
# revert to the default splash page URL. When redirecting,
# we pass any arguments along (except the c_value).
arguments = self.request.arguments()
query_suffix = '&'.join([
'%s=%s' % (arg_name, self.request.get(arg_name))
for arg_name in arguments if arg_name != 'c'])

target_url = feconf.SPLASH_URL
if query_suffix:
target_url += '?%s' % query_suffix
self.redirect(target_url)
return


class AboutPage(base.BaseHandler):
"""Page with information about Oppia."""

Expand All @@ -56,24 +28,6 @@ def get(self):
self.render_template('dist/about-page.mainpage.html')


class GetStartedPage(base.BaseHandler):
"""Page with information about how to get started using Oppia."""

@acl_decorators.open_access
def get(self):
"""Handles GET requests."""
self.render_static_template('dist/get-started-page.mainpage.html')


class TeachPage(base.BaseHandler):
"""Page with information about how to teach on Oppia."""

@acl_decorators.open_access
def get(self):
"""Handles GET requests."""
self.render_static_template('dist/teach-page.mainpage.html')


class ContactPage(base.BaseHandler):
"""Page with information about how to contact Oppia."""

Expand Down
3 changes: 0 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,9 @@ def ui_access_wrapper(self, *args, **kwargs):
get_redirect_route(r'/_ah/warmup', WarmupPage),
get_redirect_route(r'/', HomePageRedirectPage),

get_redirect_route(feconf.SPLASH_URL, pages.SplashPage),
get_redirect_route(r'/about', pages.AboutPage),
get_redirect_route(r'/get_started', pages.GetStartedPage),
get_redirect_route(r'/foundation', pages.FoundationRedirectPage),
get_redirect_route(r'/credits', pages.AboutRedirectPage),
get_redirect_route(r'/teach', pages.TeachPage),
get_redirect_route(r'/participate', pages.TeachRedirectPage),
get_redirect_route(r'/site_guidelines', pages.TeachRedirectPage),
get_redirect_route(r'/console_errors', pages.ConsoleErrorPage),
Expand Down
Loading