Helloπ!
Im surprised how much I learned from a seemingly easy challenge. I ran into problems with CORS on firefox and lost a lot of time trying to figure it out. Hopefully I found solution. List of things I learned or used creating this project:
- This is first time I used webpack. And more specifically I used
laravel mix
which is a wrapper for webpack and targets the 80% usecase. I didn't realize how powerful tool it is. LINK
- Implement an
environment variable
(link) for my API_KEY. This is a variable whose value is set outside the program to secure our sensitive data. I did it with serverless Lambda function on netlify, so my API_KEY never shows up in my code and no one will steal it. Tutorial
- This is first time I used
object literals
(link) instead of if statement
in Tracker.changeContent function. Shortly, we have an object where the keys are the conditions and the values are the responses. Then we can use the square bracket notation to select the correct value of the object from the argument passed in. This looks clean and I will definitely continue to use this.
- Added
aria-live="polite"
and aria-atomic="true"
to my .tracker__results-wrapper
element to expose dynamic content changes in a way that can be announced by assistive technologies after my results inner elements change content.
- I didn't like that results container with address informations cover the map and user can't do anything with that. Added button to hide that container. More useful on mobile tho.
Errors that I encountered:
- The problem I talked about is only on Firefox. Console throws an CORS error ->
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource
. It turned out that after the recent firefox update you cannot fetch data between different origins. The fix is to send request to a proxy. A proxy acts as an intermediary between a client and server. The proxy server operates in between the frontend web app making the request, and the server that responds with data. Simply add https://cors.bridged.cc/ to any URL youβre fetching from or use cors-anywhere
. Read about this CORS error. Since i work with serverless function on netlify i didn't have to put this in my fetch, because netlify fetching that data for me. But i think that info will be helpful for others.
- Since we're using proxy, we have to somehow get user IP if we want to display user info on init. That is because geo api will return proxy ip address on default. I use cloudflare trace utility to work around this problem. It retunrs a plain-text set of key/value pairs.
Special thanks to @brasspetals for total help, @mattstuddert for helping with these CORS problems and @grace-snow for for throwing the link with object literals on slack. No specific questions here but any additional feedback will be appreciated! Thanks! π