-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Runtime and IPC only injected on index.html file #2047
Comments
Hi 👋 It only injects them on the first page at startup. I guess it was designed with SPAs in mind. One issue I can foresee is that there's an event at the bottom of the runtime to indicate it's loaded... How would that work per page? Maybe it just does.... I'd need to think about it a bit more. Adding a config to just add it to any HTML file being served up would be easy enough I guess. We need to consider what that means for a custom assets handler. |
Maybe adding something more configurable would be interesting, like a map of paths of html files where the runtime should be injected.
That should work already out of the box, since the injection is done as the very last step before serving the content to the WebViews. It already does injection of the runtime to the |
Yeah, the user defined function approach sounds great. |
Sure, or -- honestly, just adding the scripts myself is nice and easy to understand. It'd have to be documented, but I like having that control and transparency. Could the 2 files be combined into 1?
I typically run all my code after the page has loaded, like: function ready(fn) {
if (document.readyState != 'loading') fn();
else document.addEventListener('DOMContentLoaded', fn);
} And according to MDN, this event happens after deferred scripts :
(Although, on reading this, I have just learned about Does that help? Personally, I'd be OK with auto-injecting into any HTTP response with a |
I guess I was thinking of the scenario where currently people treat that as an "init" event and start modifying/loading state. This may have consequences if called multiple times. I'm erring on a multipronged approach:
Thoughts? |
Sure, that'd probably work for me at least. It does sound like more work, but I suppose the flexibility will be nice. |
I've started working on this, since we would need that also for multi window assets. Currently I took a simple approach to inject it on all index.html served and when for a directory request a html file is served. I think that should solve this for multi window assets and also in your case @mholt What are your thoughts on this approach? |
I suppose that's fine; although, any thoughts on my 3 proposed solutions in the OP? I'm not sure about implementation complexity but in theory they sound simple and consistent. |
Regarding your proposed solutions my very personal thoughts:
Personally I wouldn't prefer to include it in all html files. Maybe a user/framework uses html snippets which would be fetched and included dynamically, which would then probably fail to be loaded.
I think that would break all existing apps when not having a config option. And users would need to know the paths on the AssetServer to those files, which might change in the future.
I'm not sure this will work for plain JS projects, which don't have any frontend build process to have them included/imported. Wouldn't that also tightly couple the frontend to the backend regarding the low-level IPC communication channel? |
Those are good counter-points. Although for (1), maybe only inject on HTML documents that have a In any case, your solution could be fine too. Either way :) |
Another option came to mind: we only care about injecting it when the webview navigates to a new page. I believe there is a signal for this for each webview. Perhaps that could be used? I believe that would prevent the injection in XHR loaded snippets. |
That sounds promising as well! |
That would relatively tight bound the AssetServer to the WebView, currently the assetserver has no knowledge of the webview. Which I sort of like. One would need to correlate that event with the first load of a custom resource event. Furthermore I currently have no idea how that navigation could be detected when an external DevServer is used and a browser is used for developing. Or how this would work with a hybrid/sever mode. Saying it's auto-injected in any index.html is an easy constraint which is pretty error free to be implemented and easy explainable to users too. Seems to me like the generalisation of the current well known behaviour. |
Yeah external Dev server is a good observation. Initially I thought we could just add a query param to the URL from the webview but yeah, external dev server would not work. I'm ok with the "any index.html" approach. How about we have a config option that defaults to this?
We could just do a suffix match on it? (I'm open to better names 🤣) EDIT: there is another option which we kinda had but in reverse: add a meta tag in your HTML to indicate that you want to inject the runtime and IPC. This would work with any page and plain HTML projects |
Yeah we could do that. It would also be injected now if you request '/test/' which is a folder and that folder contains an index.html. So we would probably also need to add '/' to that runtime matchers if you want to have it injected when requesting '/' or '/test/'. Since we can't do http redirects to redirect those requests to '/index.html' or '/test/index.html'. Meta-Tag has one big drawback in my point of view. This completely defeats having the ability to stream any html page directly to the Webview without buffering it to determine if the file contains the meta tag. Or one would need sniffing a small portion of the file and one could probably miss the meta-tag. |
I've have code ready which now always injects into suffix @leaanthony do you want to add the matchers, which defaults to AssetServer: {
RuntimeInjectionMatchers: [
assetserver.PathSuffixMatch("/"),
assetserver.PathSuffixMatch("/index.html"),
],
}, Or should we go with the current hardcoded approach and add that later if we need it? |
I've published a draft PR #2203 which contains all common code that is needed to dynamically inject into multiple html files depending on a path precondition. At the moment the PR hardcoded injects into path suffixes "/" and "/index.html". It could easily be enhanced to support the mentioned |
I'd say let's make it configurable after 👍 |
Description
My frontend is vanilla JS, so I have multiple HTML files. It's a good-ol', boring website from the early 2000s. Relatively little client-side JS (no routing, etc).
I noticed that
runtime.js
andipc.js
are only injected forindex.html
:wails/v2/internal/frontend/assetserver/assetserver.go
Lines 168 to 187 in 638caf7
This came after some head-scratching as to why
Uncaught TypeError: window.go is undefined
errors kept appearing but it worked fine on the index page.Additionally, hot reloading doesn't seem to work on any non-index file, but maybe that's a separate issue.
To Reproduce
Expected behaviour
I think it would make more sense to either inject these on all HTML pages, or to not inject them at all.
Screenshots
No response
Attempted Fixes
Fortunately, I can work around this by manually adding them:
Proposed solutions:
System Details
The text was updated successfully, but these errors were encountered: