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
feat: allow customizing browser data location #33554
Changes from all commits
e00a83d
6eaaff3
6e03876
cc18981
a4a5282
89ceed4
56ade71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -635,8 +635,18 @@ Returns `string` - The current application directory. | |
* `%APPDATA%` on Windows | ||
* `$XDG_CONFIG_HOME` or `~/.config` on Linux | ||
* `~/Library/Application Support` on macOS | ||
* `userData` The directory for storing your app's configuration files, which by | ||
default it is the `appData` directory appended with your app's name. | ||
* `userData` The directory for storing your app's configuration files, which | ||
by default is the `appData` directory appended with your app's name. By | ||
convention files storing user data should be written to this directory, and | ||
it is not recommended to write large files here because some environments | ||
may backup this directory to cloud storage. | ||
* `sessionData` The directory for storing data generated by `Session`, such | ||
as localStorage, cookies, disk cache, downloaded dictionaries, network | ||
state, devtools files. By default this points to `userData`. Chromium may | ||
write very large disk cache here, so if your app does not rely on browser | ||
storage like localStorage or cookies to save user data, it is recommended | ||
to set this directory to other locations to avoid polluting the `userData` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think it would be a good idea to recommend a particular location here! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is hard to recommend a particular location here within a short paragraph, I would like to introduce a new path that users can easily use in their app |
||
directory. | ||
* `temp` Temporary directory. | ||
* `exe` The current executable file. | ||
* `module` The `libchromiumcontent` library. | ||
|
@@ -686,9 +696,9 @@ In that case, the directory should be created with `fs.mkdirSync` or similar. | |
|
||
You can only override paths of a `name` defined in `app.getPath`. | ||
|
||
By default, web pages' cookies and caches will be stored under the `userData` | ||
By default, web pages' cookies and caches will be stored under the `sessionData` | ||
directory. If you want to change this location, you have to override the | ||
`userData` path before the `ready` event of the `app` module is emitted. | ||
`sessionData` path before the `ready` event of the `app` module is emitted. | ||
|
||
### `app.getVersion()` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const http = require('http'); | ||
const { app, ipcMain, BrowserWindow } = require('electron'); | ||
|
||
if (process.argv.length > 3) { | ||
app.setPath(process.argv[2], process.argv[3]); | ||
} | ||
|
||
const html = ` | ||
<script> | ||
async function main() { | ||
localStorage.setItem('myCat', 'Tom') | ||
const db = indexedDB.open('db-name', 1) | ||
await new Promise(resolve => db.onsuccess = resolve) | ||
await navigator.serviceWorker.register('sw.js', {scope: './'}) | ||
} | ||
|
||
main().then(() => { | ||
require('electron').ipcRenderer.send('success') | ||
}) | ||
</script> | ||
`; | ||
|
||
const js = 'console.log("From service worker")'; | ||
|
||
app.once('ready', () => { | ||
ipcMain.on('success', () => { | ||
app.quit(); | ||
}); | ||
|
||
const server = http.createServer((request, response) => { | ||
if (request.url === '/') { | ||
response.writeHead(200, { 'Content-Type': 'text/html' }); | ||
response.end(html); | ||
} else if (request.url === '/sw.js') { | ||
response.writeHead(200, { 'Content-Type': 'text/javascript' }); | ||
response.end(js); | ||
} | ||
}).listen(0, '127.0.0.1', () => { | ||
const serverUrl = 'http://127.0.0.1:' + server.address().port; | ||
const mainWindow = new BrowserWindow({ show: false, webPreferences: { webSecurity: true, nodeIntegration: true, contextIsolation: false } }); | ||
mainWindow.loadURL(serverUrl); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "electron-test-set-path", | ||
"main": "main.js" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does Chromium have this problem? I would be surprised if Chrome's caches are uploaded to cloud storage on any platform...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chrome stores all data under
%LOCALAPPDATA%/Google/Chrome
, unlike Electron apps it writes nothing to%APPDATA%
.Also Chrome writes browsing data (i.e.
sessionData
) to a subdirectory (%LOCALAPPDATA%/Google/Chrome/User Data/Default
) instead of the root%LOCALAPPDATA%/Google/Chrome
, which is also different from Electron where we mix browsing data and other types of data under the same directory.We really made some mistakes when choosing where to store data in Electron.
I'm going to expose a few more paths in
app.getPath
and write a guide on how to setup the directories to correct locations after this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hang on—so Chrome can choose the session data directory per-profile, then! Chromium will put things under
userData/Profile 1
,userData/Profile 2
and so on. Shouldn't we offer the same capability?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do the same thing in Electron, sessions created by
session.fromPartition
write data to$sessionData/Partitions/$partitionName
subdirectory instead of the root directory.The difference is Electron writes data of the default session into the root
$sessionData
directory, while Chrome browser writes into theUser Data/Default
subdirectory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, Chrome writes to
User Data/$partitionName
, right?Default
is a name of a partition.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Chrome treats the default profile the same with other profiles and writes data to
User Data/$name
.Electron treats the default session specially and writes its data to
$sessionData
, the data of other sessions are written into$sessionData/Partitions/$partitionName
instead.This was because we did not consider the support of multiple sessions in the early days of Electron.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahhhh, right, got it. Thanks, that makes sense!