Showing posts with label web-applications. Show all posts
Showing posts with label web-applications. Show all posts

Saturday, March 3, 2018

Preload multiple local WebViews

Leave a Comment

Because I could just find some outdated information / not working solutions to my problem I decided to ask this kind of question again. (See outdated / wrong solutions:)


My app is separated in one native part and one HTML part. The HTML is saved as a local file (index.html) and should be load into the myWebView view.

@IBOutlet weak var myWebView: UIWebView! func loadWebview() {     let url = Bundle.main.url(forResource: "index", withExtension: "html")     let request = URLRequest(url: url!)     myWebView.loadRequest(request)     myWebView.scrollView.isScrollEnabled = false     myWebView.allowsLinkPreview = false     myWebView.delegate = self } 

Because my DOM tree is very large, switching from the native part to the web part (on button click) takes quite a long time at -least for the first time switching- because afterward, I'm sure the webView-request gets cached.

To my question: How can I preload the WebView on app init to avoid the white screen (maybe 0.5s - 1s duration) when switching from the native to the Web part?

EDIT:

The WKWebView is displaying the scrollbar while the UIWebView was not!

Using (like with UIWebView) this styles:

::-webkit-scrollbar {      display: none; } 

is not working and adding these lines:

webview.scrollView.showsHorizontalScrollIndicator = false webview.scrollView.showsVerticalScrollIndicator = false 

is also not working at all.

1 Answers

Answers 1

Firstly, you should switch to WKWebView, UIVewView is no longer recommended to be used by Apple.

Secondly, you can create a pool of web views that get created and asked to load when the app starts. This way by the time the user switches to the web interface the web view might've got a chance to fully load.

For this you can use a class like this:

/// Keeps a cache of webviews and starts loading them the first time they are queried class WebViewPreloader {     var webviews = [URL: WKWebView]()       /// Registers a web view for preloading. If an webview for that URL already     /// exists, the web view reloads the request     ///     /// - Parameter url: the URL to preload     func preload(url: URL) {         webview(for: url).load(URLRequest(url: url))     }      /// Creates or returns an already cached webview for the given URL.     /// If the webview doesn't exist, it gets created and asked to load the URL     ///     /// - Parameter url: the URL to prefecth     /// - Returns: a new or existing web view     func webview(for url: URL) -> WKWebView {         if let cachedWebView = webviews[url] { return cachedWebView }          let webview = WKWebView(frame: .zero)         webview.load(URLRequest(url: url))         webviews[url] = webview         return webview     } } 

and ask it to preload the url sometimes during the app startup:

// extension added for convenience, as we'll use the index url in at least // two places extension Bundle {     var indexURL: URL { return self.url(forResource: "index", withExtension: "html")! } }  webviewPreloader.preload(url: Bundle.main.indexURL) 

Thirdly, you might need to use a container view instead of the actual web view, in your controller:

@IBOutlet weak var webviewContainer: UIView! 

What remains is to add the preloaded web view to the container when needed:

func loadWebview() {     // retrieve the preloaded web view, add it to the container     let webview = webviewPreloader.webview(for: Bundle.main.indexURL)     webview.frame = webviewContainer.bounds     webview.translatesAutoresizingMaskIntoConstraints = true     webview.autoresizingMask = [.flexibleWidth, .flexibleHeight]     webviewContainer.addSubview(webview) } 

And not lastly, be aware that keeping alive instances of web views, might carry performance penalties - memory and CPU-wise.

Read More

Tuesday, February 13, 2018

How to debug Azure swapping process (sometimes bringing site down)

Leave a Comment

We have a pretty large project that is running on Azure. For some reason swap times became really slow recently, like at least 10 minutes.

Somtimes during the swap the site becomes superslow, like that it doesn't respond for minutes. Other times the swap just doesn't work for one reason or another.

We are using initializationPage to warmup the most specific pages, but it doesn't seem to help.

Question Is it possible to see what's going on during the swap? I'm trying to debug why it's so slow. Is there any log that I can see why it's stuck on what?

We can't deploy emergency fixes without bringing the whole site down. and sometimes the whole site goes down.

Any help to debug swapping problems would greatly appreciated.

Update

I found the following in 'Activity log' on the Azure Portal, but I still can't find any details or any hint what is going on exactly.

enter image description here

So: The resource operation completed with terminal provisioning state 'Failed'.

Where can I find details? It really annoys me that I have to buy Azure Developer support while I'm spending hundreds euros per month already on something that seems broken or at least very uninformative about what is going wrong.

1 Answers

Answers 1

So: The resource operation completed with terminal provisioning state 'Failed'.

Where can I find details?

Microsoft has a few things that may help you.

You can view the operations for a deployment through the Azure portal. You may be most interested in viewing the operations when you have received an error during deployment so this article focuses on viewing operations that have failed. The portal provides an interface that enables you to easily find the errors and determine potential fixes.

The "View deployment operations with Azure Resource Manager" is directly from Microsoft it has several steps to follow. Follow the URL: Microsoft

I hope this helps.

Read More

Tuesday, February 6, 2018

Should views be rendered by the app, or should a static site contact the API using AJAX?

Leave a Comment

I am starting to write my first web app with Node.js and Express. I have two approaches in mind.

  • Create two sets of routes. One that sends JSON, one that renders the page using a templating engine
  • Create a static website that makes API calls to the backend using AJAX, and have only routes for the API.

I understand that approach #2 depends on AJAX support in the browser, but if this was your project, based on the advantages and disadvantages of each approach, which would you choose and why?

3 Answers

Answers 1

If I am reading it right, both options #1 and #2 first set of routes is an API that returns JSON rather than sends it.

Assuming that in #2 you wouldn't create static pages with JavaScript doing AJAX calls, but rather still use express static routing like app.use('/', express.static(path.join(__dirname, 'dist'))); the difference between 2 approaches isn't that big.

Unless you already know some supported template language, such as mustache the cons are that you have to learn one and before that pick one (which isn't always an easy task from my experience!).

If you don't know one, depending on your application, you might still benefit from learning and using one. As an example you can think of a very generic UI where a single template could be reused very many times - like a generic database UI similar to, say well known phpmyadmin.

In case of static routing, you can achieve similar results by using a JavaScript framework which has components or templates, such as angular. If you aren't planning to use one, that could result in a lot of code duplication of otherwise re-usable widgets. And even when using one I can imagine a case when template engine would result in less code (or less files in your project at the very least). Not sure though if it's going to be easier to navigate and moreover to cover with tests when the project grows.

Without knowing more about your project it is hard to give more advice.

Answers 2

If the product you're developing is primarily static content with some dynamic pieces here and there, I'd recommend serving HTML from your backend via a templating system. A good example of this would be a blog. You'll end up with better SEO and there are less moving pieces to grok in this approach.

However, if you want to develop a single page application, I recommend using your backend entirely as an api, and writing your client side logic entirely in React/Vue/Angular/whatever. Single page applications have their front ends written entirely in javascript, and are best suited to highly dynamic, "app like" experiences online. Think gmail or facebook.

Answers 3

In my current project we use both approaches. Many views are static and data is obtained from API calls (we just use angular) or bootstrapped values (we load some objects with template, like user roles etc.)

However, some views are rendered on server site, because it easily allow us to dynamically inject values, tokens or other supporting data without making extra requests.

So, I vote for flexibility

Read More

Saturday, September 2, 2017

Cache busting in a Google Chrome Web Application

Leave a Comment

We are currently using Webpack with the HtmlWebpackPlugin to generate our javascript builds for our webpage.

new HtmlPlugin({     template: 'www/index-template.html',                //source path - relative to project root     filename: 'index.html',                             //output path - relative to outpath above     hash: true,     cache: true                                         //only emit new bundle if changed }), 

This causes a hash to be added to the query string of the bundled javascript file.

<script type="text/javascript" src="/build/vendor.min.js?4aacccd01b71c61e598c"></script><script type="text/javascript" src="/build/client.min.js?4aacccd01b71c61e598c"></script> 

When using any standard desktop or mobile browser, new builds are cache busted properly and the new version of the site is loaded without any effort from the user. However, we also have a chrome web app implementation where we call:

chrome.exe --app=http://localhost:65000 --disable-extensions

In this application, for some reason the hash on the end of the javascript build doesn't bust the cache. We have to manually right click somewhere on the page, then click reload (or press F5). For some reason the cache isn't busted in the web application.

I was thinking that possibly it is caching the index.html file maybe? That may cause the app to never receive the updated hash on the build. I'm not sure how to solve that issue though if that is the case.

I have also noticed that if our localhost server is down, the page still loads as if the server were running. This indicates to me some kind of offline cache. I checked the manifest.json parameters and can't find anything to force a reload.

I have also tried these chrome command line switches which did not help either: --disk-cache-size=0, --aggressive-cache-discard, --disable-offline-auto-reload.

Another caveat is that we need to retain the localStorage data and their cookies. In a standard browser window, or any browser for that matter it works just fine, but not when it is inside a Chrome web app.

2 Answers

Answers 1

Are you talking "Progressive Web App" with service workers? If so then the html file can (and should) be cached on first download. You need to have some sort of aggressive update process on the client to ensure new files are loaded properly.

Perhaps having an api call that checks some sort of dirty flag on the server could work, and if it comes back true, it should reload the template files. Or something more complex where it gets an array of dirty files from the server so it knows which ones to reload instead of loading everything. Just some ideas.

Answers 2

As your page works without the server running at localhost, I suspect that your app is offline first. This is done exactly through service workers(as pointed out by @Chad H) which are officially supported by Chrome and are experimental in other browsers. So, expect different behavior in other browsers. To bust the cache,

In Production

For a permanent solution, you to find and modify the service worker (SW) code. Deletion of old caches happens only in activate event of SW.

You can also read more about Service worker and ask a question with the updated SW code. Also, check out this resolved issue that faced a problem similar to yours.

For dev setup

You can use the Disable Cache option under Network tab in Chrome DevTools (works only when DevTools is open) or use a more robust chrome extension called Cache Killer.

Read More

Sunday, May 8, 2016

Do I have to build maven webapp project everytime i make changes into static files?

Leave a Comment

I am using JBOSS AS7.1, Eclipse Luna for development. My eclipse installation does have a plugin installed for maven.

I have created my webapp project using maven command-line.

In my current set up, I have to build my maven project using mvn clean install every time for all changes, even for the static files like HTML, CSS.

Then, I have to deploy the generated WAR file using JBOSS console running at http://localhost:9990/console.

I am quite sure that there must be another way to do this. Surely, it does take a hell lot of time.

Please guide me to the approaches I can adopt for faster development.

2 Answers

Answers 1

One option is jrebel. It's not free though.

If you are not bound to JBOSS, you could use spring boot. It also supports automatic restart (spring boot devtools)

Answers 2

You can replace the static file in your target folder and launch a build skipping the compile phase.

It will save you a lot of time, when only updating static files.

It is not a good practice, but should let you achieve your goal.

How to:

  • Use the maven-clean-plugin to remove the files to replace from the target folder (or they will not be overwritten);
  • Use the resources tag if your static files are not the only content of the resources folder you want to copy (or your static files are not in resources folder at all);
  • Use the maven-compiler-plugin to skip the compile phase.

Customize this profile (and use it with mvn clean install -P skip-compile):

<profile>     <id>skip-compile</id>     <build>         <resources> <!-- optional -->             <resource>                 <directory>src/main/resources/META-INF</directory>                 <targetPath>META-INF</targetPath>                 <excludes>                     <exclude>**/*.xml</exclude>                 </excludes>                 <includes>                     <include>**/*.html</include>                     <include>**/*.css</include>                 </includes>             </resource>         </resources>         <plugins>             <plugin>                 <artifactId>maven-clean-plugin</artifactId>                 <version>3.0.0</version>                 <configuration>                 <excludeDefaultDirectories>true</excludeDefaultDirectories>                     <filesets>                         <fileset>                             <directory>${project.build.outputDirectory}/META-INF</directory>                             <excludes>                                 <exclude>**/not_to_delete.xml</exclude>                             </excludes>                             <includes>                                 <include>**/*.html</include>                                 <include>**/*.css</include>                             </includes>                             <followSymlinks>false</followSymlinks>                         </fileset>                     </filesets>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <executions>                     <execution>                         <id>default-compile</id>                         <phase>compile</phase>                         <goals>                             <goal>compile</goal>                         </goals>                         <configuration>                             <skipMain>true</skipMain>                         </configuration>                     </execution>                 </executions>             </plugin>         </plugins>     </build> </profile> 
Read More

Monday, April 11, 2016

Can't disable pinch zoom (and double tap) zooming on a web app in iOS Safai

Leave a Comment

I am trying to disable pinch-zoom on a web app, but the following meta-tag is having zero effect in iOS Safari and iOS Chrome:

<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0"/> 

I tried adding the following to the meta tag, but I'm having no success there either. Pinch (and double-tap) zooming is still enabled.

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 

Any insight into why those two meta tags might not be working?

1 Answers

Answers 1

For me following meta tags are working in both the scenarios

This works proper for cordova/Ionic project it disable the double tap/pinch on normal page

<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 

I open a simple page with using jquery/bootstrap in iOS safari, this also disable the double tap/pinch zoom

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> 

You may check this codes if you have not tried that : http://stackoverflow.com/a/11689448/4557505

Also please provide some details about your web app is it default web app like html5/responsive or you are using any cross platform like cordova/Ionic, it will help in checking the specific scenario

Read More