mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-08 15:51:16 +08:00
fix(ssr): fix global window and document references (#17590)
This commit is contained in:
@ -4,10 +4,12 @@ import { isPlatform, setupPlatforms } from '../utils/platform';
|
||||
|
||||
import { Config, configFromSession, configFromURL, saveConfig } from './config';
|
||||
|
||||
const win = window;
|
||||
const Ionic = (win as any)['Ionic'] = (win as any)['Ionic'] || {};
|
||||
declare const Context: any;
|
||||
|
||||
const win = Context['window'] ? Context['window'] : typeof (window as any) !== 'undefined' ? window : {} as Window;
|
||||
|
||||
const Ionic = (win as any)['Ionic'] = (win as any)['Ionic'] || {};
|
||||
|
||||
// queue used to coordinate DOM reads and
|
||||
// write in order to avoid layout thrashing
|
||||
Object.defineProperty(Ionic, 'queue', {
|
||||
@ -21,25 +23,27 @@ Context.isPlatform = isPlatform;
|
||||
// create the Ionic.config from raw config object (if it exists)
|
||||
// and convert Ionic.config into a ConfigApi that has a get() fn
|
||||
const configObj = {
|
||||
...configFromSession(),
|
||||
...configFromSession(win),
|
||||
persistConfig: false,
|
||||
...Ionic['config'],
|
||||
...configFromURL()
|
||||
...configFromURL(win)
|
||||
};
|
||||
const config = Ionic['config'] = Context['config'] = new Config(configObj);
|
||||
if (config.getBoolean('persistConfig')) {
|
||||
saveConfig(configObj);
|
||||
saveConfig(win, configObj);
|
||||
}
|
||||
|
||||
// first see if the mode was set as an attribute on <html>
|
||||
// which could have been set by the user, or by prerendering
|
||||
// otherwise get the mode via config settings, and fallback to md
|
||||
const documentElement = document.documentElement!;
|
||||
const mode = config.get('mode', documentElement.getAttribute('mode') || (isPlatform(win, 'ios') ? 'ios' : 'md'));
|
||||
const documentElement = win.document ? win.document.documentElement : null;
|
||||
const mode = config.get('mode', (documentElement && documentElement.getAttribute('mode')) || (isPlatform(win, 'ios') ? 'ios' : 'md'));
|
||||
Ionic.mode = Context.mode = mode;
|
||||
config.set('mode', mode);
|
||||
documentElement.setAttribute('mode', mode);
|
||||
documentElement.classList.add(mode);
|
||||
if (documentElement) {
|
||||
documentElement.setAttribute('mode', mode);
|
||||
documentElement.classList.add(mode);
|
||||
}
|
||||
if (config.getBoolean('_testing')) {
|
||||
config.set('animated', false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user