Skip to content

Commit

Permalink
[notifications] Guard notifications from loading badgin in SSR (expo#…
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored Oct 22, 2020
1 parent 94d499f commit 2cf9762
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/expo-notifications/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Removed the large icon from managed workflow. ([#10492](https://github.com/expo/expo/pull/10492) by [@lukmccall](https://github.com/lukmccall))
- Fixed crash happening due to non-existent `ExpoNotificationsService` being declared in `AndroidManifest.xml`. ([#10638](https://github.com/expo/expo/pull/10638) by [@sjchmiela](https://github.com/sjchmiela))
- Fixed notifications _not_ playing any sound when `shouldShowAlert: false` but `shouldPlaySound: true` in `setNotificationHandler`. ([#10699](https://github.com/expo/expo/pull/10699) by [@cruzach](https://github.com/cruzach))
- Add guard against badgin usage in SSR environments. ([#10741](https://github.com/expo/expo/pull/10741) by [@bycedric](https://github.com/bycedric))

## 0.7.1 — 2020-08-26

Expand Down
4 changes: 2 additions & 2 deletions packages/expo-notifications/build/BadgeModule.web.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions packages/expo-notifications/build/BadgeModule.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-notifications/build/BadgeModule.web.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions packages/expo-notifications/src/BadgeModule.web.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import * as badgin from 'badgin';

import { BadgeModule } from './BadgeModule.types';

let lastSetBadgeCount = 0;

export default {
const badgeModule: BadgeModule = {
addListener: () => {},
removeListeners: () => {},
getBadgeCountAsync: async () => {
return lastSetBadgeCount;
},
setBadgeCountAsync: async (badgeCount: number, options?: badgin.Options) => {
setBadgeCountAsync: async (badgeCount, options) => {
// If this module is loaded in SSR (NextJS), we can't modify the badge.
// It also can't load the badgin module, that instantly invokes methods on window.
if (typeof window === 'undefined') {
return false;
}
const badgin = require('badgin');
if (badgeCount > 0) {
badgin.set(badgeCount, options);
} else {
Expand All @@ -19,4 +23,6 @@ export default {
lastSetBadgeCount = badgeCount;
return true;
},
} as BadgeModule;
};

export default badgeModule;

0 comments on commit 2cf9762

Please sign in to comment.