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

remove inline.js from head.html and footer.html #9177

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions openlibrary/plugins/openlibrary/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import jQueryRepeat from './jquery.repeat';
import { enumerate, htmlquote, websafe, foreach, join, len, range, jsdef_get } from './jsdef';
import initAnalytics from './ol.analytics';
import init from './ol.js';
import initServiceWorker from './service-worker-init.js'
import * as Browser from './Browser';
import { commify, urlencode, slice } from './python';
import Template from './template.js';
Expand Down Expand Up @@ -54,6 +55,9 @@ window.$ = jQuery;

window.Promise = Promise;

// Init the service worker first since it does caching
initServiceWorker();

// This to the best of our knowledge needs to be run synchronously,
// because it sends the initial pageview to analytics.
initAnalytics();
Expand Down
3 changes: 3 additions & 0 deletions openlibrary/plugins/openlibrary/js/ol.analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default function initAnalytics() {
var vs, i;
var startTime = new Date();
if (window.archive_analytics) {
// Setup analytics, depends on script loaded from CDN
window.archive_analytics.set_up_event_tracking();

window.archive_analytics.ol_send_event_ping = function(values) {
var endTime = new Date();
window.archive_analytics.send_ping({
Expand Down
17 changes: 17 additions & 0 deletions openlibrary/plugins/openlibrary/js/service-worker-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function initServiceWorker(){
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then(() => { })
.catch(error => {
// eslint-disable-next-line no-console
console.error(`Service worker registration failed: ${error}`);
});
});
}

window.addEventListener('beforeinstallprompt', (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault();
});
}
33 changes: 0 additions & 33 deletions openlibrary/templates/site/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,10 @@
$if show_ol_shell:
$:render_template("lib/nav_foot", page)

<!--Will be moved inside index.js-->
<script type="text/javascript">
if ("serviceWorker" in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register("/sw.js").then(registration => {

}).catch(error => {
console.log("ServiceWorker registration failed");
});
});
}

window.addEventListener('beforeinstallprompt', (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault();
});
</script>

<!-- Must be loaded after jquery -->
<script src="/cdn/archive.org/analytics.js" type="text/javascript"></script>
<script src="$static_url('build/all.js')" type="text/javascript"></script>
<!-- Passes total_time for analytics to ol.analytics.js -->
<div class="analytics-stats-time-calculator" data-time="$total_time"></div>

<!-- clear the window.q and setup the jQuery plugins -->
<script>
\$( function () {
var i;
for ( i = 0; i < window.q.length; i++ ) {
\$( window.q[i] );
}
window.q = [];
} );

if (typeof archive_analytics !== 'undefined') {
archive_analytics.set_up_event_tracking();
}
</script>
</body>
</html>
3 changes: 0 additions & 3 deletions openlibrary/templates/site/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@
*/

</script>

<!-- JavaScript execution queue that will be emptied at the bottom of the page -->
<script type="text/javascript">window.q = [];</script>
$if "dev" in ctx.features:
<link href="$static_url('build/page-dev.css')" rel="stylesheet" type="text/css"/>

Expand Down
Loading