electron: [Bug]: net::ERR_UNKNOWN_URL_SCHEME when redirecting to a custom scheme during an intercept handler

Preflight Checklist

Electron Version

15.3.4

What operating system are you using?

macOS

Operating System Version

macOS Big Sur 11.1

What arch are you using?

x64

Last Known Working Electron version

11.5.0

Expected Behavior

Rewrite and fetch data from backend service, without net::ERR_UNKNOWN_URL_SCHEME error. Protocol schema is registered, but it seems that in newer versions of Electron (v12.0.0+) this doesn’t work.

Actual Behavior

My team is having an issue with the registration of the custom protocol. We are getting net::ERR_UNKNOWN_URL_SCHEME error. This occurred after updating the Electron version from 11.x.x to any more recent version: 12, 13, 14, 15.

We wanted to intercept /api/ calls and add certain logic that will update the local database using request/response data. Following protocol is defined:

export const CUSTOM_PROTOCOL = "myapi";
export const CUSTOM_PROTOCOL_MARKER = "myapi://";

The protocol schema is registered in the main file:

// Register custom protocol
protocol.registerSchemesAsPrivileged([
  {
    scheme: CUSTOM_PROTOCOL,
    privileges: {
      standard: true,
      secure: true,
      stream: true,
      bypassCSP: true,
      supportFetchAPI: true,
    },
  },
]);

After Electron has finished initialization, filter and handler is registered.

app
  .whenReady()
  .then(async () => {
    // webRequest onBeforeRequest filter for /api/ requests that
    // redirects HTTP request to new protocol.
    registerInterceptor();

    // On ready, add handler
    protocol.registerStreamProtocol(
      CUSTOM_PROTOCOL,
      interceptedProtocolHandler
    );
	...
})
.catch(console.log);

In previous versions of Electron, this worked. I am not sure, is something changed regarding the registration of custom protocols? We have checked the documentation, breaking changes, release notes, but there are no details regarding this.

Testcase Gist URL

https://gist.github.com/30cd9f04765f958b9c6741b004aa7a7c

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 16 (3 by maintainers)

Most upvoted comments

bump

Electron 18. Same issue 😦

// In the main file
protocol.registerSchemesAsPrivileged([
	{
		scheme: 'atom',
		privileges: {
			bypassCSP: true,
			corsEnabled: true,
			secure: true,
			standard: true,
			supportFetchAPI: true
		}
	}
]);

// When app is ready
protocol.registerHttpProtocol('atom', (request, callback) => {
		request.url = path.join(__dirname, '../assets/bg/bg1.png');
		callback(request);
});

// Before request
window.webContents.session.webRequest.onBeforeRequest({ urls: ['https://*/*'] }, (details, callback) => {
	if (details.url === 'https://mywebsite.com/bg/bg1.png') {
		callback({
			redirectURL: 'atom://bg1.png'
		});
	} else {
		// Proceed normally
		callback({});
	}
});

net::ERR_UNKNOWN_URL_SCHEME

@redstonekasi don’t expect anything 😆 We had to use some workarounds.