fix(mobile): avoid YouTube embed error on iOS close #4958

This commit is contained in:
DIYgod
2026-04-10 10:52:40 +08:00
parent a0d5fafa68
commit 7aef66d500
4 changed files with 57 additions and 8 deletions
@@ -1,10 +1,11 @@
import { parseSafeUrl, transformVideoUrl } from "@follow/utils"
import { parseSafeUrl } from "@follow/utils"
import type { RefObject } from "react"
import { useCallback } from "react"
import type WebView from "react-native-webview"
import type { WebViewNavigation } from "react-native-webview"
import { openLink } from "../lib/native"
import { resolveVideoUrlForMobileOpen } from "../lib/video-url"
const allowHosts = new Set(["app.follow.is", "app.folo.is"])
export function useWebViewNavigation({ webViewRef }: { webViewRef: RefObject<WebView> }) {
@@ -18,7 +19,7 @@ export function useWebViewNavigation({ webViewRef }: { webViewRef: RefObject<Web
webViewRef.current?.stopLoading()
const formattedUrl = transformVideoUrl({ url: urlStr })
const formattedUrl = resolveVideoUrlForMobileOpen(urlStr)
if (formattedUrl) {
openLink(formattedUrl)
return
+36
View File
@@ -0,0 +1,36 @@
import { beforeAll, describe, expect, it, vi } from "vitest"
let resolveVideoUrlForMobileOpen: typeof import("./video-url").resolveVideoUrlForMobileOpen
beforeAll(async () => {
vi.stubGlobal("ELECTRON", false)
;({ resolveVideoUrlForMobileOpen } = await import("./video-url"))
})
describe("resolveVideoUrlForMobileOpen", () => {
it("keeps YouTube watch URLs unchanged for mobile browser opens", () => {
const url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
expect(resolveVideoUrlForMobileOpen(url)).toBe(url)
})
it("keeps YouTube shorts URLs unchanged for mobile browser opens", () => {
const url = "https://www.youtube.com/shorts/dQw4w9WgXcQ"
expect(resolveVideoUrlForMobileOpen(url)).toBe(url)
})
it("still transforms Bilibili links into the player URL", () => {
const url = "https://www.bilibili.com/video/BV1xx411c7mD"
const resolved = resolveVideoUrlForMobileOpen(url)
expect(resolved).toContain("https://www.bilibili.com/blackboard/newplayer.html?")
expect(resolved).toContain("bvid=BV1xx411c7mD")
})
it("falls back to the original URL when no transform is available", () => {
const url = "https://example.com/post/1"
expect(resolveVideoUrlForMobileOpen(url)).toBe(url)
})
})
+15
View File
@@ -0,0 +1,15 @@
import { parseSafeUrl, transformVideoUrl } from "@follow/utils"
const isYoutubeWatchUrl = (url: URL) =>
url.hostname === "www.youtube.com" &&
((url.pathname === "/watch" && url.searchParams.has("v")) || url.pathname.startsWith("/shorts/"))
export const resolveVideoUrlForMobileOpen = (url: string) => {
const parsedUrl = parseSafeUrl(url)
if (parsedUrl && isYoutubeWatchUrl(parsedUrl)) {
return url
}
return transformVideoUrl({ url }) || url
}
@@ -3,7 +3,7 @@ import { useEntry } from "@follow/store/entry/hooks"
import { unreadSyncService } from "@follow/store/unread/store"
import { useIsLoggedIn } from "@follow/store/user/hooks"
import { tracker } from "@follow/tracker"
import { formatDuration, transformVideoUrl } from "@follow/utils"
import { formatDuration } from "@follow/utils"
import { memo, useMemo } from "react"
import { useTranslation } from "react-i18next"
import { Linking, View } from "react-native"
@@ -15,6 +15,7 @@ import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable"
import { Text } from "@/src/components/ui/typography/Text"
import { openLink } from "@/src/lib/native"
import { toast } from "@/src/lib/toast"
import { resolveVideoUrlForMobileOpen } from "@/src/lib/video-url"
import { VideoContextMenu } from "../../context-menu/video"
import { EntryGridFooter } from "../../entry-content/EntryGridFooter"
@@ -141,10 +142,6 @@ const openVideo = async (url: string) => {
}
// Fallback to opening in in-app browser
const formattedUrl = openLinksInExternalApp
? url
: transformVideoUrl({
url,
}) || url
const formattedUrl = openLinksInExternalApp ? url : resolveVideoUrlForMobileOpen(url)
openLink(formattedUrl)
}