npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mep-agency/next-iubenda

v1.0.0-alpha5

Published

A React library for integrating Iubenda's Cookie Solution into Next.js projects

Downloads

921

Readme

Next-Iubenda

Do you use the Iubenda's Cookie Solution, but have trouble integrating it properly into your Next.js projects? Then this is the tool for you.

Our goal is to create a simple, yet powerful tool to do the following:

  • Display Iubenda's cookie banner, with the ability to customize every single configuration option
  • Make sure we can prevent code execution of components that may require specific consent if it's not granted, as well as have a fallback widget that provides users with a call to action to change their preferences
  • Take advantage of support for server-side and client-side components to improve performance
  • Provide developers with a good DX thanks to features like
    • Type safety for configuration options
    • Templates for basic needs and hooks for advanced use cases
    • reasonable styling defaults, but full customization options

This package gives you these features with minimal effort and an extremely small footprint on your codebase.

Missing features

  • Support to regulations other than the European GDPR
    Iubenda does a great job of supporting many regulations and helping developers make sure their websites/apps can be compliant worldwide. But this tool is currently limited to the European GDPR because that's what we know best. Any help in adding support for additional features is appreciated.
  • Shipping the package as pre-compiled
    Currently, this package is shipped as plain TypeScript files. While this doesn't seem to be a problem and may allow Next.js to better optimize the code, it requires the "transpilePackages" option to be enabled for this package, which may not be ideal. Any suggestions/contributions in this regard are appreciated.

Project lifecycle, contribution and support policy

Our policies are available on our main oranization page.

Original authors

Getting started

Simply install the package using any package manager:

# With Yarn
$ yarn add --dev @mep-agency/next-iubenda

# With NPM
$ npm install --save-dev @mep-agency/next-iubenda

At the moment this package is distributed as plain TypeScript code so you have to make sure Next.js will transpile it:

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ['@mep-agency/next-iubenda'],
};

module.exports = nextConfig;

Update your main layout file to wrapp any part of the page which will contain consent-aware components.

Here is an example with the App Router:

// ./app/layout.tsx

import './globals.css';
import { Inter } from 'next/font/google';
import { IubendaProvider, IubendaCookieSolutionBannerConfigInterface, i18nDictionaries } from '@mep-agency/next-iubenda';

const inter = Inter({ subsets: ['latin'] });

export const metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
};

const iubendaBannerConfig: IubendaCookieSolutionBannerConfigInterface = {
  siteId: 0000000, // Your site ID
  cookiePolicyId: 00000000, // Your cookie policy ID
  lang: 'en',

  // See https://www.iubenda.com/en/help/1205-how-to-configure-your-cookie-solution-advanced-guide
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <>
      <html lang="en">
        <body className={inter.className}>
          <IubendaProvider bannerConfig={iubendaBannerConfig}>
            // Any component at this level will have access to useIubenda()
            {children}
          </IubendaProvider>
        </body>
      </html>
    </>
  );
}

Now you can simply wrap any components with the <ConsentAwareWrapper /> provided by the library or access the useIubenda() directly.

Writing consent-aware components

In some cases you just need to toggle some components based on some requirments. This can be easily achieved using the <ConsentAwareWrapper />.

// ./app/page.tsx

import { ConsentAwareWrapper } from '@mep-agency/next-iubenda';

// ...
export default function Home() {
  return (
    <main>
      {/* ... */}
      <ConsentAwareWrapper requiredGdprPurposes={['experience', 'marketing']}>
        {/* Anything at this level won't be rendered unless the required permissions have been granted */}
        Both "experience" and "marketing" cookies can be used here!
      </ConsentAwareWrapper>
      {/* // ... */}
    </main>
  );
}
// ...

Advanced features

If you need more flexibility then you can use the useIubenda() hook directly:

// ./components/ConsentAwareComponent.tsx

'use client';

import { useIubenda } from '@mep-agency/next-iubenda';

const ConsentAwareComponent = () => {
  const {
    userPreferences, // The latest available user preferences data
    showCookiePolicy, // Displays the cookie policy popup
    openPreferences, // Opens the preferences panel
    showTcfVendors, // Opens the TCF vendors panel
    resetCookies, // Resets all cookies managed by Iubenda

    /*
     * The following exposed entries are meant for internal use only and should
     * not be used in your projects.
     */
    dispatchUserPreferences, // Update the user preferences data across the app
    i18nDictionary, // Contains the translations for the built-in components
  } = useIubenda();

  return (
    <div>
      User preferences status:{' '}
      <span style={{ color: userPreferences.hasBeenLoaded ? 'green' : 'red' }}>
        {userPreferences.hasBeenLoaded ? 'LOADED' : 'LOADING...'}
      </span>
    </div>
  );
};

export default ConsentAwareComponent;