Skip to content

Instantly share code, notes, and snippets.

@SrChach
Created November 23, 2020 19:08
Show Gist options
  • Save SrChach/ddec6dcc639fb588dc204436d27e6e0e to your computer and use it in GitHub Desktop.
Save SrChach/ddec6dcc639fb588dc204436d27e6e0e to your computer and use it in GitHub Desktop.
Electron - disable CORS problem

Disabling CORS check in an Electron / Quasar App

Sometimes we try to do http requests from Electron to any page and it seems to be blocked by the "Browser engine" that comes with the renderer proccess.

There's one solution

The hack

  1. Change the BrowserWindow security config

Within the main process, we'll search for new BrowserWindow. That's the window declaration.

Inside it, we'll search for webPreferences, and change or add the websSecurity: false property.

mainWindow = new BrowserWindow({
    width: 1000,
    height: 600,
    //... more code
    webPreferences: {
      // ... more code

      // Add the following line.
      webSecurity: false,
    }
  })
  1. Disabling CORS speciffically

Then, on the instance, the app variable has the main app. So, to disable the CORS, in any part of the main proccess disable the CORS feaature with the next line.

app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')

And that's all! Re-run your app to see the changes.

Note: in Quasar the main process is in src-electron/main-process/electron-main.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment