
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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 806,572 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.