Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDS-2244: SSR error document not defined #1270

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [Component] What has been changed
- [Notification] Change auto closing notification progressbar to decrease instead of increase.
- [CookieConsent] Fixed SSR problem with "document not defined"

#### Fixed

Expand Down
11 changes: 9 additions & 2 deletions packages/react/src/components/cookieConsent/cookieController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import cookie, { CookieSerializeOptions } from 'cookie';

import { isSsrEnvironment } from '../../utils/isSsrEnvironment';

export type CookieSetOptions = CookieSerializeOptions;

export const defaultCookieSetOptions: CookieSetOptions = {
Expand All @@ -10,6 +12,9 @@ export const defaultCookieSetOptions: CookieSetOptions = {
};

function getAll() {
if (isSsrEnvironment()) {
return {};
}
return cookie.parse(document.cookie);
}

Expand All @@ -19,7 +24,9 @@ function getNamedCookie(name: string): string | undefined {
}

function setNamedCookie(name: string, value: string, options?: CookieSerializeOptions) {
document.cookie = cookie.serialize(name, value, options);
if (!isSsrEnvironment()) {
document.cookie = cookie.serialize(name, value, options);
}
}

function createCookieController(
Expand All @@ -34,7 +41,7 @@ function createCookieController(
...options,
};

if (typeof window === 'undefined') {
if (isSsrEnvironment()) {
return {
get: () => '',
set: () => '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CookieSerializeOptions, parse } from 'cookie';

import { isSsrEnvironment } from '../../utils/isSsrEnvironment';
import { createCookieController, setNamedCookie, defaultCookieSetOptions, getAll } from './cookieController';

export type CookieSetOptions = CookieSerializeOptions;
Expand All @@ -10,7 +11,7 @@ export const VERSION_COOKIE_NAME = 'city-of-helsinki-consent-version';

// the old version how default cookie domain was picked
function getCookieDomainForMultiDomainAccess(): string {
if (typeof window === 'undefined') {
if (isSsrEnvironment()) {
return '';
}

Expand All @@ -19,7 +20,7 @@ function getCookieDomainForMultiDomainAccess(): string {

// the new version how to pick default cookie domain
export function getCookieDomainForSubDomainAccess(): string {
if (typeof window === 'undefined') {
if (isSsrEnvironment()) {
return '';
}

Expand Down Expand Up @@ -53,6 +54,9 @@ export function createCookieStorageProxy(
};

const getConsentCookies = (): string[] => {
if (isSsrEnvironment()) {
return [];
}
const cookies = document.cookie || '';
if (!cookies || cookies.indexOf('=') < 0) {
return [];
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/hooks/useTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useRef } from 'react';
import { uniqueId } from 'lodash';

import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
import { isSsrEnvironment } from '../utils/isSsrEnvironment';

/**
* Sets the given custom theme for the component
Expand All @@ -11,7 +12,7 @@ import useIsomorphicLayoutEffect from './useIsomorphicLayoutEffect';
* @return The custom class name that should be applied to the component
*/
const setComponentTheme = <T,>(selector: string, theme: T, customClass: string): void => {
if (typeof window === 'undefined') return;
if (isSsrEnvironment()) return;

// checks if the given css rule contains the custom class selector
const hasCustomRule = (rule: CSSStyleRule): boolean => rule.selectorText?.includes(`${selector}.${customClass}`);
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/utils/isSsrEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isSsrEnvironment = () => {
return typeof window === 'undefined' || typeof document === 'undefined';
};
Loading