Add fallback if NexT.boot.motion is failed#953
Add fallback if NexT.boot.motion is failed#953wherewhere wants to merge 2 commits intonext-theme:masterfrom
Conversation
Add fallback if NexT.boot.motion is failed
There was a problem hiding this comment.
Pull request overview
Adds defensive fallbacks around NexT’s client-side boot pipeline to prevent the page from remaining hidden when motion initialization fails (notably due to the use-motion CSS gating).
Changes:
- Wraps
NexT.boot.registerEvents()andNexT.boot.refresh()in try/catch blocks with error logging. - Wraps
NexT.boot.motion()integrator bootstrap in try/catch; on failure logs an error, removesuse-motion, and disables motion at runtime.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| NexT.boot.registerEvents = function() { | ||
| try { | ||
| NexT.utils.registerScrollPercent(); | ||
| NexT.utils.registerCanIUseTag(); | ||
| NexT.utils.updateFooterPosition(); | ||
|
|
||
| NexT.utils.registerScrollPercent(); | ||
| NexT.utils.registerCanIUseTag(); | ||
| NexT.utils.updateFooterPosition(); | ||
|
|
||
| // Mobile top menu bar. | ||
| document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => { | ||
| event.currentTarget.classList.toggle('toggle-close'); | ||
| const siteNav = document.querySelector('.site-nav'); | ||
| if (!siteNav) return; | ||
| siteNav.style.setProperty('--scroll-height', siteNav.scrollHeight + 'px'); | ||
| document.body.classList.toggle('site-nav-on'); | ||
| }); | ||
| // Mobile top menu bar. | ||
| document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => { | ||
| event.currentTarget.classList.toggle('toggle-close'); |
| CONFIG.prism && window.Prism.highlightAll(); | ||
| CONFIG.mediumzoom && window.mediumZoom('.post-body :not(a) > img, .post-body > img', { | ||
| background: 'var(--content-bg-color)' | ||
| }); | ||
| CONFIG.lazyload && window.lozad('.post-body img').observe(); | ||
| if (CONFIG.pangu) { | ||
| // Polyfill for requestIdleCallback if not supported | ||
| if (!window.requestIdleCallback) { | ||
| window.requestIdleCallback = function(cb) { | ||
| cb({ | ||
| didTimeout : false, | ||
| timeRemaining: () => 100 | ||
| }); | ||
| }; | ||
| } | ||
| [...document.getElementsByTagName('main')].forEach(e => window.pangu.spacingNode(e)); |
There was a problem hiding this comment.
Pull request overview
Adds runtime fallbacks to prevent pages from remaining hidden when NexT Motion fails, particularly under PJAX navigation, by catching motion bootstrap errors and switching to a static rendering mode.
Changes:
- Wrap Motion integrator bootstrapping in try/catch (initial load and PJAX success) and disable motion on failure.
- Remove the
use-motionbody class on motion failure to restore element visibility. - Add broad error handling around
NexT.boot.registerEvents()andNexT.boot.refresh()to avoid stopping later boot steps.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
source/js/pjax.js |
Adds try/catch around PJAX motion bootstrap and falls back to static mode on errors. |
source/js/next-boot.js |
Adds try/catch around boot phases and motion bootstrap to avoid blank pages when motion fails. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| NexT.boot.registerEvents = function() { | ||
| try { | ||
| NexT.utils.registerScrollPercent(); | ||
| NexT.utils.registerCanIUseTag(); | ||
| NexT.utils.updateFooterPosition(); | ||
|
|
||
| NexT.utils.registerScrollPercent(); | ||
| NexT.utils.registerCanIUseTag(); | ||
| NexT.utils.updateFooterPosition(); | ||
|
|
||
| // Mobile top menu bar. | ||
| document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => { | ||
| event.currentTarget.classList.toggle('toggle-close'); | ||
| const siteNav = document.querySelector('.site-nav'); | ||
| if (!siteNav) return; | ||
| siteNav.style.setProperty('--scroll-height', siteNav.scrollHeight + 'px'); | ||
| document.body.classList.toggle('site-nav-on'); | ||
| }); | ||
| // Mobile top menu bar. | ||
| document.querySelector('.site-nav-toggle .toggle').addEventListener('click', event => { | ||
| event.currentTarget.classList.toggle('toggle-close'); |
| NexT.boot.refresh = function() { | ||
|
|
||
| /** | ||
| * Register JS handlers by condition option. | ||
| * Need to add config option in Front-End at 'scripts/helpers/next-config.js' file. | ||
| */ | ||
| CONFIG.prism && window.Prism.highlightAll(); | ||
| CONFIG.mediumzoom && window.mediumZoom('.post-body :not(a) > img, .post-body > img', { | ||
| background: 'var(--content-bg-color)' | ||
| }); | ||
| CONFIG.lazyload && window.lozad('.post-body img').observe(); | ||
| if (CONFIG.pangu) { | ||
| // Polyfill for requestIdleCallback if not supported | ||
| if (!window.requestIdleCallback) { | ||
| window.requestIdleCallback = function(cb) { | ||
| cb({ | ||
| didTimeout : false, | ||
| timeRemaining: () => 100 | ||
| }); | ||
| }; | ||
| try { | ||
| // Register JS handlers by condition option. | ||
| // Need to add config option in Front-End at 'scripts/helpers/next-config.js' file. | ||
| CONFIG.prism && window.Prism.highlightAll(); | ||
| CONFIG.mediumzoom && window.mediumZoom('.post-body :not(a) > img, .post-body > img', { | ||
| background: 'var(--content-bg-color)' | ||
| }); | ||
| CONFIG.lazyload && window.lozad('.post-body img').observe(); | ||
| if (CONFIG.pangu) { | ||
| // Polyfill for requestIdleCallback if not supported |
| .add(NexT.motion.middleWares.postList) | ||
| .bootstrap(); | ||
| } catch (error) { | ||
| console.error('NexT Motion Error, fallback to static mode', error); |
| .add(NexT.motion.middleWares.footer) | ||
| .bootstrap(); | ||
| } catch (error) { | ||
| console.error('NexT Motion Error, fallback to static mode', error); |
PR Checklist
PR Type
What is the current behavior?
Document will empty if
NexT.boot.motionis not run or failed.What is the new behavior?
Add try catch to ensure
NexT.boot.motionrun and removeuse-motionif it failed.