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

Support query params #37

Merged
merged 12 commits into from
Jan 22, 2024
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ declare namespace electronServe {
/**
Load the index file in the window.
*/
type loadURL = (window: BrowserWindow) => Promise<void>;
type loadURL = (window: BrowserWindow, searchParameters?: Record<string, string> | URLSearchParams) => Promise<void>;
}

/**
Expand All @@ -70,6 +70,9 @@ let mainWindow;
await loadURL(mainWindow);
// Or optionally with search parameters.
await loadURL(mainWindow, {id: 4, foo: 'bar'});
// The above is equivalent to this:
await mainWindow.loadURL('app://-');
// The `-` is just the required hostname.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ module.exports = options => {
session.protocol.registerFileProtocol(options.scheme, handler);
});

return async window_ => {
await window_.loadURL(`${options.scheme}://${options.hostname}`);
return async (window_, searchParameters) => {
const queryString = searchParameters ? '?' + new URLSearchParams(searchParameters).toString() : '';
await window_.loadURL(`${options.scheme}://${options.hostname}${queryString}`);
};
};
22 changes: 21 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ let mainWindow;

await loadURL(mainWindow);

// Or optionally with search parameters.
await loadURL(mainWindow, {id: 4, foo: 'bar'});

// The above is equivalent to this:
await mainWindow.loadURL('app://-');
// The `-` is just the required hostname
Expand All @@ -37,7 +40,7 @@ let mainWindow;

## API

### serve(options)
### loadUrl = serve(options)

#### options

Expand Down Expand Up @@ -86,6 +89,23 @@ Default: [`electron.session.defaultSession`](https://electronjs.org/docs/api/ses

The [partition](https://electronjs.org/docs/api/session#sessionfrompartitionpartition-options) the protocol should be installed to, if you're not using Electron's default partition.

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
### loadUrl(window, searchParameters?)

The `serve` function returns a `loadUrl` function, which you use to serve your HTML file in that window.

##### window

*Required*\
Type: `BrowserWindow`

The window to load the file in.

##### searchParameters

Type: `object | URLSearchParams`

Key value pairs or an [`URLSearchParams` instance](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) to set as the search parameters.

## Related

- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules
Expand Down
15 changes: 15 additions & 0 deletions test/fixture-search-params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
const {app, BrowserWindow} = require('electron');
const {join} = require('path');
const serve = require('..');

const loadUrl = serve({directory: join(__dirname, 'sub')});

let mainWindow;

(async () => {
await app.whenReady();

mainWindow = new BrowserWindow();
loadUrl(mainWindow, {id: 4, foo: 'bar'});
})();
9 changes: 9 additions & 0 deletions test/index 2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>🚀</h1>
</body>
</html>
15 changes: 15 additions & 0 deletions test/sub/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script>
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id');
const foo = urlParams.get('foo');
document.getElementById('params').innerHTML = `${id}${foo}`;
</script>
</head>
<body>
<h1 id="params"></h1>
</body>
</html>
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ test('serves directory custom file', async t => {
await client.waitUntilTextExists('h1', '🦉', 5000);
t.pass();
});

test('serves directory index with search params', async t => {
t.context.spectron = new Application({
path: electron,
args: ['fixture-search-params.js']
});
await t.context.spectron.start();
const {client} = t.context.spectron;
await client.waitUntilWindowLoaded();
await client.waitUntilTextExists('h1', '4bar', 5000);
t.pass();
});