
Security News
Federal Government Rescinds Software Supply Chain Mandates, Makes SBOMs Optional
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.
next-client-script
Advanced tools
Supercharge the performance of your Next.js apps by using a minimal client runtime that avoids full-blown hydration. 🚀
By default, Next.js adds the code to your client bundle that is necessary to execute your whole page. At a minimum this includes React itself, the components to render the markup and if relevant, the data that is necessary to rehydrate the markup (result from getInitialProps and friends).
For content heavy sites this can cause performance issues since the page is unresponsive while the client bundle is being executed.
Recently, an early version of removing the client side bundle was shipped to Next.js which doesn't suffer from performance problems caused by hydration. However, for a typical website you'll likely still need some JavaScript on the client side to deliver a reasonable user experience.
This is a Next.js plugin that is intended to be used in conjunction with disabled runtime JavaScript. You can add client bundles on a per-page basis that only sprinkle a tiny bit of JavaScript over otherwise completely static pages.
This allows for the same architecture that Netflix has chosen for their public pages.
Benefits:
The tradeoff is that you can't use any client-side features of React (state, effects, event handlers, …). Note that some features of Next.js might not be available (yet) – e.g. code splitting via dynamic within a page.
// ./src/client/index.ts
console.log('Hello from client.');
next.config.js and reference your client script.const withClientScripts = require('next-client-script/withClientScripts');
// Define which paths will cause which scripts to load
module.exports = withClientScripts({
'/': './src/client/index.ts',
// You can use parameters as provided by path-to-regexp to match routes dynamically.
'/products/:id': './src/client/product.ts'
})();
<ClientScript /> component as the last child in the body.+ import ClientScript from 'next-client-script/ClientScript';
// ...
+ <ClientScript />
</body>
// ./pages/index.ts
export const config = {
unstable_runtimeJS: false
};
Note that you can mix this approach with the traditional hydration approach, to optimize the performance of critical pages while keeping the flexibility of using React on the client side for other pages.
See the example folder for a project demonstrating this setup.
To help with a component-oriented approach for client-side code, this library contains convenience APIs that help with passing data to the client and initializing widgets.
Use the ClientWidget component to mark an entry point for the client side and to optionally pass in some data.
// Counter.js
import ClientWidget from 'next-client-script/ClientWidget';
import styles from './Counter.module.scss';
export default function Counter({initialCount = 2}) {
return (
<ClientWidget className={styles.root} data={{initialCount}}>
<p className={styles.label}>
Count: <span className={styles.count}>{initialCount}</span>
</p>
<button className={styles.button}>Increment</button>
</ClientWidget>
);
}
Now you can add a client part for this component that receives the data and adds interactivity.
// Counter.client.js
import styles from './Counter.module.scss';
export default function initCounter(rootNode, data) {
let count = data.initialCount;
const countNode = rootNode.querySelector(`.${styles.count}`);
const buttonNode = rootNode.querySelector(`.${styles.button}`);
buttonNode.addEventListener('click', () => {
count++;
countNode.textContent = count;
});
}
// This will be passed to `querySelectorAll` to find all widgets on the page
initCounter.selector = `.${styles.root}`;
As a last step, you need to reference the client counter code in your client script:
import initWidgets from 'next-client-script/initWidgets';
import Counter from 'components/Counter/Counter.client';
initWidgets([Counter]);
I really hope that React will solve hydration problems in the future with partial hydration and server-side components, but I think a tiny bit of vanilla JavaScript on the client side is really hard to beat.
FAQs
Add a separate client entry point to your Next.js pages.
The npm package next-client-script receives a total of 2 weekly downloads. As such, next-client-script popularity was classified as not popular.
We found that next-client-script 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
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.

Security News
crates.io adds a Security tab backed by RustSec advisories and narrows trusted publishing paths to reduce common CI publishing risks.

Research
/Security News
A Chrome extension claiming to hide Amazon ads was found secretly hijacking affiliate links, replacing creators’ tags with its own without user consent.