-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssr-context.ts
More file actions
25 lines (20 loc) · 998 Bytes
/
ssr-context.ts
File metadata and controls
25 lines (20 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { InjectionToken } from '@angular/core';
export const SSR_REQUEST = new InjectionToken<any>('SSR_REQUEST');
const globalAny = typeof global !== 'undefined' ? (global as any) : (typeof globalThis !== 'undefined' ? (globalThis as any) : {});
if (!globalAny.ssrRequestStorage) {
// Default dummy implementation
globalAny.ssrRequestStorage = {
getStore: () => null,
run: (store: any, callback: () => void) => callback()
};
// Async load for SSR, protected by node environment check
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
import('node:async_hooks').then(hooks => {
globalAny.ssrRequestStorage = new hooks.AsyncLocalStorage();
}).catch(e => console.error('Failed to load async_hooks', e));
}
}
export const ssrRequestStorage = {
getStore: () => globalAny.ssrRequestStorage.getStore(),
run: (store: any, cb: () => void) => globalAny.ssrRequestStorage.run(store, cb)
};