Reminder on using Ionic for the Web

This post is more than 2 years old.

Earlier today the Ionic folks published an article about Ionic 2 and PWA (Progressive Web Apps) - "Announcing PWA support in Ionic 2". The gist is that - you guessed it - Ionic 2 apps will support PWAs out the box. You don't have to actually make use of PWA of course, but if you want to build an Ionic 2 app for the web, and not hybrid mobile, then Ionic has done some of the grunt work to get you started down that path. I thought it might be useful to remind folks how you can build an Ionic web apps instead of hybrid mobile apps.

First and foremost, if you've been playing with Ionic V2 apps, most likely you've been using the beta version of the Ionic CLI. A few weeks ago the CLI was updated to 2.0 and you should no longer be using the beta version. To be clear, while the Ionic CLI is at version 2, the Ionic framework itself still defaults to version 1 (and Angular 1). To work with V2 apps, you need to pass the --v2 flag when creating your application.

To work with a web app, not Cordova, you should add another flag: -w. This tells the CLI to not add platforms and other Cordova-related stuff.

So to summarize - you can create your new app like so:

ionic start noHybridNoProblem --v2 -w

Once done, you've good to go. But then you may wonder - how in the heck do you actually view your app? Normally I use ionic emulate ios to see my builds in the simulator. Since we aren't using Cordova, that's not an option.

Instead - you'll want to use the Gulp script included in the project. Don't worry if you aren't familiar with Gulp, it's basically a simple automation library for projects. To "build" your app into something you can browse, simply do:

gulp build

You should see output like so:

And you can then see the results in your www folder. When I tested, I used httpster to fire up a little web server. Everything worked as expected, but you want to remove the line including cordova.js in your index.html file since it won't exist.

Finally, if you want the code to build automatically while you edit, use:

gulp watch

That's it. Enjoy.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Antonio Gallo posted on 9/14/2016 at 3:19 PM

"ionic serve" does not work anymore?

Comment 2 by Antonio Gallo posted on 9/14/2016 at 3:19 PM

anyway i tested PWA and looks great for "in company" apps... no more headache to go thru' the store anymore

Comment 3 (In reply to #1) by Raymond Camden posted on 9/14/2016 at 3:22 PM

Sure - and you can use that - my thinking was that I'd probably want to test as 'close to the bone' as possible and that means a vanilla web server. httpster makes that trivial.

Comment 4 by Cesar posted on 9/14/2016 at 3:35 PM

Awesome. Thanks for the info. I was wondering how this worked.

Is it possible to have both a hybrid mobile app and pwa? Keep the -w flag or is -w used if only building a pwa?

Comment 5 (In reply to #4) by Raymond Camden posted on 9/14/2016 at 3:38 PM

You could - but my gut says that's a bad idea. So for example, you could have code that says, "If I'm a hybrid app, I can read Contacts, so enable this button". But it feels like you'll have a mishmash of features imo. I don't know. I don't have a great answer for this outside of saying I'd just pick one and focus on one, even though you COULD do both at once.

Comment 6 by Don (Gear Offer founder) posted on 9/14/2016 at 4:00 PM

I think the real dream is to be able to write an Angular 2 web app and share code between the web app and Ionic2. So, somehow in the build process, output the component (classes) into directories for the web and Ionic and create views for each.

Comment 7 by Mibinn posted on 9/14/2016 at 4:41 PM

Great Raymond!
We already use Ionic2 for developing web app integrated in a Liferay Portal as portlets, sharing authentication stuffs and consuming json web service exposed by the portal.
We used "ionic serve" to test the app outside the portal, but "gulp build/watch" give me new perspectives like autodeploying to the portal web server.

Comment 8 (In reply to #4) by Antonio Gallo posted on 9/14/2016 at 6:05 PM

Yes, ionic2 blog has example about it, its just a few line of codes... however you cannot use "plugins" :-) lots of "if" in your code then :-P

Comment 9 (In reply to #6) by Antonio Gallo posted on 9/14/2016 at 6:06 PM

so far ionic2 seems to work fine on a webserver like a normal angular2 application; very different from ionic1

Comment 10 (In reply to #9) by Don (Gear Offer founder) posted on 9/14/2016 at 6:08 PM

Yeah, it works well as a web app, but what I'd like to do is create a separate set of views for a web app. So you'd have your Ionic views for the mobile app and more website appropriate views for the web app, but use the same code. I'm going to try it on a future project.

Comment 11 by Šime Vidas posted on 9/14/2016 at 10:46 PM

Could you explain what kind of web app (if any) you get when you go down the “hybrid mobile app” path? Is it the same one that is described in your article? I’m asking because I’m anticipating that a pure PWA + iOS app combo could be in demand in the near future.

Comment 12 by Adam posted on 9/15/2016 at 7:15 AM

Hi not sure if this is something simple, I want to offer a mobile web version of my app also, i.e actually view in a browser, (not PWA in the sense of add to home screen etc) however it seems that some of the components still use the device resolution. I.e device height, and does not factor in the normal browser status bars etc in height calculations, as such things dont display properly. any ideas?
In particular <ion-slide>

Comment 13 by chandramuralis posted on 9/15/2016 at 1:11 PM

I think you were referring to 'httpster in the last paragraph instead of 'httster'

Comment 14 (In reply to #13) by Raymond Camden posted on 9/15/2016 at 1:13 PM

Thanks - will be fixed in next update.

Comment 15 (In reply to #12) by Raymond Camden posted on 9/15/2016 at 1:14 PM

I'm not entirely sure I get you. A PWA is still a web app and runs in the browser. That being said - if you are seeing issues with layout in UI items like ion-slide, your best bet is to create a reproducible example and file a bug report.

Comment 16 (In reply to #11) by Raymond Camden posted on 9/15/2016 at 1:15 PM

I'm not quite sure I get your question. Are you asking what a hybrid mobile app is? I don't think so - but I don't get exactly what you are asking either.

Comment 17 (In reply to #7) by Rodrigo Mantica posted on 5/22/2017 at 7:56 PM

Hey mibinn could you explain how you packaged an ionic 2 app inside of a liferay portlet? I'm trying to do that on liferay 6.2 with an existing ionic 2 app and existing liferay portal. Any help would be greatly appreciated.

Comment 18 (In reply to #17) by Mibinn posted on 5/23/2017 at 9:53 PM

Hi Rodrigo,

It's a bit late for me tonight but I will try to explain how we do that :

We do it this way :
- build the Ionic app for "browser"
- creatan empty liferay plugin (portlet)
- copy all build stuff to the liferay plugin, in html folder
- Create a simple JSP for the portlet with an iframe inside it, adressing the Ionic index.html (you should set a fixed height to your iFrame)
- Build the portlet, deploy it and drag on a page

Bonus :
- to consume liferay (web) services from the ionic app, we need to retrieve the pAuth from Liferay and use it for each http request (from ionic code). So we have added a "serveresource" method insdide the portlet class to deliver this pAuth in response of an http request coming from the Ionic app.

Other way :
- we also tried to include directly the root code of the ionic app ( the launcher that is in the index.html page) instead of an iframe. It works almost but we had trouble with relative positionning (x/y) of element. That's the reason why we choose to use iFrame. And iFrame is de facto asynchrous.

Architectural :
"Server" component : Liferay
Liferay service (and our own service made by service builder) are consumed by the Ionic app via the JSON Remote services.

"Client" component : Ionic App
Inside Ionic App we define a service layer, that make http request towards the json api of Liferay. Nothing special here, just the natural Ionic/angular2 mechanism (providers, subscribe, etc...)

And it works ! efficiently ! easier than alloyui or any javascript library.
And moreover : same code can be used to build a mobile app (androïd/ios) or even a desktop one (via Electron).

Regards !

Christophe (aka Mibinn)

Comment 19 (In reply to #18) by Rodrigo Mantica posted on 5/24/2017 at 2:11 AM

You sir, are a life saver. I was about to rebuild the ionic app as an angular web app but your solution worked! Thank you so much :DD

EDIT: There is no CORS issue. I was just calling the wrong requests!

Thanks again and I appreciate your help

Comment 20 (In reply to #19) by Mibinn posted on 6/2/2017 at 8:55 AM

Was offline last days, but glad you had success. I have some tips about cors and auth. I'll post all about that soon ... Stay tuned!

Comment 21 (In reply to #20) by Rodrigo Mantica posted on 6/2/2017 at 6:28 PM

That would be amazing :D putting the app within liferay as a portlet solved my cors issue. I'm also able to make calls as long as I have email and pass basic auth header (I don't need to include the pAuth param for some reason). What I haven't figured out is how to perform Facebook login using jsonws since I don't have a way of looking up users by their facebookId and I don't know how to create a basic auth header using a facebook response.

Any tips would be greatly appreciated!

Comment 22 (In reply to #20) by Rodrigo Mantica posted on 6/6/2017 at 4:57 AM

Have you gotten deeplinks/url routing to work with ionic 3? Looks like in portal environment, liferay impedes the app from controlling the url.