Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
ssr-window
Advanced tools
The ssr-window package provides a simple way to handle window and document objects in server-side rendering (SSR) environments. It allows developers to use window and document objects without worrying about SSR compatibility issues.
Window Object
This feature allows you to use the window object in an SSR environment. The code sample demonstrates how to access the window's innerWidth property.
const { window } = require('ssr-window');
console.log(window.innerWidth);
Document Object
This feature allows you to use the document object in an SSR environment. The code sample demonstrates how to create a new div element and set its innerHTML.
const { document } = require('ssr-window');
const div = document.createElement('div');
div.innerHTML = 'Hello, world!';
console.log(div.outerHTML);
Custom Window and Document
This feature allows you to create custom window and document objects by extending the default ones provided by ssr-window. The code sample demonstrates how to add custom properties and methods.
const { window, document } = require('ssr-window');
const customWindow = { ...window, customProperty: 'customValue' };
const customDocument = { ...document, customMethod: () => 'customMethod' };
console.log(customWindow.customProperty);
console.log(customDocument.customMethod());
jsdom is a JavaScript implementation of the WHATWG DOM and HTML standards, primarily intended for use with Node.js. It provides a complete, fully-featured DOM environment, making it more comprehensive than ssr-window. However, it is also heavier and more complex to set up.
domino is a fast, minimal DOM implementation for Node.js. It is lighter than jsdom and provides a basic DOM environment, making it more similar to ssr-window in terms of simplicity and performance. However, it may lack some of the advanced features of jsdom.
happy-dom is a lightweight and fast DOM implementation for Node.js. It aims to be a drop-in replacement for jsdom with better performance. It provides a more complete DOM environment compared to ssr-window, but it is still relatively simple to use.
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:
Library available on NPM:
npm i ssr-window
import { window, document } from 'ssr-window';
window.addEventListener('resize', () => {});
const div = document.querySelectorAll('div');
If you rely on some window/document properties which are not included here, you can use extend
helper to add them:
import { window, document, extend } from 'ssr-window';
// add window.navigator.language
extend(window, {
navigator: {
language: 'en',
},
});
// add document.body
extend(document, {
body: {
/* ... */
},
});
Yes please! See the contributing guidelines for details.
This project is licensed under the terms of the MIT license.
FAQs
Better handling for window object in SSR environment
The npm package ssr-window receives a total of 619,297 weekly downloads. As such, ssr-window popularity was classified as popular.
We found that ssr-window demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.