fix(elements): avoid exception when window is undefined (#23324)
Detect server environment by checking `typeof window` and schedule render immediately instead of waiting for RAF. This does not make Angular Elements work on platform-server. This is just the first step. PR Close #23324
This commit is contained in:
parent
7e8cee6b61
commit
af46d097ff
|
@ -21,7 +21,7 @@ export const scheduler = {
|
||||||
* Returns a function that when executed will cancel the scheduled function.
|
* Returns a function that when executed will cancel the scheduled function.
|
||||||
*/
|
*/
|
||||||
schedule(taskFn: () => void, delay: number): () =>
|
schedule(taskFn: () => void, delay: number): () =>
|
||||||
void{const id = window.setTimeout(taskFn, delay); return () => window.clearTimeout(id);},
|
void{const id = setTimeout(taskFn, delay); return () => clearTimeout(id);},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedule a callback to be called before the next render.
|
* Schedule a callback to be called before the next render.
|
||||||
|
@ -32,6 +32,11 @@ export const scheduler = {
|
||||||
scheduleBeforeRender(taskFn: () => void): () => void{
|
scheduleBeforeRender(taskFn: () => void): () => void{
|
||||||
// TODO(gkalpak): Implement a better way of accessing `requestAnimationFrame()`
|
// TODO(gkalpak): Implement a better way of accessing `requestAnimationFrame()`
|
||||||
// (e.g. accounting for vendor prefix, SSR-compatibility, etc).
|
// (e.g. accounting for vendor prefix, SSR-compatibility, etc).
|
||||||
|
if (typeof window === 'undefined') {
|
||||||
|
// For SSR just schedule immediately.
|
||||||
|
return scheduler.schedule(taskFn, 0);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof window.requestAnimationFrame === 'undefined') {
|
if (typeof window.requestAnimationFrame === 'undefined') {
|
||||||
const frameMs = 16;
|
const frameMs = 16;
|
||||||
return scheduler.schedule(taskFn, frameMs);
|
return scheduler.schedule(taskFn, frameMs);
|
||||||
|
|
Loading…
Reference in New Issue