Skip to content

Use a custom protocol when loading HTML#71

Merged
just-be-dev merged 5 commits intomainfrom
html-custom-protocol
Sep 29, 2024
Merged

Use a custom protocol when loading HTML#71
just-be-dev merged 5 commits intomainfrom
html-custom-protocol

Conversation

@just-be-dev
Copy link
Copy Markdown
Owner

Fixes #70

When I first reported tauri-apps/wry#1367, @amrbashir recommended I use a custom protocol instead of with_html. I hadn't experimented enough with custom protocols at the time and didn't real understand the tradeoffs between that an with_html. The challenge with using with_html is that the origin is null which means the page is considered to be in an insecure context. That disables a lot of features (like clipboard, noted in #70). The other challenge with with_html is that when you reload you lose the contents of the page (which is what tauri-apps/wry#1367 was all about). I'd implemented load_html in tauri-apps/wry#1368, but that really doesn't get around the other problems of with_html.

This PR implements a custom protocol called load-html://. The request itself is ignored, it's just used to trigger the protocol load. There's a RefCell in the scope of the process where the HTML is cached. When the webview is initialized with html, that initial HTML string is loaded into the ref cell and the webview is initialized with the url load-html://init. Likewise, when webview.loadHtml is called, it sets the refcell and loads load-html://load/<id>.

@just-be-dev just-be-dev force-pushed the html-custom-protocol branch 2 times, most recently from d4644d4 to d89bcfd Compare September 29, 2024 01:43
When using `html` as an option for constructing a webview, you may want to have a finer grained control over the origin. This change allows you to specify the origin both during creation of the webview and at `load_html` time.
@just-be-dev just-be-dev merged commit dc31e5c into main Sep 29, 2024
@just-be-dev just-be-dev deleted the html-custom-protocol branch September 29, 2024 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clipboard unavailable when using html to create a webview.

1 participant