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

fix: failed to load page with hash in path in prod #926

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions packages/main/src/mainWindow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {app, BrowserWindow} from 'electron';
import {join} from 'node:path';
import {URL} from 'node:url';
import {join, resolve} from 'node:path';

async function createWindow() {
const browserWindow = new BrowserWindow({
Expand Down Expand Up @@ -31,16 +30,25 @@ async function createWindow() {
});

/**
* URL for main window.
* Vite dev server for development.
* `file://../renderer/index.html` for production and test.
* Load the main page of the main window.
*/
const pageUrl =
import.meta.env.DEV && import.meta.env.VITE_DEV_SERVER_URL !== undefined
? import.meta.env.VITE_DEV_SERVER_URL
: new URL('../renderer/dist/index.html', 'file://' + __dirname).toString();

await browserWindow.loadURL(pageUrl);
if (import.meta.env.DEV && import.meta.env.VITE_DEV_SERVER_URL !== undefined) {
/**
* Load from the Vite dev server for development.
*/
await browserWindow.loadURL(import.meta.env.VITE_DEV_SERVER_URL);
} else {
/**
* Load from the local file system for production and test.
*
* Use BrowserWindow.loadFile() instead of BrowserWindow.loadURL() for WhatWG URL API limitations
* when path contains special characters like `#`.
* Let electron handle the path quirks.
* @see /~https://github.com/nodejs/node/issues/12682
* @see /~https://github.com/electron/electron/issues/6869
*/
await browserWindow.loadFile(resolve(__dirname, '../../renderer/dist/index.html'));
}

return browserWindow;
}
Expand Down
14 changes: 11 additions & 3 deletions packages/main/tests/unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {MockedClass} from 'vitest';
import type {MockedClass, MockedObject} from 'vitest';
import {beforeEach, expect, test, vi} from 'vitest';
import {restoreOrCreateWindow} from '../src/mainWindow';

Expand All @@ -12,6 +12,7 @@ vi.mock('electron', () => {
const bw = vi.fn() as unknown as MockedClass<typeof BrowserWindow>;
bw.getAllWindows = vi.fn(() => bw.mock.instances);
bw.prototype.loadURL = vi.fn((_: string, __?: Electron.LoadURLOptions) => Promise.resolve());
bw.prototype.loadFile = vi.fn((_: string, __?: Electron.LoadFileOptions) => Promise.resolve());
// Use "any" because the on function is overloaded
// eslint-disable-next-line @typescript-eslint/no-explicit-any
bw.prototype.on = vi.fn<any>();
Expand Down Expand Up @@ -40,8 +41,15 @@ test('Should create a new window', async () => {

await restoreOrCreateWindow();
expect(mock.instances).toHaveLength(1);
expect(mock.instances[0].loadURL).toHaveBeenCalledOnce();
expect(mock.instances[0].loadURL).toHaveBeenCalledWith(expect.stringMatching(/index\.html$/));
const instance = mock.instances[0] as MockedObject<BrowserWindow>;
const loadURLCalls = instance.loadURL.mock.calls.length;
const loadFileCalls = instance.loadFile.mock.calls.length;
expect(loadURLCalls + loadFileCalls).toBe(1);
if (loadURLCalls === 1) {
expect(instance.loadURL).toHaveBeenCalledWith(expect.stringMatching(/index\.html$/));
} else {
expect(instance.loadFile).toHaveBeenCalledWith(expect.stringMatching(/index\.html$/));
}
});

test('Should restore an existing window', async () => {
Expand Down