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

MWPW-164637 Use redirectUrl to prefetch next page #932

Merged
merged 4 commits into from
Jan 9, 2025
Merged
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
85 changes: 41 additions & 44 deletions acrobat/blocks/verb-widget/verb-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import LIMITS from './limits.js';
import { setLibs, getEnv, isOldBrowser } from '../../scripts/utils.js';
import verbAnalytics from '../../scripts/alloy/verb-widget.js';
import createSvgElement from './icons.js';
import { localeMap } from '../unity/unity.js';

const miloLibs = setLibs('/libs');
const { createTag, getConfig } = await import(`${miloLibs}/utils/utils.js`);
Expand Down Expand Up @@ -42,24 +41,19 @@ const setDraggingClass = (widget, shouldToggle) => {
shouldToggle ? widget.classList.add('dragging') : widget.classList.remove('dragging');
};

function prefetchNextPage(verb) {
const { locale } = getConfig();
const localePath = localeMap[locale.prefix.replace('/', '')].split('-').reverse().join('/');
const nextPageHost = getEnv() === 'prod' ? 'acrobat.adobe.com' : 'stage.acrobat.adobe.com';
const nextPageUrl = `https://${nextPageHost}/${localePath}/${verb}`;

function prefetchNextPage(url) {
const link = document.createElement('link');
link.rel = 'prefetch';
link.href = nextPageUrl;
link.href = url;
link.crossOrigin = 'anonymous';
link.as = 'document';

document.head.appendChild(link);
}

function initiatePrefetch(verb) {
function initiatePrefetch(url) {
if (!window.prefetchInitiated) {
prefetchNextPage(verb);
prefetchNextPage(url);
window.prefetchInitiated = true;
}
}
Expand Down Expand Up @@ -225,7 +219,6 @@ export default async function init(element) {
button.addEventListener('click', () => {
verbAnalytics('filepicker:shown', VERB);
verbAnalytics('dropzone:choose-file-clicked', VERB);
initiatePrefetch(VERB);
});

button.addEventListener('cancel', () => {
Expand All @@ -235,7 +228,6 @@ export default async function init(element) {
widget.addEventListener('dragover', (e) => {
e.preventDefault();
setDraggingClass(widget, true);
initiatePrefetch(VERB);
});

widget.addEventListener('dragleave', () => {
Expand All @@ -249,38 +241,43 @@ export default async function init(element) {

element.addEventListener('unity:track-analytics', (e) => {
const cookieExp = new Date(Date.now() + 90 * 1000).toUTCString();

if (e.detail?.event === 'change') {
verbAnalytics('choose-file:open', VERB, e.detail?.data);
setUser();
}
// maybe new event name files-dropped?
if (e.detail?.event === 'drop') {
initiatePrefetch(VERB);
verbAnalytics('files-dropped', VERB, e.detail?.data);
setDraggingClass(widget, false);
setUser();
}

if (e.detail?.event === 'cancel') {
verbAnalytics('job:cancel', VERB, e.detail?.data);
setUser();
}

if (e.detail?.event === 'uploading') {
verbAnalytics('job:uploading', VERB, e.detail?.data);
setUser();
document.cookie = `UTS_Uploading=${Date.now()};domain=.adobe.com;path=/;expires=${cookieExp}`;
window.addEventListener('beforeunload', (w) => {
handleExit(w);
});
}

if (e.detail?.event === 'uploaded') {
verbAnalytics('job:test-uploaded', VERB, e.detail?.data, false);
exitFlag = true;
setUser();
document.cookie = `UTS_Uploaded=${Date.now()};domain=.adobe.com;path=/;expires=${cookieExp}`;
const { event, data } = e.detail || {};

if (!event) return;

const analyticsMap = {
change: () => {
verbAnalytics('choose-file:open', VERB, data);
setUser();
},
drop: () => {
verbAnalytics('files-dropped', VERB, data);
setDraggingClass(widget, false);
setUser();
},
cancel: () => {
verbAnalytics('job:cancel', VERB, data);
setUser();
},
uploading: () => {
verbAnalytics('job:uploading', VERB, data);
setUser();
document.cookie = `UTS_Uploading=${Date.now()};domain=.adobe.com;path=/;expires=${cookieExp}`;
window.addEventListener('beforeunload', handleExit);
},
uploaded: () => {
verbAnalytics('job:test-uploaded', VERB, data, false);
exitFlag = true;
setUser();
document.cookie = `UTS_Uploaded=${Date.now()};domain=.adobe.com;path=/;expires=${cookieExp}`;
},
redirectUrl: () => {
if (data) initiatePrefetch(data);
},
};

if (analyticsMap[event]) {
analyticsMap[event]();
}
});

Expand Down
Loading