r/javascript • • Feb 22 '18

Created a local weather Progressive Web App that works offline and on any device with modern browser. Built with Vue.js.

https://jimmerioles.github.io/progressive-weather-app/
490 Upvotes

86 comments sorted by

32

u/jimmerioles Feb 22 '18 edited Feb 22 '18

Also here's the Source code Please let me know your thoughts on it. Thanks!

13

u/4spooky6you Feb 22 '18

Looks cool. Is there a reason your mixing paradigms (async/await + vanilla promises) in the weather service?

9

u/jimmerioles Feb 22 '18 edited Feb 22 '18

Thanks! I just don't prefer method chaining so I'm using the ECMAScript2017's async/await feature. For me it is way more cleaner than method chaining.

 

Edit: Clarifications

 

One of my goals is to write clean and easy to understand code as much as possible to avoid suffering maintainability issues. But I also still tend to weigh the options before adapting.

 

As for the reason in mixing async/await + vanilla promises in the weather forecast service, I was left with an option to abstract it for consistency or to use fetch directly there to void messing up the simple hierarchy of the methods. So sometimes it can't be helped to "mix paradigms" to trade consistency over simplicity, though there's so many ways to skin a cat :)

 

Definitely the WeatherForecast service class has lots of room to refactor. I could abstract specific responsibilities in their own classes. I am very open for refactor ideas that still maintain simplicity and clean code.

 

Thanks for the feedback! Gained an insight for next refactor!

4

u/JustinsWorking Feb 22 '18

For example the getForecast method. You fetch().then() rather than await fetch()

They were asking why you chose to use the promise (then) and not await there

-2

u/jimmerioles Feb 22 '18

I think it would error out if I await fetch() because of the asynchronous nature of fetch() thus passing invalid data to this.populate(data).

5

u/JustinsWorking Feb 22 '18

No because your using the fetch promise to return to promise from .json() which is an async call

If you wanted to write it with async await, you’d

Data = await fetch();
Return data.json();

Then your await above would await on the json promise. Just like your current code

You’d have to wrap it in a try/catch to get the error.

You could also await the data.json() wrap that in a try catch and return the final json value as well that would add error handling Incase the fetch result wasn’t json.

1

u/GBcrazy Feb 23 '18

Well what you are saying doesn't make any sense

await does exactly the same thing as .then behind the scenes

This isn't criticism by the way, just something that you should think for consistency

15

u/e_man604 Feb 22 '18

Doesn't seem to answer his questions? Why you're mixing and using both.

0

u/[deleted] Feb 22 '18

[deleted]

8

u/asdf7890 Feb 22 '18

No, apart from consistency generally makes things clearer and therefore easier to maintian.

0

u/[deleted] Feb 22 '18

[deleted]

5

u/ProbablyNotJaRule Feb 22 '18

You could use bluebird to promisify, I know node has its own promisify function now but I’m not sure about vanilla js. I don’t think it’s a feature of vanilla js but I could be wrong. You can promisify any “thenable” function

2

u/TheHamsterOfGod Feb 22 '18

One thing you don't want to have is another bloat by adding more and more libraries to your code specially if it's a PWA

1

u/ProbablyNotJaRule Feb 22 '18

Bloat is such an issue with npm/node/js so I definitely feel you there. I think if you only have one or two methods that require a callback, just do the callback. Promisifying really comes in handy when you have to use a lot of callbacks

0

u/[deleted] Feb 22 '18

[deleted]

2

u/ProbablyNotJaRule Feb 22 '18

But re-doing all of your code is the name of the game... just kidding. Just letting you know for future development if youre interested. Nothing wrong with callbacks or normal promises just easier to look at without all the callbacks.

1

u/rybl Feb 22 '18

You could wrap it in a function that returns a promise. Something like this:

function funcWithCallbackWrapped() {
    return new Promise((resolve, reject) => {
        funcWithCallback().then((data) => resolve(data), (err) => reject(err));
    });
}

1

u/morb6699 Feb 22 '18

There is a defined difference between async/await and promises that goes beyond method chaining.

1

u/PissBlaster2k Feb 22 '18

Hm, I thought async/await depended on returned Promises?

1

u/[deleted] Feb 22 '18

Thanks for sharing the code. Looks great and I like the details like day night mode!

1

u/jimmerioles Feb 22 '18

Thanks man!

52

u/backlashsid Feb 22 '18

Manual zip code input will be a nice to have for those who have location blocked.

21

u/jimmerioles Feb 22 '18

That would be a nice next feature to have, thanks for the feedback!

9

u/magenta_placenta Feb 22 '18

Came here to make this comment as well.

I made a similar angular app years ago like this. Where are you sourcing your weather svgs? I see them in your assets folder. I used http://www.alessioatzeni.com/meteocons/ which look very similar (they also have night versions as well).

I dig the artwork and monochromatic feel you have going, though your day time theme could have a little more contrast. BTW, your camp fire should be lit up in the night theme :)

1

u/ekim43 Feb 23 '18

THIS is extremely valuable feedback. We don't know how much users would actually click "Allow" for your website to know your location. A huge population is already used to the prompt on their smart phones and just naturally dismissing/denying it.

14

u/the_ju66ernaut Feb 22 '18

Very nice work. I am a vue fan and I am all for pwa. One thing to note: I viewed the page on my phone (on wifi) and had gps off and it asked me to go online instead of turning on my GPS.

6

u/jimmerioles Feb 22 '18

Thanks man! Nice catch!

2

u/monstera-rgb Feb 22 '18

Samesies. Looks great though.

1

u/yuipcheng Feb 22 '18

I have both mobile data and location on but rejected to send location when prompted then the app asks to internet on.

6

u/uniqueusername37 Feb 22 '18

Looks great! Nice work!

2

u/jimmerioles Feb 22 '18

Thanks man!

5

u/Bruyninx Feb 22 '18

Looks really clean! I'm a novice webdesigner (first year), is it recommended to use things like Vue.js? Or should I stick to standard JS?

3

u/jimmerioles Feb 22 '18

You should probably learn Vanilla/Standard JS first then frameworks later.

is it recommended to use things like Vue.js?

IMO, Vue.js has a bright future.

9

u/inhalingsounds Feb 22 '18

Learn the ropes around vanilla JS. Those will be the founding blocks no matter which framework you choose later on.

Vue is amazing, I work with it exclusively. Even if you decide on other framework Vue is great as your first approach to modern development.

3

u/Bruyninx Feb 22 '18

I'm following a more general course (C#, HTML5, JS , SQL), but I'm really feeling webdesign the most.

When you say the basics of vanilla JS, does this include small JQuery stuff? (I'm unexperienced in that aswell).

Searching arround the web makes me think JQuery is a really standard way to learn design but I'm not sure..

6

u/Thef19 Feb 22 '18

I would say no. jQuery is mostly just unnecessary bloat now-a-days. While it is good to have some experience with jQuery for being able to maintain legacy apps, there is almost no reason to use it anymore other than it being a dependency for a lot of things.

Learning vanilla JS really well is your best option IMO. If you know vanilla really well, it doesn't matter what library / framework you want to / are required to work with in the future, you will be able to pick them up and learn them with ease as you already know how they work behind the scenes anyways.

3

u/inhalingsounds Feb 22 '18

Not at all. Don't go that way. You see plenty of tutorials that go this (really bad) path:

  • Download x and y jQuery plugins

  • Mess around with their array parameters

  • That's it, you're a JS pro because you have a modal window fading in and out of your screen!

jQuery isn't Javascript. It's a Javascript library. This means it helps you out with some JS stuff but it will not replace knowing how to write JS. You might get away with it for simple stuff, but that lack of knowledge will definitely emerge when you least expect.

Some good resources for begginers / intermediate devs:

Once you start getting serious, give http://eloquentjavascript.net a shot. It's pretty advanced but goes through a lot of important aspects of the language.

One more thing. jQuery is a mess and I wouldn't recommend it to anyone developing professionally in 2018. I doesn't make any sense to use jQuery nowadays when you have React, Angular and Vue. The only possible reason to do so is if you're stuck working with legacy stuff that depends on very specific packages you can't find alternatives for.

1

u/Bruyninx Feb 22 '18

I've only used the smoothscroll script that I downloaded from the internet hehe :D

But thanks for the detailed comment, definitly saving this one!

3

u/chillstrumentals Feb 22 '18

Searching arround the web makes me think JQuery is a really standard way to learn design but I'm not sure..

jQuery is awesome but I recommend you at least try a quick passion project or two using Vanilla JS and taking it as far as you can. A good example is traversing the DOM in JS vs jQuery - I built a replica of a video game menu and the extra day or two of time I put in learning how to grab elements and iterate over them, working with events ( I learned so much about Event bubbling vs capturing ) and building out functions was 100% worth it. I built custom functions and made code in JS I was proud of (that also could have been done easily in 5 minutes with jQuery). I also understood more things about whats happening when I am using jQuery to perform an action.

Ultimately - you can write jQuery (chaining methods etc) in Vue and jQuery is going to accomplish what you need but you sound like me and I jumped ahead so taking a small step back actually allowed me to continue growing.

1

u/Bruyninx Feb 22 '18

My teacher says the same haha, says I'm to aeger to learn but I'm often aiming too far..

1

u/aaarrrggh Feb 22 '18

As someone who uses React but has never tried vue, could you give an elevator pitch on why I might consider it above React?

3

u/inhalingsounds Feb 22 '18

I think Matt Campbell's video is excellent as he shows off Vue as if you were a React guy.

For me personally I really like how simple and beautiful writing component-based stuff is with Vue. In fact, everything is just ... simple. Sure you'll bump into some weird problems now and then (it's JS after all) but the whole framework is very clean and feels nice to work with.

5

u/budcorthair Feb 22 '18

Looks great! Do you have any resources/guides that you recommend for someone to build their first PWA?

5

u/jimmerioles Feb 22 '18

Thanks man! Google Dev is my go to PWA resource.

2

u/sullitf Feb 22 '18

Very cool! I'm a big fan of simple weather app designs, no clue why so many of them throw so much info on screen when 90% of the time I just want to know the basics.

I've been working with Svelte quite a bit recently. If you don't mind I may take some time this weekend to remake this in Svelte and see how it compares to Vue

2

u/jimmerioles Feb 22 '18

Thanks man! Sure! I too prefer simple and clean design even in code.

2

u/JezuzCrist Feb 22 '18

awesome, clean, amazing work. I also am playing around with Vue, it's so easy compared to angular or react

this is what I created so far http://shapefor.me

1

u/jimmerioles Feb 22 '18

That's slick man!

0

u/[deleted] Feb 23 '18

[deleted]

1

u/JezuzCrist Feb 23 '18

thanks you kind sir, what do you mean by it's "collar"?

3

u/TackleByNumber69 Feb 22 '18

Pretty cool! Small suggestion would be to rip out your constants (API URI, app ID, the snow/rain IDs etc.) so there's a central place to change them, should they need to be changed.

Also (this is pedantic), do you need to include the dist/ folder in the source? I've never used Vue, but if it's just gonna be generated as a bundle like webpack does, then no need to include it

4

u/jimmerioles Feb 22 '18

Thanks for the suggestion, I was also puzzled on how to go about this. I was trying to use .env to store and hide my APP_ID but found out process.env is only visible in node and not in browser.

do you need to include the dist/ folder in the source?

I needed the dist/ to be hosted in gh-pages through git subtree.

4

u/Asrijaal Feb 22 '18

Look into the webpack define plugin. It injects environment variables into your App context on build time.

https://github.com/gdi2290/angular-starter/issues/386 as an example

1

u/jimmerioles Feb 22 '18

Thanks man! definitely gonna lvl up my webpack-fu this time :)

4

u/AUTplayed Feb 22 '18

it just displays "please connect to internet" for me... did we crash the weather server?

1

u/AskYous Feb 22 '18

Does free code camp teach progressive web apps?

1

u/jimmerioles Feb 24 '18

Currently no but hopefully in the future

1

u/AlDrag Feb 22 '18

This is absolutely beautiful. Love the design. You're a designer yourself?

1

u/jimmerioles Feb 23 '18

Thanks man! I am mainly a back-end developer, just started dabbling on front-end last year.

1

u/[deleted] Mar 20 '18

Please connect to internet to fetch latest forecast :)

I think I broke it. Ctrl+F5 doesn't help :(

Went into Chrome's Devtool's Network tab and went offline. Got that same message. Went back online and still got that message.

1

u/jimmerioles Mar 20 '18

I think you need to enable your browser's location services. What browser are you using?

1

u/[deleted] Mar 20 '18

Chrome 65.0.3325.162.

At first it asked me for the location service (I accepted) and it worked fine, but after going offline via the Devtools it broke.

Edit: wait what? I just re-opened the page and got 8 degrees (my first result), but after refreshing the page I got the same error again :/

1

u/jimmerioles Mar 20 '18

Weird I just tested it with same browser as yours, and it works fine on my end. Maybe you have rouge browser extensions?

1

u/[deleted] Mar 20 '18

Could be! But disabling uBlock Origin on that page does nothing and my other extensions don't block anything, AFAIK.

1

u/jimmerioles Mar 20 '18

I am also using uBlock Origin and it doesn't break the site so it is more likely that one of your other extensions is the culprit. And I don't have any errors on the console, just a note about geolocation request which is a UX improvement tip from Google.

1

u/[deleted] Mar 20 '18

Even after clearing storage under the Application tab I get nothing. Firefox 59 still works fine.

I'm getting a Failed to load resource: net::ERR_BLOCKED_BY_CLIENT in Chrome though.

1

u/tragicb0t Feb 22 '18

Please correct me if I am wrong. I think PWAs are useless, all it does is show me stale data until I have a working connection. I would like my app to show me no internet connection rather than showing me 17° when actually it is 25°C outside. The concept of offline is more appropriate for games.

2

u/jimmerioles Feb 22 '18

PWAs are excellent solution to certain problems that it fits. But I guess your problem is just about the implementation detail.

4

u/CSEngineer13 Feb 22 '18

Eh. You can cache forecasted data for a few days out, and use it when fresh data is not available? But you should indicate as much.

3

u/OmegaVesko Feb 22 '18

A weather app is indeed a rather poor case study for a PWA, but even if you're offline, it can still show you:

  • Cached forecast data - for a lot of forecasts, you really don't care about whether the data is a couple hours old or not

  • The temperature from the last time you checked it (not so useful, but still)

Regardless, showing stale data is always better than not showing anything at all. It's a problem if it shows stale data without pointing out that it's stale, but this would be a UX issue, not a failing of the concept of PWAs.

1

u/Helvanik Feb 22 '18

Or for articles that you loaded once, and want to read later.

1

u/nwsm Feb 22 '18

agreed it's a bad example. A better hello world for PWA would be a classic TODO / grocery list app. It's useful offline but user doesn't have to install an actual ios/android app.

1

u/[deleted] Feb 22 '18

Some general ideas for PWA:

  • mail app that allows you to read/write emails even if you're offline (hospital/airport w no service)

  • calculator (sunrise/sunset, moonphase, temperature conversions)

  • camera filter app

  • game (as you mentioned)

These don't require internet, you can use while camping for example.

I would like my app to show me no internet connection rather than showing me 17° when actually it is 25°C outside.

Agreed. This is just a UX improvement and not a mark against PWAs imo.

1

u/name_was_taken Feb 22 '18

I agree. This is a nice example of how to create a PWA, but it's a horrible example of a PWA-worthy app.

That said, ignoring the offline stuff, PWAs are still good for otherwise pointless apps because they'll load up faster the second time you go there, even if they have to get fresh data.

2

u/[deleted] Feb 22 '18

can you write in the readme how to run it locally? Sorry, node noob. Also, how do you package and deploy something like this?

2

u/The_Monodon Feb 22 '18

“Please connect to internet to fetch latest forecast :)”

:(

2

u/macfly669 Feb 23 '18

Great! Nice work !

Wich vue pwa's template do you use ?

1

u/dogofpavlov Feb 22 '18

Hi please excuse my ignorance, this is really interesting to me. I am completely new to understanding PWA. I'm trying to understand the benefits of doing this. When you say it works "offline", that requires you to have visited the website before right? I went to the URL with an internet connection, and then again when I enabled airplane mode, and although it does seem to load the html,css, it says "Please connect to internet to fetch latest forcast", with all the values being at 0. So when you say "works offline" does that really just mean, when you access the URL again you see some html/css instead of just a "You are offline" white screen in the mobile browser? I guess thats pretty neat. Was this just for you to learn? thanks for your time

1

u/GrosSacASacs Feb 22 '18

You are mostly right, in a more advanced stage of the project, instead of displaying values as 0 it could display the latest available locally (from the last online visit).

1

u/mac_iver Feb 22 '18

Nice I like the minimalist design. Though you should probably hide your app-id in the repo.

1

u/schm0 Feb 22 '18

After downloading the page, it says I'm not connected to the internet.

1

u/iTouchTheSky Feb 23 '18

Turning location on, loading the page, turning it off and reloading doesn't give the last fetch data .. it will only tell us to connect to the internet

1

u/tomtht123 Feb 23 '18

I love the simplicity, make it a iOS app

1

u/srmogita Feb 23 '18

Hey, this is an inspiring project.

However I created a demo project and set up the registerServiceWorker codes like what's in your repo, the "yarn build" doesn't output anything related to the service worker, no "service-worker.js" nor the "precache" file got generated. Do you modified anything in your local webpack config or there was something I missed from your repo I wonder?

1

u/srmogita Feb 25 '18

Well figured this out that I missed a dev dep called "@vue/cli-plugin-pwa". Installed it and built again, simply worked.

1

u/daedius Web Components fanboy Feb 23 '18

it doesn't seem to remember my choice of Fahrenheit temp

1

u/TwoMovies Feb 23 '18

Add manual location submit button, but looks great, nice design. A lot of users using proxies, vpn etc. Good luck.

0

u/nothingduploading Feb 22 '18

are we doing offline again? That was like 2011.

0

u/[deleted] Feb 22 '18

[deleted]

1

u/jimmerioles Feb 22 '18

Nice catch! definitely need a bit of tweaking.Thanks for the feedback!