Cookie Banner
This plugin adds a cookie banner to your Next.js Supabase application. It's a simple component that will store the user's consent in a cookie. This banner will stop appearing once the user has given their consent - or rejected it.
This plugin adds a cookie banner to your Next.js application. It's a simple component that will store the user's consent in a cookie. This banner will stop appearing once the user has given their consent - or rejected it.
Using the Plugin
Installation
To install the plugin, you can use git subtrees from your original repository:
bashgit subtree add --prefix plugins/cookie-banner git@github.com:nextkit/next-supabase-saas-kit-plugins.git cookie-banner --squashAfter running this command, you will have the plugin in your repository at plugins/cookie-banner. Once pulled, you can apply any customization you need.
Using the CLI
If you're using the CLI, you can run the following command to install the plugin:
bashnpx @nextkit/cli plugins installFollow the instructions to install the plugin.
If the installation fails
Some users are not able to install using the GitHub SSH URL. If you're having issues with that:
- properly set up SSH access to GitHub with your SSH key
- use the HTTPS URL instead of the SSH URL
To use the HTTPS URL, you can run the following command:
bashgit subtree add --prefix plugins/cookie-banner https://github.com/nextkit/next-supabase-saas-kit-plugins cookie-banner --squashTranslations
Add the translations to your public/locales/en/common.json file:
json"cookieBanner": {
"title": "Hi, we use cookies.",
"description": "This website uses cookies to ensure you get the best
experience on our website.",
"reject": "Reject",
"accept": "Accept"
}Importing the Plugin
You can import the CookieBanner component from the plugin in your _app. tsx file and add it to your application:
tsximport dynamic from 'next/dynamic';
const CookieBanner = dynamic(
() => import('~/plugins/cookie-banner/CookieBanner'),
{
ssr: false,
},
);
export default function App() {
return (
<>
<CookieBanner />
{/* ... */}
</>
);
}API
To retrieve the current consent status, you can use the useCookieConsent:
tsximport { useCookieConsent } from 'plugins/cookie-banner';
function MyComponent() {
const consent = useCookieConsent();
// ...
}You can use this hook to decide whether to load third-party scripts or not. For example, you can use it to load Google Analytics only if the user has consented:
tsximport { useCookieConsent } from 'plugins/cookie-banner';
function MyAnalyticsScript() {
const consent = useCookieConsent();
if (consent === 'accepted') {
return (
<script
async
src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-X"
/>
);
}
return null;
}Keeping the plugin up to date
To keep the plugin up to date, you can use git subtrees again:
bashgit subtree pull --prefix plugins/cookie-banner git@github.com:nextkit/next-supabase-saas-kit-plugins.git cookie-banner --squash