02a475d652
新增移动壳 deep link 到同源 H5 路径的解析与运行时监听 配置移动壳真实品牌图标、iOS associated domain 和 Android app link 补充移动壳配置守门、单测和宿主壳文档记忆
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { buildMobileShellUrl, type MobileShellUrlOptions } from './mobileShellUrl';
|
|
|
|
const supportedHosts = new Set(['open', 'app']);
|
|
|
|
function extractPathFromNativeUrl(url: URL) {
|
|
if (supportedHosts.has(url.hostname)) {
|
|
return `${url.pathname}${url.search}${url.hash}`;
|
|
}
|
|
|
|
return `${url.hostname ? `/${url.hostname}` : ''}${url.pathname}${url.search}${url.hash}`;
|
|
}
|
|
|
|
function resolveTargetPath(rawUrl: string, webOrigin: string) {
|
|
try {
|
|
const url = new URL(rawUrl);
|
|
if (url.protocol === 'genarrative:') {
|
|
return extractPathFromNativeUrl(url);
|
|
}
|
|
|
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
return null;
|
|
}
|
|
|
|
if (url.origin !== webOrigin) {
|
|
return null;
|
|
}
|
|
|
|
return `${url.pathname}${url.search}${url.hash}`;
|
|
} catch {
|
|
if (!rawUrl.startsWith('/') && !rawUrl.startsWith('#')) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const relativeUrl = new URL(rawUrl, webOrigin);
|
|
if (relativeUrl.origin !== webOrigin) {
|
|
return null;
|
|
}
|
|
|
|
return `${relativeUrl.pathname}${relativeUrl.search}${relativeUrl.hash}`;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function buildMobileShellUrlFromDeepLink(
|
|
rawUrl: string | null | undefined,
|
|
baseWebUrl: string,
|
|
options: MobileShellUrlOptions,
|
|
) {
|
|
const defaultUrl = buildMobileShellUrl(baseWebUrl, options);
|
|
if (!rawUrl) {
|
|
return defaultUrl;
|
|
}
|
|
|
|
const webOrigin = new URL(baseWebUrl).origin;
|
|
const targetPath = resolveTargetPath(rawUrl, webOrigin);
|
|
if (!targetPath) {
|
|
return defaultUrl;
|
|
}
|
|
|
|
return buildMobileShellUrl(new URL(targetPath, webOrigin).toString(), options);
|
|
}
|