Skip to content

Commit 2ee0a3d

Browse files
committed
Fix various issues related to Next.js and SSR
1 parent a9a9bfa commit 2ee0a3d

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22

33
* Add missing TypeScript `title` attribute type to steps.
4+
* Fix various issues related to Next.js and SSR.
45

56
## 0.6.0
67

src/components/Hints/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Component } from 'react';
44

55
import * as introJsPropTypes from '../../helpers/proptypes';
66
import * as introJsDefaultProps from '../../helpers/defaultProps';
7+
import { isServer } from '../../helpers/server';
78

89
/**
910
* Intro.js Hints Component.
@@ -92,6 +93,10 @@ export default class Hints extends Component {
9293
* Installs Intro.js.
9394
*/
9495
installIntroJs() {
96+
if (isServer()) {
97+
return;
98+
}
99+
95100
this.introJs = introJs();
96101

97102
const { onClick, onClose } = this.props;

src/components/Hints/index.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import renderer from 'react-test-renderer';
33
import { shallow } from 'enzyme';
44

55
import Hints from './index';
6+
import * as server from '../../helpers/server';
67

78
/**
89
* Hints.
@@ -119,4 +120,12 @@ describe('Hints', () => {
119120

120121
expect(wrapper.instance().introJs.onHintClose).toBe(onClose);
121122
});
123+
124+
test('should not install intro.js during SSR', () => {
125+
jest.spyOn(server, 'isServer').mockReturnValueOnce(true);
126+
127+
const wrapper = shallow(<Hints hints={hints} />);
128+
129+
expect(wrapper.instance().introJs).toBe(null);
130+
});
122131
});

src/components/Steps/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { renderToStaticMarkup } from 'react-dom/server';
55

66
import * as introJsPropTypes from '../../helpers/proptypes';
77
import * as introJsDefaultProps from '../../helpers/defaultProps';
8+
import { isServer } from '../../helpers/server';
89

910
/**
1011
* Intro.js Steps Component.
@@ -221,6 +222,10 @@ export default class Steps extends Component {
221222
* Installs Intro.js.
222223
*/
223224
installIntroJs() {
225+
if (isServer()) {
226+
return;
227+
}
228+
224229
this.introJs = introJs();
225230

226231
this.introJs.onexit(this.onExit);

src/components/Steps/index.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import renderer from 'react-test-renderer';
44
import { shallow } from 'enzyme';
55

66
import Steps from './index';
7+
import * as server from '../../helpers/server';
78

89
jest.useFakeTimers();
910

@@ -344,4 +345,12 @@ describe('Steps', () => {
344345

345346
expect(wrapper.instance().introJs._introItems[0].element).toEqual(expect.any(HTMLDivElement));
346347
});
348+
349+
test('should not install intro.js during SSR', () => {
350+
jest.spyOn(server, 'isServer').mockReturnValueOnce(true);
351+
352+
const wrapper = shallow(<Steps initialStep={0} steps={steps} onExit={() => {}} />);
353+
354+
expect(wrapper.instance().introJs).toBe(null);
355+
});
347356
});

src/helpers/server.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isServer() {
2+
return typeof window === 'undefined';
3+
}

0 commit comments

Comments
 (0)