diff --git a/src/components/StickySidebar/StickyResponsiveSidebar.tsx b/src/components/StickySidebar/StickyResponsiveSidebar.tsx index 51167b36..24b0a06e 100644 --- a/src/components/StickySidebar/StickyResponsiveSidebar.tsx +++ b/src/components/StickySidebar/StickyResponsiveSidebar.tsx @@ -6,9 +6,10 @@ import { RedocNormalizedOptions, RedocRawOptions } from '../../services/RedocNor import styled, { media, withProps } from '../../styled-components'; import { OptionsContext } from '../OptionsProvider'; import { AnimatedChevronButton } from './ChevronSvg'; +import { IS_BROWSER } from '../../utils/index'; let Stickyfill; -if (typeof window !== 'undefined') { +if (IS_BROWSER) { Stickyfill = require('stickyfill').default; } diff --git a/src/services/HistoryService.ts b/src/services/HistoryService.ts index ab80869d..2ae14c3d 100644 --- a/src/services/HistoryService.ts +++ b/src/services/HistoryService.ts @@ -1,6 +1,6 @@ import { bind, debounce } from 'decko'; import { EventEmitter } from 'eventemitter3'; -import { isBrowser } from '../utils/'; +import { IS_BROWSER } from '../utils/'; const EVENT = 'hashchange'; @@ -18,7 +18,7 @@ class IntHistoryService { } get hash(): string { - return isBrowser ? window.location.hash : ''; + return IS_BROWSER ? window.location.hash : ''; } subscribe(cb): () => void { @@ -35,13 +35,13 @@ class IntHistoryService { }; bind() { - if (isBrowser) { + if (IS_BROWSER) { window.addEventListener('hashchange', this.emit, false); } } dispose() { - if (isBrowser) { + if (IS_BROWSER) { window.removeEventListener('hashchange', this.emit); } this.causedHashChange = false; @@ -54,13 +54,13 @@ class IntHistoryService { return; } if (rewriteHistory) { - if (isBrowser) { + if (IS_BROWSER) { window.history.replaceState(null, '', window.location.href.split('#')[0] + '#' + hash); } return; } this.causedHashChange = true; - if (isBrowser) { + if (IS_BROWSER) { window.location.hash = hash; } } diff --git a/src/services/OpenAPIParser.ts b/src/services/OpenAPIParser.ts index 9f2bb96d..1d18a4d4 100644 --- a/src/services/OpenAPIParser.ts +++ b/src/services/OpenAPIParser.ts @@ -3,7 +3,7 @@ import { resolve as urlResolve } from 'url'; import { OpenAPIRef, OpenAPISchema, OpenAPISpec, Referenced } from '../types'; -import { appendToMdHeading, isBrowser } from '../utils/'; +import { appendToMdHeading, IS_BROWSER } from '../utils/'; import { JsonPointer } from '../utils/JsonPointer'; import { isNamedDefinition } from '../utils/openapi'; import { buildComponentComment, COMPONENT_REGEXP } from './MarkdownRenderer'; @@ -54,7 +54,7 @@ export class OpenAPIParser { this.spec = spec; - const href = isBrowser ? window.location.href : ''; + const href = IS_BROWSER ? window.location.href : ''; if (typeof specUrl === 'string') { this.specUrl = urlResolve(href, specUrl); } else { diff --git a/src/services/ScrollService.ts b/src/services/ScrollService.ts index 13459028..b184f9f5 100644 --- a/src/services/ScrollService.ts +++ b/src/services/ScrollService.ts @@ -1,7 +1,7 @@ import { bind } from 'decko'; import * as EventEmitter from 'eventemitter3'; -import { isBrowser, querySelector, Throttle } from '../utils'; +import { IS_BROWSER, querySelector, Throttle } from '../utils'; import { RedocNormalizedOptions } from './RedocNormalizedOptions'; const EVENT = 'scroll'; @@ -11,7 +11,7 @@ export class ScrollService { private _emiter: EventEmitter; private _prevOffsetY: number = 0; constructor(private options: RedocNormalizedOptions) { - this._scrollParent = isBrowser ? window : undefined; + this._scrollParent = IS_BROWSER ? window : undefined; this._emiter = new EventEmitter(); this.bind(); } diff --git a/src/services/SearchStore.ts b/src/services/SearchStore.ts index 0e1d1165..b806693d 100644 --- a/src/services/SearchStore.ts +++ b/src/services/SearchStore.ts @@ -1,11 +1,11 @@ import { IMenuItem } from './MenuStore'; import { OperationModel } from './models'; +import { IS_BROWSER } from '../utils/'; let worker; -if (typeof URL !== 'undefined') { +if (IS_BROWSER) { try { - // tslint:disable-next-line worker = require('workerize-loader?inline&fallback=false!./SearchWorker.worker'); } catch (e) { worker = require('./SearchWorker.worker').default; diff --git a/src/services/models/ApiInfo.ts b/src/services/models/ApiInfo.ts index d67557f2..175cf5b0 100644 --- a/src/services/models/ApiInfo.ts +++ b/src/services/models/ApiInfo.ts @@ -1,5 +1,5 @@ import { OpenAPIContact, OpenAPIInfo, OpenAPILicense } from '../../types'; -import { isBrowser } from '../../utils/'; +import { IS_BROWSER } from '../../utils/'; import { OpenAPIParser } from '../OpenAPIParser'; import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; @@ -25,12 +25,12 @@ export class ApiInfoModel implements OpenAPIInfo { return this.parser.specUrl; } - if (isBrowser && window.Blob && window.URL) { + if (IS_BROWSER && window.Blob && window.URL) { const blob = new Blob([JSON.stringify(this.parser.spec, null, 2)], { type: 'application/json', }); return window.URL.createObjectURL(blob); - } else if (!isBrowser) { + } else if (!IS_BROWSER) { return ( 'data:application/octet-stream;base64,' + new Buffer(JSON.stringify(this.parser.spec, null, 2)).toString('base64') diff --git a/src/utils/dom.ts b/src/utils/dom.ts index 4b069cd9..bce67e84 100644 --- a/src/utils/dom.ts +++ b/src/utils/dom.ts @@ -1,4 +1,4 @@ -export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'; +export const IS_BROWSER = typeof window !== 'undefined' && 'HTMLElement' in window; export function querySelector(selector: string): Element | null { if (typeof document !== 'undefined') {