Skip to content

nolimits4web/ssr-window

Repository files navigation

SSR Window

Better handling for window and document object in SSR environment.

This library doesn't implement the DOM (like JSDOM), it just patches (or creates window and document objects) to avoid them to fail (throw errors) during server-side rendering.

Was created for use in:

Installation

Library available on NPM:

npm i ssr-window

Usage

import { getWindow, getDocument } from 'ssr-window';

const window = getWindow();
const document = getDocument();

window.addEventListener('resize', () => {});

const div = document.querySelectorAll('div');

Extending

If you rely on some window/document properties which are not included here, you can use extend helper to add them:

import { ssrWindow, ssrDocument, extend } from 'ssr-window';

// add window.navigator.language
extend(ssrWindow, {
  navigator: {
    language: 'en',
  },
});

// add document.body
extend(ssrDocument, {
  body: {
    /* ... */
  },
});

Contribution

Yes please! See the contributing guidelines for details.

Licence

This project is licensed under the terms of the MIT license.