-
Notifications
You must be signed in to change notification settings - Fork 684
/
shopify_app_turbolinks.js
65 lines (53 loc) · 2.01 KB
/
shopify_app_turbolinks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* If your app is a server-side rendered app that uses Turbolinks with session tokens,
* require this file at the end of `app/javascript/shopify_app/index.js`:
*
* >> require('./shopify_app_turbolinks');
*/
import { getSessionToken } from "@shopify/app-bridge-utils";
const SESSION_TOKEN_REFRESH_INTERVAL = 2000; // Request a new token every 2s
document.addEventListener("turbolinks:request-start", function (event) {
var xhr = event.data.xhr;
xhr.setRequestHeader("Authorization", "Bearer " + window.sessionToken);
});
document.addEventListener("turbolinks:render", function () {
$("form, a[data-method=delete]").on("ajax:beforeSend", function (event) {
const xhr = event.detail[0];
xhr.setRequestHeader("Authorization", "Bearer " + window.sessionToken);
});
});
document.addEventListener("DOMContentLoaded", async function () {
// Wait for a session token before trying to load an authenticated page
await retrieveToken(app);
// Keep retrieving a session token periodically
keepRetrievingToken(app);
// Redirect to the requested page when DOM loads
var isInitialRedirect = true;
redirectThroughTurbolinks(isInitialRedirect);
document.addEventListener("turbolinks:load", function (event) {
redirectThroughTurbolinks();
});
// Helper functions
function redirectThroughTurbolinks(isInitialRedirect = false) {
var data = document.getElementById("shopify-app-init").dataset;
var validLoadPath = data && data.loadPath;
var shouldRedirect = false;
switch(isInitialRedirect) {
case true:
shouldRedirect = validLoadPath;
break;
case false:
shouldRedirect = validLoadPath && data.loadPath !== '/home'; // Replace with the app's home_path
break;
}
if (shouldRedirect) Turbolinks.visit(data.loadPath);
}
async function retrieveToken(app) {
window.sessionToken = await getSessionToken(app);
}
function keepRetrievingToken(app) {
setInterval(() => {
retrieveToken(app);
}, SESSION_TOKEN_REFRESH_INTERVAL);
}
});