From a53af63397c0bbbb6d10915f95ce2d26a2531d9a Mon Sep 17 00:00:00 2001 From: Brett Blashko Date: Tue, 20 Aug 2024 12:47:19 -0700 Subject: [PATCH] (ios) Added a NSURLRequestReloadIgnoringLocalCacheData policy when loading requests --- .../CDVWebViewEngine/CDVWebViewEngine.m | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m index 5c0ad5990..346ddb49e 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m @@ -63,7 +63,7 @@ - (nullable instancetype)initWithFrame:(CGRect)frame configuration:(nullable WKW if (NSClassFromString(@"WKWebView") == nil) { return nil; } - + self.configuration = configuration; self.engineWebView = configuration ? [[WKWebView alloc] initWithFrame:frame configuration:configuration] : [[WKWebView alloc] initWithFrame:frame]; } @@ -85,7 +85,7 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti configuration = [[WKWebViewConfiguration alloc] init]; configuration.processPool = [[CDVWebViewProcessPoolFactory sharedFactory] sharedProcessPool]; } - + if (settings == nil) { return configuration; } @@ -155,7 +155,7 @@ - (WKWebViewConfiguration*) createConfigurationFromSettings:(NSDictionary*)setti } else if ([contentMode isEqual: @"desktop"]) { configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeDesktop; } - + } #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 @@ -333,26 +333,35 @@ - (BOOL)shouldReloadWebView:(NSURL*)location title:(NSString*)title - (id)loadRequest:(NSURLRequest*)request { - if ([self canLoadRequest:request]) { // can load, differentiate between file urls and other schemes - if(request.URL.fileURL && self.cdvIsFileScheme) { - NSURL* readAccessUrl = [request.URL URLByDeletingLastPathComponent]; - return [(WKWebView*)_engineWebView loadFileURL:request.URL allowingReadAccessToURL:readAccessUrl]; - } else if (request.URL.fileURL) { + // Set the cache policy to not use the cache, so that we don't have diskcache issues when updating cordova versions, removing plugins, etc. + NSURL *url = request.URL; + NSURLRequest *noCacheRequest = [NSURLRequest requestWithURL:url + cachePolicy:NSURLRequestReloadIgnoringLocalCacheData + timeoutInterval:request.timeoutInterval]; + + // Log the cache policy + NSLog(@"Cache policy set to: %lu", (unsigned long)noCacheRequest.cachePolicy); + + if ([self canLoadRequest:noCacheRequest]) { // can load, differentiate between file urls and other schemes + if(noCacheRequest.URL.fileURL && self.cdvIsFileScheme) { + NSURL* readAccessUrl = [noCacheRequest.URL URLByDeletingLastPathComponent]; + return [(WKWebView*)_engineWebView loadFileURL:noCacheRequest.URL allowingReadAccessToURL:readAccessUrl]; + } else if (noCacheRequest.URL.fileURL) { NSURL* startURL = [NSURL URLWithString:((CDVViewController *)self.viewController).startPage]; NSString* startFilePath = [self.commandDelegate pathForResource:[startURL path]]; - NSURL *url = [[NSURL URLWithString:self.CDV_ASSETS_URL] URLByAppendingPathComponent:request.URL.path]; - if ([request.URL.path isEqualToString:startFilePath]) { + NSURL *url = [[NSURL URLWithString:self.CDV_ASSETS_URL] URLByAppendingPathComponent:noCacheRequest.URL.path]; + if ([noCacheRequest.URL.path isEqualToString:startFilePath]) { url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.CDV_ASSETS_URL, startURL]]; } - if(request.URL.query) { - url = [NSURL URLWithString:[@"?" stringByAppendingString:request.URL.query] relativeToURL:url]; + if(noCacheRequest.URL.query) { + url = [NSURL URLWithString:[@"?" stringByAppendingString:noCacheRequest.URL.query] relativeToURL:url]; } - if(request.URL.fragment) { - url = [NSURL URLWithString:[@"#" stringByAppendingString:request.URL.fragment] relativeToURL:url]; + if(noCacheRequest.URL.fragment) { + url = [NSURL URLWithString:[@"#" stringByAppendingString:noCacheRequest.URL.fragment] relativeToURL:url]; } - request = [NSURLRequest requestWithURL:url]; + noCacheRequest = [NSURLRequest requestWithURL:url]; } - return [(WKWebView*)_engineWebView loadRequest:request]; + return [(WKWebView*)_engineWebView loadRequest:noCacheRequest]; } else { // can't load, print out error NSString* errorHtml = [NSString stringWithFormat: @"" @@ -362,7 +371,7 @@ - (id)loadRequest:(NSURLRequest*)request @"

Most likely the cause of the error is that the loading of file urls is not supported in iOS %@.

" @"", NSStringFromClass([self class]), - [request.URL description], + [noCacheRequest.URL description], [[UIDevice currentDevice] systemVersion] ]; return [self loadHTMLString:errorHtml baseURL:nil];