FirebaseExtended / codelab-friendlyeats-flutter

FlutterFire Firestore codelab. Develop a cross platform restaurant recommendation app with Flutter.
Apache License 2.0
78 stars 36 forks source link

Images not loading in codelab app. CORS error is thrown. #10

Open spetz83 opened 3 years ago

spetz83 commented 3 years ago

Browser: Chrome Browser version: 88.0.4324.146 Operating system: Windows 10 Pro Operating system version: 19042.789

What steps will reproduce the problem:

  1. Follow the codelab verbatim to step 9 and run the app.

What is the expected result? App should run and display cards with pictures for each restaurant.

What happens instead of that? App loads and cards are displayed, but pictures do not load. Console reports CORS errors.

Please provide any other information below, and attach a screenshot if possible.

Example Error Message: Access to XMLHttpRequest at 'https://storage.googleapis.com/firestorequickstarts.appspot.com/food_11.png' from origin 'http://localhost:60020' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

image

ditman commented 3 years ago

Oh, Flutter recently changed its web renderer to "auto" from "html", and in desktop browsers "auto" prefers "canvaskit". In canvaskit, "network" images from a domain different to the one your website is hosted on (cross-origin) require CORS setup.

A quick way for this to be circumvented so you can keep going with the codelab is to use the html renderer. Please try:

flutter run -d chrome --web-renderer html

More info:

I'll keep this open so we can revisit the instructions and make a note of that small change. Thanks for reporting the issue!

PS: Same thing a bunch of steps down, when building for web: flutter build web --web-renderer html needs to be added to the build command!

gOzaru commented 3 years ago

Still failed to load Network Image. I have defined CORS.json as described in this website. This is my config gor CORS.json (link)

The error lines: Failed to load network image. Image URL: https://storage.googleapis.com/firestorequickstarts.appspot.com/food_8.png Trying to load an image from another domain? Find answers at: https://flutter.dev/docs/development/platform-integration/web-images

ditman commented 3 years ago

@gOzaru that should be the way to go. From what I see from your links, the image still is not being returned with the correct CORS headers.

You can verify this by looking at the "Response Headers" section of the Network panel for the request:

Screen Shot 2021-03-12 at 5 42 11 PM

Or (maybe more straightforward), just attempting to fetch the image in the JS console:

Screen Shot 2021-03-12 at 5 45 04 PM

Once the images start to come back with the correct Access-Control-Allow-Origin header, images will be fine.


@gOzaru reading further here: https://cloud.google.com/storage/docs/cross-origin#cors-elements, it doesn't seem that you need to add Access-Control-Allow-Origin as the responseHeader of your JSON, I'd try leaving it blank:

[
    {
      "origin": ["*"],
      "method": ["GET"],
      "maxAgeSeconds": 3600
    }
]

(Also, I'm not sure if * is an allowed origin, you might need to be more specific)


Here's some Troubleshooting tips, it doesn't seem like * is a valid origin. I've seen examples with Access-Control-Allow-Origin in the responseHeader as yours.

gOzaru commented 3 years ago

I followed your suggestion and it seems that tutorial's images are in cloud storage where the access to there is denied. This is the url I use in origin: https://storage.googleapis.com/firestorequickstarts.appspot.com/

I have been doing trial and error for this tutorial since yesterday and all comes to this conclusion: The tutorial is not made for social learning purpose as they don't teach us how to fetch images from their cloud storage. They FORGOT THAT. This tutorial is made for themselves. This is wasting my time. I am gonna seek another tutorial. Thank you for your quick support, but this is useless. They should make tutorial about how to fetch images from our own cloud storage where the images must be uploaded there, not from their cloud storage which is denied. This logic of thinking is wrong in teaching tutorial.

ditman commented 3 years ago

@gOzaru you can always try --web-renderer html so you don't encounter the CORS issue with images, as suggested above.

j-vasil commented 3 years ago
j-vasil commented 3 years ago

@ditman said:

PS: Same thing a bunch of steps down, when building for web: flutter build --web-renderer html needs to be added to the build command!

I tried deploying using this build command (looks like your comment omitted the 'web'): $ flutter build web --web-renderer html

When I deployed the page this way the app page came up as a blank page. Depending upon which responses I gave to the 'init hosting' questions, I also sometimes saw an opening page that said "Welcome -- Firebase Hosting Setup Complete". All my build steps are documented in a response to issue https://github.com/FirebaseExtended/codelab-friendlyeats-flutter/issues/9.

iekuzmuk commented 3 years ago

this is how it works: flutter run -d chrome --web-renderer html

omartinma commented 2 years ago

@ditman what would be the solution if we need canvaskit?

ditman commented 2 years ago

@omartinma ensure that your image files are returned from the CDN with the appropriate CORS Response Headers for your app, at the bare minimum: Access-Control-Allow-Origin, as described in the error message.

For example, this is how'd you configure a GCP storage bucket to add the CORS response headers to it: https://cloud.google.com/storage/docs/configuring-cors#gsutil

And this is how you'd configure an S3 bucket: https://docs.aws.amazon.com/AmazonS3/latest/userguide/enabling-cors-examples.html (I think, never tried this one! :P)

dasharath8998 commented 1 year ago

omartinma If you really need the canvaskit, the below solution works for me for the S3 bucket. Please refer to the steps in the below link and also to have it secure check, given an example of mine as mentioned below.

Steps to follow: https://stackoverflow.com/a/73807573

A better JSON file to make sure your images are secure and not available publically:

 [
      {
        "AllowedHeaders": ["*"],
        "AllowedMethods": ["GET", "POST"],
        "AllowedOrigins": ["https://foo.bar.org"],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
      }
    ]
ditman commented 1 year ago

How are you people still finding this repo? The codelab should have been archived a while ago :)

DylanoHa commented 2 months ago

How are you people still finding this repo? The codelab should have been archived a while ago :)

It's one of the first to show up on google when searching for the cors error when using canvaskit

ditman commented 2 months ago

This should be the main tracking issue:

The codelab itself would need a big rewrite; right now its code is severely outdated!