Skip to content
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

Modified image not updated in preview #746

Closed
niklasberglund opened this issue Feb 6, 2017 · 8 comments · Fixed by #747
Closed

Modified image not updated in preview #746

niklasberglund opened this issue Feb 6, 2017 · 8 comments · Fixed by #747
Labels

Comments

@niklasberglund
Copy link
Collaborator

  1. Insert an image
  2. Modify image in external program and save
  3. Go back to MacDown. The image haven't changed.

Would be nice if the image could be automatically updated when coming back to MacDown. But the issue I observed is that even by:

  1. Remove image's markdown
  2. Insert image markdown again

Expecting step 5 to trigger a reload of the image but the image is using a cached version so it's still not updated. Sometimes it will be updated after removing and inserting again, sometimes not. ⌘R reloading the preview also doesn't help.

I'm thinking it's because of caching in the WebView

@niklasberglund
Copy link
Collaborator Author

I looked into this a bit. Haven't gotten it to work. What I've tried is:

  • Disable caching globally by setting the application's sharedURLCache to an empty one (NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];)
  • Implemented WebResourceLoadDelegate callback method to explicitly disable cache like so:
- (NSURLRequest *)webView:(WebView *)sender
                resource:(id)identifier
                willSendRequest:(NSURLRequest *)request
                redirectResponse:(NSURLResponse *)redirectResponse
                fromDataSource:(WebDataSource *)dataSource
{
    // Explicitly setting cache policy
    request = [NSURLRequest requestWithURL:[request URL]
                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                            timeoutInterval:[request timeoutInterval]];
    return request;
}

Haven't worked much with WebKit's WebView but cache handling with the old UIWebView is cumbersome and very limited in what you can do.

@uranusjr
Copy link
Member

uranusjr commented Feb 6, 2017

This is a pretty long-standing issue, and I think there is actually a duplicate somewhere. I also tried the second method a while ago (and it didn’t work, obviously). I’m thinking maybe the best way around this is to implement some kind of cache busting logic in the Markdown renderer.

@niklasberglund
Copy link
Collaborator Author

Good idea. But I think it won't work for local files? Gave it a shot anyway. I tried adding a per request unique GET parameter value for images in the - (NSURLRequest *)webView:resource:willSendRequest:redirectResponse:fromDataSource: callback without any luck.

I then tried something really nasty just to see if it'd work. I prepended an incremental number of /.. so a path would be something like /../../../../../../Users/niklas/image.jpg. Also not working. Maybe internally it all translates to the same path. Or maybe I'm poking around in the wrong place.

@uranusjr
Copy link
Member

uranusjr commented Feb 6, 2017

Paths are probably manipulated internally. This has been a problem with PDF exports—Safari would export files with absolute URLs, breaking links. I would have thought GET parameters would work… but maybe it just treats the file:// protocol differently. :(

@uranusjr
Copy link
Member

uranusjr commented Feb 6, 2017

One thing I’m curious about: Would the “new” WebKit API (classes with WK prefix) solve this issue? It is only supported in 10.10+, but it has a WKWebsiteDataStore class with - removeDataOfTypes: modifiedSince: completionHandler: that looks like a solution. I didn’t find a similar method with the old API.

@niklasberglund
Copy link
Collaborator Author

niklasberglund commented Feb 6, 2017

Ah perhaps. That'd be a nice way to solve the issue.
Found a private API in WebKit which solves the issue - [WebCache setDisabled:YES].
Since MacDown isn't in the app store is it somewhat okay to use private API?

Would it be a good idea to use it for now and then look into moving over to WKWebView?
Using WKWebView should be good either way. I could do the migration and send a PR if you want.

@niklasberglund
Copy link
Collaborator Author

WKWebView for 10.10+ and WebView for earlier for backwards compatibility?

@FranklinYu FranklinYu added the bug label Feb 6, 2017
niklasberglund added a commit to niklasberglund/macdown that referenced this issue Feb 6, 2017
uranusjr added a commit that referenced this issue Feb 6, 2017
Disable cache in preview

This mainly solves #746 by calling WebCache's setDisabled: private API.
See #746 and #747 for discussions about a more long-term solution to this.
@FranklinYu
Copy link
Member

@niklasberglund FYI I’m planning to migrate to WKWebView in #1181.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants