0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-10 22:22:45 -05:00

refactor(experience): skip non-object messages in native (#5491)

* refactor(experience): skip non-object messages in native

* chore: add changeset
This commit is contained in:
Gao Sun 2024-03-12 11:27:18 +08:00 committed by GitHub
parent 213d6f97a4
commit 5a7204571a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -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.

View file

@ -1,3 +1,4 @@
import { isObject } from '@silverhand/essentials';
import { useEffect } from 'react';
import { isNativeWebview } from '@/utils/native-sdk';
@ -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 {}