Skip to content

Commit

Permalink
fix: ssr issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Oct 2, 2023
1 parent efb3fea commit 4063384
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ import {Button, Modal, launchModal} from '@getalby/bitcoin-connect-react';
Programmatically launch modal
</button>
```

#### React SSR / NextJS

Make sure to only render the components **client side**. This can be done either by creating a wrapper component with 'use client' directive (NextJS app directory) or by using next/dynamic.

### Other Frameworks
*Use another popular framework? please let us know or feel free to create a PR for a wrapper. See the React package for an example implementation.*

_Use another popular framework? please let us know or feel free to create a PR for a wrapper. See the React package for an example implementation._

### Pure HTML

Expand Down Expand Up @@ -210,6 +216,10 @@ You should have a certain level of trust on the website you decide to connect yo
- [Alby NWC](https://nwc.getalby.com)
- [Generic NWC URL](https://github.com/nostr-protocol/nips/blob/master/47.md)

## Known Issues

- NWC connectors do not work on iOS in non-secure contexts because window.crypto.subtle is unavailable. If testing on your phone, please run an https server or use an https tunnel.

## 🔥 Lit

This project is powered by Lit.
Expand Down
9 changes: 6 additions & 3 deletions src/components/internal/InternalElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ export class InternalElement extends LitElement {
}

protected _getBrandColorLuminance() {
if (!globalThis.window) {
return 0;
}
const brandColor =
getComputedStyle(this as HTMLElement).getPropertyValue(
'--bc-color-brand'
) || '#196CE7';
window
.getComputedStyle(this as HTMLElement)
.getPropertyValue('--bc-color-brand') || '#196CE7';
function calculateLuminance(color: string) {
if (color.startsWith('#')) {
color = color.slice(1); // Remove the '#' character
Expand Down
15 changes: 14 additions & 1 deletion src/components/twind/withTwind.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {defineConfig} from '@twind/core';
import presetTailwind from '@twind/preset-tailwind';
import install from '@twind/with-web-components';
import {LitElement} from 'lit';

const colors = {
'brand-light': 'var(--bc-color-brand, #196CE7)',
Expand Down Expand Up @@ -74,4 +75,16 @@ export const withTwindExtended = () =>
})
);

export const withTwind = () => withTwindExtended();
export const withTwind = () => {
if (globalThis.window) {
return withTwindExtended();
} else {
// prevent SSR issues
return withNothing;
}
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function withNothing<T extends new (...args: any[]) => LitElement>(Base: T) {
return Base;
}
2 changes: 1 addition & 1 deletion src/components/utils/loadFonts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let appendedFonts = false;
export function loadFonts() {
if (appendedFonts) {
if (!globalThis.document || appendedFonts) {
return;
}
appendedFonts = true;
Expand Down
6 changes: 4 additions & 2 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,7 @@ function addEventListeners() {
});
}

loadConfig();
addEventListeners();
if (globalThis.window) {
loadConfig();
addEventListeners();
}

0 comments on commit 4063384

Please sign in to comment.