Skip to content

Commit 1a8a4d9

Browse files
committed
docs: update static assets instructions
1 parent 396f6f8 commit 1a8a4d9

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

docs/guide/guide.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,33 @@ module.exports = {
3737

3838
## Handling Static Assets
3939

40-
### Renderer Process (Vue App)
40+
Static assets work the same as a regular web app. Read Vue CLI's documentation [here](https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling) for more information.
4141

42-
In the renderer process, static assets work similarly to a regular app. Read Vue CLI's documentation [here](https://cli.vuejs.org/guide/html-and-static-assets.html) before continuing. However, there are a few changes made:
42+
<!-- prettier-ignore -->
43+
:::tip __static
44+
Available only in Electron, the global variable `__static` is added to the main and renderer process. It is set to the path of your public folder on disk. This is useful if you need to use Node APIs on the file, such as`fs.readFileSync`or`child_process.spawn`. Note that files in the public folder are read-only in production as they are packaged into a `.asar` archive. If you need files to be writeable, use [electron-builder's extraResources config](https://www.electron.build/configuration/contents#extraresources).
45+
:::
4346

44-
- The `__static` global variable is added. It provides a path to your public directory in both development and production. Use this to read/write files in your app's public directory.
45-
- In production, the `process.env.BASE_URL` is replaced with the path to your app's files.
46-
47-
**Note: `__static` is not available in regular build/serve. It should only be used in electron to read/write files on disk. To import a file (img, script, etc...) and not have it be transpiled by webpack, use the `process.env.BASE_URL` instead.**
48-
49-
### Main Process (`background.js`)
50-
51-
The main process won't have access to `process.env.BASE_URL` or `src/assets`. However, you can still use `__static` to get a path to your public directory in development and production.
47+
:::warning
48+
Sourcing images from the `public` folder will fail on v2.0 beta and rc.1. Please upgrade to v2.0.0-rc.2 for a fix.
49+
:::
5250

5351
### Examples:
5452

5553
```vue
56-
<!-- Renderer process only -->
57-
<!-- This image will be processed by webpack and placed under img/ -->
54+
<!-- To load an image that will be processed by webpack -->
5855
<img src="./assets/logo.png">
59-
<!-- Renderer process only -->
60-
<!-- This image will no be processed by webpack, just copied-->
56+
<!-- To load an image from the `public` folder which webpack will not process, just copy -->
6157
<!-- imgPath should equal `path.join(process.env.BASE_URL, 'logo.png')` -->
6258
<img :src="imgPath">
63-
<!-- Both renderer and main process -->
64-
<!-- This will read the contents of public/myText.txt -->
6559
<script>
60+
// Only works in electron serve/build
61+
// Will not work in renderer process unless you enable nodeIntegration
62+
// Expects myText.txt to be placed in public folder
63+
6664
const fs = require('fs')
6765
const path = require('path')
6866
69-
// Expects myText.txt to be placed in public folder
7067
const fileLocation = path.join(__static, 'myText.txt')
7168
const fileContents = fs.readFileSync(fileLocation, 'utf8')
7269

docs/guide/security.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Now, you can access `ipcRenderer` with `window.ipcRenderer` in your Vue app.
2929

3030
## Loading Local Images/Resources
3131

32-
If WebSecurity is enabled, you won't be able to load resources from the file system, ie `<img src="file://image.png"/>`. [Disabling WebSecurity is strongly discouraged](https://www.electronjs.org/docs/tutorial/security#5-do-not-disable-websecurity), so you should instead use the following technique to load local resources and keep WebSecurity enabled.
32+
If WebSecurity is enabled, you won't be able to load resources from the file system, ie `<img src="file:///path/to/some/image.png"/>`. However, you will still be able to load images and other resources from the `public` folder, see [handling static assets](./guide.html#handling-static-assets). If you need to load resources from outside of the public folder you will have to disable WebSecurity or use a custom protocol. [Disabling WebSecurity is strongly discouraged](https://www.electronjs.org/docs/tutorial/security#5-do-not-disable-websecurity), so you should instead use the following technique to load local resources and keep WebSecurity enabled.
3333

3434
Add the following to your main process file (`background.(js|ts)` by default):
3535

0 commit comments

Comments
 (0)