接入原生壳页面刷新能力
新增 app.reloadWebView HostBridge 契约和 H5 facade 移动端通过 react-native-webview reload 刷新当前 WebView 桌面端通过 Tauri WebviewWindow reload 刷新主窗口 更新壳能力检查、测试、方案文档和共享决策记录
This commit is contained in:
@@ -140,6 +140,7 @@ const requiredMainSnippets = [
|
||||
'"share.open"',
|
||||
'"share.setTarget"',
|
||||
'"navigation.openNativePage"',
|
||||
'"app.reloadWebView"',
|
||||
'"app.setTitle"',
|
||||
'"app.setBadgeCount"',
|
||||
'"clipboard.writeText"',
|
||||
@@ -162,6 +163,7 @@ const requiredMainSnippets = [
|
||||
'import_image_file_payload',
|
||||
'set_title',
|
||||
'set_badge_count',
|
||||
'window.reload()',
|
||||
'read_text()',
|
||||
'normalize_clipboard_text',
|
||||
'window.theme()',
|
||||
|
||||
@@ -97,6 +97,7 @@ fn capabilities() -> Vec<&'static str> {
|
||||
"share.open",
|
||||
"share.setTarget",
|
||||
"navigation.openNativePage",
|
||||
"app.reloadWebView",
|
||||
"app.openExternalUrl",
|
||||
"app.setTitle",
|
||||
"app.setBadgeCount",
|
||||
@@ -904,6 +905,13 @@ async fn host_bridge_request(
|
||||
None => failed(request.id, "host_error", "main window not found"),
|
||||
}
|
||||
}
|
||||
"app.reloadWebView" => match app.get_webview_window("main") {
|
||||
Some(window) => match window.reload() {
|
||||
Ok(()) => ok(request.id, json!(true)),
|
||||
Err(error) => failed(request.id, "host_error", error.to_string()),
|
||||
},
|
||||
None => failed(request.id, "host_error", "main window not found"),
|
||||
},
|
||||
"clipboard.writeText" => {
|
||||
let text = match required_string_payload(&request, "text") {
|
||||
Ok(text) => text,
|
||||
@@ -1208,6 +1216,10 @@ mod tests {
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&json!("navigation.openNativePage")));
|
||||
assert!(result["capabilities"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&json!("app.reloadWebView")));
|
||||
assert!(result["capabilities"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"build": {
|
||||
"beforeDevCommand": "npm --prefix ../.. run dev:web",
|
||||
"beforeBuildCommand": "npm --prefix ../.. run build:raw && npm run typecheck",
|
||||
"devUrl": "http://127.0.0.1:3000/?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,clipboard.readText,file.exportText,file.importText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"devUrl": "http://127.0.0.1:3000/?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.reloadWebView,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,clipboard.readText,file.exportText,file.importText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"frontendDist": "../../../dist"
|
||||
},
|
||||
"app": {
|
||||
@@ -14,7 +14,7 @@
|
||||
{
|
||||
"create": false,
|
||||
"label": "main",
|
||||
"url": "index.html?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,clipboard.readText,file.exportText,file.importText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"url": "index.html?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.reloadWebView,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,clipboard.readText,file.exportText,file.importText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"title": "Genarrative",
|
||||
"width": 1280,
|
||||
"height": 820,
|
||||
|
||||
@@ -58,6 +58,9 @@ export default function App() {
|
||||
openWebViewUrl(url) {
|
||||
setWebUrl(url);
|
||||
},
|
||||
reloadWebView() {
|
||||
webViewRef.current?.reload();
|
||||
},
|
||||
});
|
||||
|
||||
return () => configureMobileHostBridgeNavigation(null);
|
||||
|
||||
@@ -117,6 +117,7 @@ for (const snippet of [
|
||||
"Linking.addEventListener('url'",
|
||||
'buildMobileShellUrlFromDeepLink',
|
||||
'configureMobileHostBridgeNavigation',
|
||||
'webViewRef.current?.reload()',
|
||||
'AppState.addEventListener',
|
||||
'app.lifecycle',
|
||||
'network.statusChanged',
|
||||
@@ -184,6 +185,7 @@ for (const snippet of [
|
||||
'clipboard.readText',
|
||||
'notification.showLocal',
|
||||
'network.status',
|
||||
'app.reloadWebView',
|
||||
'getMobileNetworkStatus',
|
||||
'Notifications.scheduleNotificationAsync',
|
||||
'Notifications.setNotificationChannelAsync',
|
||||
@@ -222,6 +224,7 @@ for (const capability of [
|
||||
'network.statusChanged',
|
||||
'navigation.openNativePage',
|
||||
'navigation.canGoBack',
|
||||
'app.reloadWebView',
|
||||
'app.openExternalUrl',
|
||||
'clipboard.writeText',
|
||||
'clipboard.readText',
|
||||
|
||||
@@ -293,6 +293,7 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
'appearance.getColorScheme',
|
||||
'host.events',
|
||||
'app.lifecycle',
|
||||
'app.reloadWebView',
|
||||
'network.status',
|
||||
'network.statusChanged',
|
||||
'navigation.canGoBack',
|
||||
@@ -343,6 +344,7 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
configureMobileHostBridgeNavigation({
|
||||
allowedOrigin: 'https://app.genarrative.world',
|
||||
openWebViewUrl,
|
||||
reloadWebView: vi.fn(),
|
||||
});
|
||||
|
||||
const response = await send(
|
||||
@@ -361,6 +363,7 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
configureMobileHostBridgeNavigation({
|
||||
allowedOrigin: 'https://app.genarrative.world',
|
||||
openWebViewUrl: vi.fn(),
|
||||
reloadWebView: vi.fn(),
|
||||
});
|
||||
|
||||
const response = await send(
|
||||
@@ -386,6 +389,20 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
expect(failedResponse.error.code).toBe('unsupported_method');
|
||||
});
|
||||
|
||||
test('app.reloadWebView 刷新移动壳当前 WebView', async () => {
|
||||
const reloadWebView = vi.fn();
|
||||
configureMobileHostBridgeNavigation({
|
||||
allowedOrigin: 'https://app.genarrative.world',
|
||||
openWebViewUrl: vi.fn(),
|
||||
reloadWebView,
|
||||
});
|
||||
|
||||
const response = await send(request('app.reloadWebView'));
|
||||
|
||||
expectOk(response);
|
||||
expect(reloadWebView).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('app.openExternalUrl 只打开允许的外链协议', async () => {
|
||||
const response = await send(
|
||||
request('app.openExternalUrl', {
|
||||
|
||||
@@ -82,6 +82,7 @@ export const MOBILE_HOST_CAPABILITIES: HostBridgeCapability[] = [
|
||||
'share.setTarget',
|
||||
'navigation.openNativePage',
|
||||
'navigation.canGoBack',
|
||||
'app.reloadWebView',
|
||||
'app.openExternalUrl',
|
||||
'network.status',
|
||||
'network.statusChanged',
|
||||
@@ -109,6 +110,7 @@ export function resolveMobileHostCapabilities(platform = Platform.OS) {
|
||||
export type MobileHostBridgeNavigation = {
|
||||
allowedOrigin: string;
|
||||
openWebViewUrl: (url: string) => void;
|
||||
reloadWebView: () => void;
|
||||
};
|
||||
|
||||
let currentShareTarget: unknown = null;
|
||||
@@ -692,6 +694,15 @@ function openNativePage(payload: unknown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function reloadWebView() {
|
||||
if (!navigation) {
|
||||
throw unsupported('app.reloadWebView');
|
||||
}
|
||||
|
||||
navigation.reloadWebView();
|
||||
return true;
|
||||
}
|
||||
|
||||
async function handleRequest(request: HostBridgeRequest) {
|
||||
switch (request.method) {
|
||||
case 'host.getRuntime':
|
||||
@@ -706,6 +717,8 @@ async function handleRequest(request: HostBridgeRequest) {
|
||||
return ok(request, getColorScheme());
|
||||
case 'app.openExternalUrl':
|
||||
return ok(request, await openExternalUrl(request.payload));
|
||||
case 'app.reloadWebView':
|
||||
return ok(request, reloadWebView());
|
||||
case 'network.status':
|
||||
return ok(request, await getMobileNetworkStatus());
|
||||
case 'clipboard.writeText':
|
||||
|
||||
@@ -2283,3 +2283,10 @@
|
||||
- 影响范围:`api-server` 资产计费包裹、钱包退款补偿、拼图首图后台生成、`spacetime-module` 拼图 task 表、`spacetime-client` bindings/facade、前端 API request id 复用和后端架构文档。
|
||||
- 验证方式:`npm run spacetime:generate`、`npm run check:spacetime-schema`、`npm run check:spacetime-runtime-access`、`node scripts/check-server-rs-ddd-boundaries.mjs`、`cargo check -p api-server --manifest-path server-rs/Cargo.toml`、`cargo test -p api-server --manifest-path server-rs/Cargo.toml wallet_refund_outbox`、`cargo test -p api-server --manifest-path server-rs/Cargo.toml asset_operation`、`npm run test -- src/services/apiClient.test.ts`、`npm run check:encoding`。
|
||||
- 关联文档:`docs/【后端架构】server-rs与SpacetimeDB数据契约-2026-05-15.md`。
|
||||
|
||||
## 2026-06-18 原生壳 WebView 刷新能力只保留受控当前页刷新
|
||||
|
||||
- 背景:Expo 移动壳和 Tauri 桌面壳都需要一个真实的宿主级刷新入口,供 H5 在检测到资源、登录态或运行态需要重新载入时请求宿主刷新当前容器;该能力不能演变成任意 URL 导航或原生 WebView ref 透传。
|
||||
- 决策:新增 HostBridge method `app.reloadWebView` 和 H5 facade `reloadHostWebView()`。移动端只调用当前 `react-native-webview` 的 `reload()`,桌面端只调用 Tauri 主 `WebviewWindow.reload()`;该 method 不接受 payload,成功只表示宿主已发起刷新,刷新后当前 H5 上下文会卸载。继续把同源跳转留给 `navigation.openNativePage`,外链离开容器留给 `app.openExternalUrl`。
|
||||
- 影响范围:`packages/shared/src/contracts/hostBridge.ts`、`src/services/host-bridge/hostBridge.ts`、`apps/mobile-shell/`、`apps/desktop-shell/`、原生壳能力检查脚本和 HostBridge 架构文档。
|
||||
- 验证方式:`npm run check:native-shells`、`npm run typecheck`、`npm run check:encoding`、`git diff --check`。
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -54,6 +54,7 @@ AI H5 sandbox
|
||||
- `showHostLocalNotification()`:原生 App 宿主的受控即时本地通知入口。H5 只能传必填 `title` 和可选 `body`,两者都会去除首尾空白、折叠普通空白、限制长度并拒绝控制字符;Expo 移动壳通过 `expo-notifications` 请求通知权限、创建 Android 本地通知 channel 并立刻调度本地通知,Tauri 桌面壳通过 Rust 侧 `tauri-plugin-notification` 发送系统通知。该能力不包含远程推送、token 注册、定时提醒、后台远程通知或任意通知插件透传,宿主未声明、权限拒绝或系统失败时由 H5 视作失败并继续主流程。
|
||||
- `setHostAppTitle()`:原生 App 宿主的受控窗口标题入口。H5 主站会按当前平台阶段先同步 `document.title`,再通过 `app.setTitle` 请求宿主窗口标题同步;Tauri 桌面壳支持该能力,Expo 移动壳不声明时静默忽略。
|
||||
- `setHostAppBadgeCount()`:原生 App 宿主的受控应用角标入口。H5 只传 `0-99999` 的整数,`0` 表示清除角标;Expo 移动壳只在 iOS 声明 `app.setBadgeCount` 并通过 React Native `PushNotificationIOS` 设置应用图标角标,Android 不声明该能力;Tauri 桌面壳通过主窗口 `set_badge_count` 设置任务栏角标,底层平台不支持时返回明确错误,由 H5 视作失败并继续主流程。
|
||||
- `reloadHostWebView()`:原生 App 宿主的受控 WebView 刷新入口。H5 只能请求刷新当前承载主站的宿主 WebView;Expo 移动壳调用当前 `react-native-webview` 的 `reload()`,Tauri 桌面壳调用主 `WebviewWindow.reload()`。该能力不接受 payload,不开放任意 URL 导航、脚本执行、Tauri guest API 或 RN WebView ref;成功只表示宿主已发起刷新,刷新后当前 H5 上下文会卸载。
|
||||
- `openHostExternalUrl()`:原生 App 宿主的受控外链入口。H5 中需要离开主站的外链在 `native_app` 下先通过 `app.openExternalUrl` 请求宿主系统浏览器打开;只允许 `http:`、`https:`、`mailto:`、`tel:`,相对路径会先归一化到当前站点绝对 URL。宿主不可用或拒绝时回退浏览器外链行为,普通浏览器和小程序保持原有 `<a>` 语义。
|
||||
- `navigateHostNativePage()`:受控跳转宿主页,供订阅授权、支付、登录等 adapter 复用。Expo 移动壳首版只接受同源 H5 route 并切换 WebView URL;Tauri 桌面壳同样只接受 `https://app.genarrative.world` 同源 H5 route 并在主窗口内跳转。真正原生页面、登录和支付能力必须等对应 SDK / 页面接入后再声明支持。
|
||||
- `exportHostTextFile()`:原生 App 宿主的受控文本导出入口。Expo 移动壳通过 `file.exportText` 写入缓存文本文件并交给系统分享 / 保存面板;Tauri 桌面壳通过 `file.exportText` 打开系统保存对话框并写入用户选择的文件。文件名必须清洗,单次文本不超过 5 MiB,成功只返回文件名和字节数,不把本机绝对路径暴露给 H5;系统分享不可用或用户取消时返回明确错误,由 H5 fallback 承接。
|
||||
|
||||
@@ -53,6 +53,7 @@ describe('HostBridge shared contract helpers', () => {
|
||||
test('识别 HostBridge 能力白名单', () => {
|
||||
expect(isHostBridgeCapability('appearance.getColorScheme')).toBe(true);
|
||||
expect(isHostBridgeCapability('share.open')).toBe(true);
|
||||
expect(isHostBridgeCapability('app.reloadWebView')).toBe(true);
|
||||
expect(isHostBridgeCapability('app.lifecycle')).toBe(true);
|
||||
expect(isHostBridgeCapability('network.status')).toBe(true);
|
||||
expect(isHostBridgeCapability('network.statusChanged')).toBe(true);
|
||||
|
||||
@@ -21,6 +21,7 @@ export const HOST_BRIDGE_METHODS = [
|
||||
'share.setTarget',
|
||||
'share.open',
|
||||
'navigation.openNativePage',
|
||||
'app.reloadWebView',
|
||||
'app.openExternalUrl',
|
||||
'app.setTitle',
|
||||
'app.setBadgeCount',
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
postWechatMiniProgramMessage,
|
||||
readHostClipboardText,
|
||||
refreshNativeAppHostRuntime,
|
||||
reloadHostWebView,
|
||||
requestHostHapticsImpact,
|
||||
requestHostLogin,
|
||||
requestHostPayment,
|
||||
@@ -632,6 +633,7 @@ describe('hostBridge', () => {
|
||||
'clipboard.writeText',
|
||||
'haptics.impact',
|
||||
'app.openExternalUrl',
|
||||
'app.reloadWebView',
|
||||
'app.setTitle',
|
||||
'app.setBadgeCount',
|
||||
'network.status',
|
||||
@@ -676,6 +678,7 @@ describe('hostBridge', () => {
|
||||
await expect(
|
||||
openHostExternalUrl({ url: '/works/detail?work=PZ-1' }),
|
||||
).resolves.toBe(true);
|
||||
await expect(reloadHostWebView()).resolves.toBe(true);
|
||||
await expect(setHostAppTitle({ title: ' 拼图 - 陶泥儿 ' })).resolves.toBe(
|
||||
true,
|
||||
);
|
||||
@@ -775,6 +778,11 @@ describe('hostBridge', () => {
|
||||
},
|
||||
}),
|
||||
});
|
||||
expect(invoke).toHaveBeenCalledWith('host_bridge_request', {
|
||||
request: expect.objectContaining({
|
||||
method: 'app.reloadWebView',
|
||||
}),
|
||||
});
|
||||
expect(invoke).toHaveBeenCalledWith('host_bridge_request', {
|
||||
request: expect.objectContaining({
|
||||
method: 'app.setTitle',
|
||||
@@ -882,6 +890,7 @@ describe('hostBridge', () => {
|
||||
await expect(
|
||||
openHostExternalUrl({ url: 'javascript:alert(1)' }),
|
||||
).resolves.toBe(false);
|
||||
await expect(reloadHostWebView()).resolves.toBe(false);
|
||||
await expect(setHostAppTitle({ title: '拼图 - 陶泥儿' })).resolves.toBe(
|
||||
false,
|
||||
);
|
||||
@@ -916,6 +925,7 @@ describe('hostBridge', () => {
|
||||
test('普通浏览器不处理宿主文件导出', async () => {
|
||||
await expect(getHostAppearanceColorScheme()).resolves.toBe(false);
|
||||
await expect(readHostClipboardText()).resolves.toBe(false);
|
||||
await expect(reloadHostWebView()).resolves.toBe(false);
|
||||
await expect(
|
||||
exportHostTextFile({
|
||||
fileName: '作品记录.txt',
|
||||
|
||||
@@ -642,6 +642,18 @@ export async function openHostExternalUrl({ url }: HostExternalUrlRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function reloadHostWebView() {
|
||||
if (!canUseNativeHostCapability('app.reloadWebView')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return await requestNativeHostBoolean('app.reloadWebView');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function postWechatMiniProgramMessage(message: unknown) {
|
||||
return setHostShareTarget(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user