Skip to content

Commit

Permalink
Switch to navigator.sendBeacon() for unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Jun 6, 2020
1 parent 34cddaf commit 8665aa6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion appmode/server_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def get(self, path):
#===========================================================================
@web.authenticated
@gen.coroutine
def delete(self, path):
def post(self, path):
assert self.get_body_arguments("appmode_action")[0] == "delete"
path = path.strip('/')
self.log.info('Appmode deleting: %s', path)

Expand Down
14 changes: 12 additions & 2 deletions appmode/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ define([
) {
"use strict";

//==========================================================================
var get_cookie = function (name) {
// from tornado docs: http://www.tornadoweb.org/en/stable/guide/security.html
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : undefined;
}

//==========================================================================
var appmode_unload_handler = function (e) {
var nb = Jupyter.notebook;
var url_parts = [nb.base_url, 'apps', nb.notebook_path];
var url = utils.url_path_join.apply(null, url_parts);

// tell server to clean up session, kernel, and tmp notebook file.
utils.ajax(url, {cache: false, type: "DELETE", async: false});
var form_data = new FormData();
form_data.append("appmode_action", "delete");
form_data.append("_xsrf", get_cookie("_xsrf"));
navigator.sendBeacon(url, form_data);
};

//==========================================================================
Expand Down Expand Up @@ -123,7 +133,7 @@ define([
//==========================================================================
function initialize_step4() {
// install unload-handler
window.onbeforeunload = appmode_unload_handler;
window.addEventListener('unload', appmode_unload_handler, false);

// disable autosave
Jupyter.notebook.set_autosave_interval(0);
Expand Down

0 comments on commit 8665aa6

Please sign in to comment.