fix(ssr): fix global window and document references (#17590)

This commit is contained in:
Adam Bradley
2019-02-22 20:13:09 -06:00
committed by GitHub
parent 6bea9d3248
commit 4646f53ec7
18 changed files with 76 additions and 70 deletions

View File

@ -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);
}