Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom protocol not working in development #36

Closed
4 tasks done
michael-dm opened this issue Oct 20, 2022 · 2 comments
Closed
4 tasks done

Custom protocol not working in development #36

michael-dm opened this issue Oct 20, 2022 · 2 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@michael-dm
Copy link

Describe the bug

Setting up a custom protocol does not work in development (with a dev server). It works fine when loading the html file.
Error is ERR_UNKNOWN_URL_SCHEME.

import { protocol } from 'electron'

protocol.registerSchemesAsPrivileged([
    {
      scheme: 'file',
      privileges: {
        supportFetchAPI: true,
        bypassCSP: true,
        corsEnabled: true
      }
    }
  ])

in renderer :

<img src="file:///Users/path-to/img1.jpg" />

Tested on Mac ARM.

Electron-Vite Version

1.0.10

Electron Version

21.1.1

Vite Version

3.1.8

Validations

@michael-dm michael-dm added the bug Something isn't working label Oct 20, 2022
@alex8088
Copy link
Owner

alex8088 commented Oct 20, 2022

hi, @michael-dm

Can you give me more details to reproduce this. And my solution for custom protocol :

  • register scheme before the ready event of the app
protocol.registerSchemesAsPrivileged([
  {
    scheme: 'app',
    privileges: {
      supportFetchAPI: true,
      bypassCSP: true,
      corsEnabled: true
    }
  }
])
  • register protocol to convert app:// to file://
protocol.registerFileProtocol('app', (request, callback) => {
  const absUrl = path.join(__dirname, '../../build', request.url.substr(6))
  return callback({ path: path.normalize(absUrl) })
})
  • use in renderer (vue )
<script setup lang="ts">
const icon = 'app:///icon.png'
</script>

<template>
  <img :src="icon" />
</template>

Furthermore, it is not necessary to register scheme for file:// that supported by default.

@michael-dm
Copy link
Author

I didn't realise you still had to use "registerFileProtocol" when using "registerSchemesAsPrivileged". Thanks it works now 👍

file:// is not supported when "webSecurity" is enabled, best practice is to filter which files the renderer can access using a custom protocol.

@alex8088 alex8088 added good first issue Good for newcomers question Further information is requested and removed bug Something isn't working labels Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants