Skip to content

Commit

Permalink
refactor(experience): skip non-object messages in native (#5491)
Browse files Browse the repository at this point in the history
* refactor(experience): skip non-object messages in native

* chore: add changeset
  • Loading branch information
gao-sun committed Mar 12, 2024
1 parent 213d6f9 commit 5a72045
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .changeset/silent-singers-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@logto/experience": patch
---

skip non-object messages in the native environment

In the `WKWebView` of new iOS versions, some script will constantly post messages to the
window object with increasing numbers as the message content ("1", "2", "3", ...).

Ideally, we should check the source of the message with Logto-specific identifier in the
`event.data`; however, this change will result a breaking change for the existing
native SDK implementations. Add the `isObject` check to prevent the crazy messages while
keeping the backward compatibility.
10 changes: 9 additions & 1 deletion packages/experience/src/hooks/use-native-message-listener.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isObject } from '@silverhand/essentials';
import { useEffect } from 'react';

import { isNativeWebview } from '@/utils/native-sdk';
Expand All @@ -23,7 +24,14 @@ const useNativeMessageListener = (bypass = false) => {
}

const nativeMessageHandler = (event: MessageEvent) => {
if (event.origin === window.location.origin) {
// In the `WKWebView` of new iOS versions, some script will constantly post messages to the
// window object with increasing numbers as the message content ("1", "2", "3", ...).
//
// Ideally, we should check the source of the message with Logto-specific identifier in the
// `event.data`; however, this change will result a breaking change for the existing
// native SDK implementations. Add the `isObject` check to prevent the crazy messages while
// keeping the backward compatibility.
if (event.origin === window.location.origin && isObject(event.data)) {
try {
setToast(JSON.stringify(event.data));
} catch {}
Expand Down

0 comments on commit 5a72045

Please sign in to comment.