-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement lazy loading in app (#152)
* feat: implement lazy loading in app * fix: remove unnecesary x at the end * fix: move polyfill functions outside validation * fix: cancel request inmediatly an event ocurrs * fix: simplify lazy loading approach * fix: define function type
- Loading branch information
Showing
6 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import globalB3 from '@b3/global-b3' | ||
|
||
export const requestIdleCallbackFunction: typeof window.requestIdleCallback = window.requestIdleCallback ? window.requestIdleCallback : (cb: IdleRequestCallback) => { | ||
const start = Date.now() | ||
return window.setTimeout(() => { | ||
cb({ | ||
didTimeout: false, | ||
timeRemaining() { | ||
return Math.max(0, 50 - (Date.now() - start)) | ||
}, | ||
}) | ||
}, 1) | ||
} | ||
|
||
window.b2bStorefrontApp = { | ||
/* init flag listener */ | ||
__isInitVariable: false, | ||
__isInitListener: () => {}, | ||
set __isInit(value: boolean) { | ||
this.__isInitVariable = value | ||
this.__isInitListener(value) | ||
}, | ||
get __isInit() { | ||
return this.__isInitVariable | ||
}, | ||
registerIsInitListener(listener: Function) { | ||
this.__isInitListener = listener | ||
}, | ||
|
||
// helper variable to save clicked link | ||
clickedLinkElement: undefined, | ||
} | ||
|
||
export const initApp = async () => { | ||
if (window.b2bStorefrontApp.__isInit) return | ||
|
||
await import('./react-setup') | ||
} | ||
|
||
const clickLink = async (e: MouseEvent) => { | ||
window.b2bStorefrontApp.clickedLinkElement = e.target | ||
e.preventDefault() | ||
e.stopPropagation() | ||
await initApp() | ||
} | ||
|
||
export const bindLinks = () => { | ||
const links:NodeListOf<HTMLAnchorElement> = document.querySelectorAll(`${globalB3['dom.registerElement']}, ${globalB3['dom.allOtherElement']}`) | ||
|
||
links.forEach((accessLink) => accessLink.addEventListener('click', clickLink)) | ||
} | ||
export const unbindLinks = () => { | ||
const links:NodeListOf<HTMLAnchorElement> = document.querySelectorAll(`${globalB3['dom.registerElement']}, ${globalB3['dom.allOtherElement']}`) | ||
|
||
links.forEach((accessLink) => accessLink.removeEventListener('click', clickLink)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { | ||
bindLinks, initApp, requestIdleCallbackFunction, unbindLinks, | ||
} from './load-functions' | ||
|
||
// check if the accesed url contains a hashtag | ||
if (window.location.hash.startsWith('#/')) { | ||
initApp() | ||
} else { | ||
// load the app when the browser is free | ||
requestIdleCallbackFunction(initApp) | ||
// and bind links to load the app | ||
bindLinks() | ||
window.addEventListener('beforeunload', unbindLinks) | ||
// and observe global flag to simulate click | ||
window.b2bStorefrontApp.registerIsInitListener(() => { | ||
unbindLinks() | ||
setTimeout(() => window.b2bStorefrontApp.clickedLinkElement?.click(), 0) | ||
}) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters