From 763c7708f4bc6a81d788e4a180dae18d67c01a71 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 15 Aug 2023 20:18:43 +1000 Subject: [PATCH] Fix HMR. Better logging. --- v3/internal/assetserver/assetserver.go | 7 +++- v3/pkg/application/application.go | 2 +- v3/pkg/application/application_debug.go | 4 +-- v3/pkg/application/webview_window_windows.go | 36 +++++++++++++------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/v3/internal/assetserver/assetserver.go b/v3/internal/assetserver/assetserver.go index 12215f4b..1a18a405 100644 --- a/v3/internal/assetserver/assetserver.go +++ b/v3/internal/assetserver/assetserver.go @@ -21,6 +21,9 @@ const ( flagsPath = "/wails/flags" ) +const webViewRequestHeaderWindowId = "x-wails-window-id" +const webViewRequestHeaderWindowName = "x-wails-window-name" + type RuntimeAssets interface { DesktopIPC() []byte WebsocketIPC() []byte @@ -135,7 +138,9 @@ func (d *AssetServer) AddPluginScript(pluginName string, script string) { } func (d *AssetServer) logRequest(req *http.Request, code int) { - d.logger.Info("AssetServer:", "code", code, "method", req.Method, "url", req.URL.Path) + windowName := req.Header.Get(webViewRequestHeaderWindowName) + windowID := req.Header.Get(webViewRequestHeaderWindowId) + d.logger.Info("AssetServer:", "code", code, "windowName", windowName, "windowID", windowID, "method", req.Method, "url", req.URL.Path) } func (d *AssetServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 998fe1d5..8a593d41 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -44,7 +44,7 @@ func New(appOptions Options) *App { mergeApplicationDefaults(&appOptions) - result := newApplication(&appOptions) + result := newApplication(appOptions) globalApplication = result if result.isDebugMode && result.Logger == nil { diff --git a/v3/pkg/application/application_debug.go b/v3/pkg/application/application_debug.go index a9a6928d..66ae3aaf 100644 --- a/v3/pkg/application/application_debug.go +++ b/v3/pkg/application/application_debug.go @@ -28,10 +28,10 @@ func init() { } // We use this to patch the application to production mode. -func newApplication(options *Options) *App { +func newApplication(options Options) *App { result := &App{ isDebugMode: true, - options: options.getOptions(true), + options: options, } result.init() return result diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index f6fe1f68..217f6506 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -1327,7 +1327,7 @@ func (w *windowsWebviewWindow) processRequest(req *edge.ICoreWebView2WebResource if reqUri.Scheme != "http" { // Let the WebView2 handle the request with its default handler return - } else if reqUri.Host != "wails.localhost" { + } else if !strings.HasPrefix(reqUri.Host, "wails.localhost") { // Let the WebView2 handle the request with its default handler return } @@ -1462,21 +1462,31 @@ func (w *windowsWebviewWindow) setupChromium() { } else { var startURL = "http://wails.localhost" if globalApplication.options.Assets.ExternalURL != "" { - startURL = globalApplication.options.Assets.ExternalURL - } else if w.parent.options.URL != "" { - // parse the url - parsedURL, err := url.Parse(w.parent.options.URL) + // Parse the port + parsedURL, err := url.Parse(globalApplication.options.Assets.ExternalURL) if err != nil { - globalApplication.fatal("Error parsing URL: " + err.Error()) + globalApplication.fatal("Error parsing ExternalURL: " + err.Error()) } - if parsedURL.Scheme == "" { - startURL = path.Join(startURL, w.parent.options.URL) - // if the original URL had a trailing slash, add it back - if strings.HasSuffix(w.parent.options.URL, "/") { - startURL = startURL + "/" + port := parsedURL.Port() + if port != "" { + startURL += ":" + port + } + } else { + if w.parent.options.URL != "" { + // parse the url + parsedURL, err := url.Parse(w.parent.options.URL) + if err != nil { + globalApplication.fatal("Error parsing URL: " + err.Error()) + } + if parsedURL.Scheme == "" { + startURL = path.Join(startURL, w.parent.options.URL) + // if the original URL had a trailing slash, add it back + if strings.HasSuffix(w.parent.options.URL, "/") { + startURL = startURL + "/" + } + } else { + startURL = w.parent.options.URL } - } else { - startURL = w.parent.options.URL } } chromium.Navigate(startURL)