Last active
August 29, 2015 14:19
-
-
Save dcousineau/7483fb945725ad9de3b9 to your computer and use it in GitHub Desktop.
How to initialize cookies for Safari when your app lives in an Iframe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cookie from 'cookie-cutter'; | |
App.on('before:start', () => { | |
//Safari does not respect P3P policies by default and blocks all 3rd party cookies (which is what our cookie is when | |
//loaded in an Iframe). To work around this we need to open a window to our application and set the cookies then | |
//close it. Safari allows us to interact with cookies that have already been set (but not create new ones). | |
if (cookie.get('expected_cookie') === undefined) { | |
//However, Safari (like all browsers) puts the kibosh on all windows that open without user interaction! | |
//Therefore we intercept all clicks to open the short-lived window that initializes all of our cookies. | |
$(document.body).one('click', '[data-goto]', e => { | |
let initListener = e => { | |
if (e.data === 'cookies:success') { | |
window.removeEventListener('message', initListener); | |
//Turns out we don't need to reload the iframe to work with these cookies. We should turn this back | |
//on if it ever becomes a problem | |
//window.location.href = window.location.href; | |
} | |
}; | |
window.addEventListener('message', initListener); | |
window.open(location.origin + '/_init', 'initCookies', 'width=200, height=100'); | |
}); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from application import app | |
from flask import render_template, make_response | |
@app.route("/_init", methods=['GET']) | |
def init_cookies(): | |
""" | |
Initialize all cookies your app will ever need | |
""" | |
resp = make_response(render_template("init_cookies.jinja.html")) | |
resp.set_cookie('expected_cookie', value='') | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment