Skip to content

Commit

Permalink
Build and register service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
jellybob committed Apr 1, 2024
1 parent 3434901 commit 9104967
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
12 changes: 12 additions & 0 deletions apps/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def favicon():
)


# Service worker has to be served from the route so that it can take
# control of all pages, otherwise it only has control over files in
# /js.
@base.route("/serviceworker.js")
def serviceworker():
return send_from_directory(
os.path.join(app.root_path, "static/js"),
"serviceworker.js",
mimetype="application/javascript",
)


@base.route("/404")
def raise_404():
abort(404)
Expand Down
14 changes: 11 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ const main_js = (cb) => pump(jsBuild('main.js'), cb),
line_up_js = (cb) => pump(jsBuild('line-up.js'), cb),
schedule_js = (cb) => pump(jsBuild('schedule.js'), cb),
volunteer_schedule_js = (cb) => pump(jsBuild('volunteer-schedule.js'), cb),
arrivals_js = (cb) => pump(jsBuild('arrivals.js'), cb);
arrivals_js = (cb) => pump(jsBuild('arrivals.js'), cb),
serviceworker_js = (cb) => pump(jsBuild('serviceworker.js'), cb),


function js(cb) {
gulp.parallel(main_js, line_up_js, schedule_js, volunteer_schedule_js, arrivals_js)(cb);
gulp.parallel(
main_js,
line_up_js,
schedule_js,
volunteer_schedule_js,
arrivals_js,
serviceworker_js
)(cb);
}

function css(cb) {
Expand All @@ -109,7 +117,7 @@ function css(cb) {
'css/schedule.scss',
'css/volunteer_schedule.scss',
'css/flask-admin.scss',
'css/dhtmlxscheduler_flat.css',
'css/dhtmlxscheduler_flat.css'
]),
gulpif(!production, sourcemaps.init()),
sass({ includePaths: ['../node_modules'] }).on('error', function (err) {
Expand Down
18 changes: 17 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,20 @@ $(() => {
return false;
});
});
});
});

async function registerServiceWorker() {
if (!("serviceWorker" in navigator)) {
return
}

try {
await navigator.serviceWorker.register("/serviceworker.js", {
scope: "/",
});
} catch (error) {
console.error("Service worker registration failed:", error);
}
}

registerServiceWorker()
2 changes: 1 addition & 1 deletion js/serviceworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ registerRoute(
}),
],
}),
);
);

0 comments on commit 9104967

Please sign in to comment.