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

feat: prevent infinite refreshes and exiting race condition #97

Merged
merged 3 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 27 additions & 18 deletions src/toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,34 @@ async function setup (rawInput) {
const analytics = previewState.auth && new Analytics(toolbarClient);

// Start concurrently preview (always) and prediction (if authenticated)
const { initialRef, upToDate } = await preview.setup();
const { initialRef, upToDate, isActive } = await preview.setup();
const { convertedLegacy } = previewCookieHelper.init(initialRef);
const displayPreview = Boolean(initialRef);

if (convertedLegacy || !upToDate) {
reloadOrigin();
} else if (displayPreview || previewState.auth) {
// eslint-disable-next-line no-undef
await script(`${CDN_HOST}/prismic-toolbar/${version}/toolbar.js`);
new window.prismic.Toolbar({
displayPreview,
auth: previewState.auth,
preview,
prediction,
analytics
});

// Track initial setup of toolbar
if (analytics) analytics.trackToolbarSetup();

if (isActive) {
if (convertedLegacy || !upToDate) {
reloadOrigin();
return;
}

if (previewState.auth) {
// eslint-disable-next-line no-undef
await script(`${CDN_HOST}/prismic-toolbar/${version}/toolbar.js`);
new window.prismic.Toolbar({
displayPreview: isActive,
auth: previewState.auth,
preview,
prediction,
analytics
});

// Track initial setup of toolbar
if (analytics) analytics.trackToolbarSetup();
}
} else {
if (previewCookieHelper.getRefForDomain())
previewCookieHelper.deletePreviewForDomain();

await toolbarClient.closePreviewSession();
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/toolbar/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ export class Preview {
this.documents = preview.documents || [];

const refUpToDate = preview.ref === this.cookie.getRefForDomain();
const displayPreview = preview.ref && refUpToDate;
const displayPreview = this.active && refUpToDate;
// We don't display the preview by default unless the start function says so
if (displayPreview) this.watchPreviewUpdates();

return { initialRef: preview.ref, upToDate: refUpToDate };
return {
isActive: this.active,
initialRef: preview.ref,
upToDate: refUpToDate
};
};

watchPreviewUpdates() {
if (this.active) {
this.interval = setInterval(() => {
if (document.visibilityState === 'visible') this.updatePreview();
if (document.visibilityState === 'visible') {
if (this.cookie.getRefForDomain()) {
this.updatePreview();
} else {
this.end();
}
}
}, 3000);
}
}
Expand All @@ -45,6 +55,8 @@ export class Preview {
const { reload, ref } = await this.client.updatePreview();
this.start(ref);
if (reload) {
this.cancelPreviewUpdates();

// Dispatch the update event and hard reload if not cancelled by handlers
if (dispatchToolbarEvent(toolbarEvents.previewUpdate, { ref })) {
reloadOrigin();
Expand All @@ -70,7 +82,6 @@ export class Preview {
async end() {
this.cancelPreviewUpdates();
await this.client.closePreviewSession();
if (!this.cookie.getRefForDomain()) return;
this.cookie.deletePreviewForDomain();

// Dispatch the end event and hard reload if not cancelled by handlers
Expand Down