Skip to content

Fix SSR check on Node.js v22 #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['20.x', '22.x']
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Use Node.js
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: NPM install
run: npm install
- name: Verify build works
run: npm run build
- name: Jest
run: npm run test

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ require('codemirror/mode/javascript/javascript');

- Is server side rendering supported?

Yes. react-codemirror2 will prevent rendering in absence of `navigator`. You can also force the component to not render via a `PREVENT_CODEMIRROR_RENDER` global.
Yes. react-codemirror2 will prevent rendering in absence of `window`. You can also force the component to not render via a `PREVENT_CODEMIRROR_RENDER` global.

- How can I get the instance?

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
"src/**"
],
"setupFiles": [
"@nteract/mockument",
"raf/polyfill"
"./test/setup.js"
],
"testRegex": "./test/(index|index.server).spec.tsx",
"transform": {
".(ts|tsx)": "ts-jest"
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as codemirror from 'codemirror';
declare let global: any;
declare let require: any;

const SERVER_RENDERED = (typeof navigator === 'undefined' || (typeof global !== 'undefined' && global['PREVENT_CODEMIRROR_RENDER'] === true));
const SERVER_RENDERED = (typeof window === 'undefined' || (typeof global !== 'undefined' && global['PREVENT_CODEMIRROR_RENDER'] === true));

let cm;
if (!SERVER_RENDERED) {
Expand Down
16 changes: 16 additions & 0 deletions test/index-server.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';
// @ts-ignore
import { renderToString } from 'react-dom/server';

import {Controlled, UnControlled} from '../src';

describe('Server Rendering', () => {

it('should not render', () => {
const uWrapper = renderToString(<UnControlled value="nomatter"/>);
const cWrapper = renderToString(<Controlled value="nomatter" onBeforeChange={() => {}}/>);

expect(uWrapper).toBe("");
expect(cWrapper).toBe("");
});
});
4 changes: 4 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if (typeof window !== 'undefined') {
require('@nteract/mockument');
require('raf/polyfill');
}