Skip to content

Commit 118c85e

Browse files
committed
perf: fixes and perf of Laravel on subsequent launches
1 parent 15de455 commit 118c85e

File tree

2 files changed

+50
-24
lines changed
  • resources/js/electron-plugin

2 files changed

+50
-24
lines changed

resources/js/electron-plugin/dist/server/php.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ const bootstrapCache = join(app.getPath('userData'), 'bootstrap', 'cache');
2424
const argumentEnv = getArgumentEnv();
2525
const appPath = getAppPath();
2626
mkdirpSync(bootstrapCache);
27-
function runningProdVersion() {
28-
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'));
29-
}
3027
function runningSecureBuild() {
3128
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'));
3229
}
3330
function shouldMigrateDatabase(store) {
3431
return store.get('migrated_version') !== app.getVersion()
3532
&& process.env.NODE_ENV !== 'development';
3633
}
34+
function shouldOptimize(store) {
35+
return store.get('optimized_version') !== app.getVersion()
36+
&& process.env.NODE_ENV !== 'development';
37+
}
3738
function getPhpPort() {
3839
return __awaiter(this, void 0, void 0, function* () {
3940
return yield getPort({
@@ -175,11 +176,11 @@ function getDefaultEnvironmentVariables(secret, apiPort) {
175176
NATIVEPHP_VIDEOS_PATH: getPath('videos'),
176177
NATIVEPHP_RECENT_PATH: getPath('recent'),
177178
};
178-
if (runningProdVersion()) {
179+
if (runningSecureBuild()) {
179180
variables.APP_SERVICES_CACHE = join(bootstrapCache, 'services.php');
180181
variables.APP_PACKAGES_CACHE = join(bootstrapCache, 'packages.php');
181182
variables.APP_CONFIG_CACHE = join(bootstrapCache, 'config.php');
182-
variables.APP_ROUTES_CACHE = join(bootstrapCache, 'routes.php');
183+
variables.APP_ROUTES_CACHE = join(bootstrapCache, 'routes-v7.php');
183184
variables.APP_EVENTS_CACHE = join(bootstrapCache, 'events.php');
184185
}
185186
return variables;
@@ -202,18 +203,31 @@ function serveApp(secret, apiPort, phpIniSettings) {
202203
cwd: appPath,
203204
env
204205
};
205-
const store = new Store();
206+
const store = new Store({
207+
name: 'nativephp',
208+
});
206209
if (!runningSecureBuild()) {
207210
callPhpSync(['artisan', 'storage:link', '--force'], phpOptions, phpIniSettings);
208211
}
209-
if (runningProdVersion()) {
212+
if (shouldOptimize(store)) {
210213
console.log('Caching view and routes...');
211-
callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings);
214+
let result = callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings);
215+
if (result.status !== 0) {
216+
console.error('Failed to cache view and routes:', result.stderr.toString());
217+
}
218+
else {
219+
store.set('optimized_version', app.getVersion());
220+
}
212221
}
213222
if (shouldMigrateDatabase(store)) {
214223
console.log('Migrating database...');
215-
callPhpSync(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
216-
store.set('migrated_version', app.getVersion());
224+
let result = callPhpSync(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
225+
if (result.status !== 0) {
226+
console.error('Failed to migrate database:', result.stderr.toString());
227+
}
228+
else {
229+
store.set('migrated_version', app.getVersion());
230+
}
217231
}
218232
if (process.env.NODE_ENV === 'development') {
219233
console.log('Skipping Database migration while in development.');

resources/js/electron-plugin/src/server/php.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ const appPath = getAppPath();
2121

2222
mkdirpSync(bootstrapCache);
2323

24-
function runningProdVersion() {
25-
//TODO: Check if the app is running the production version
26-
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'))
27-
}
28-
2924
function runningSecureBuild() {
3025
return existsSync(join(appPath, 'build', '__nativephp_app_bundle'))
3126
}
@@ -35,6 +30,11 @@ function shouldMigrateDatabase(store) {
3530
&& process.env.NODE_ENV !== 'development';
3631
}
3732

33+
function shouldOptimize(store) {
34+
return store.get('optimized_version') !== app.getVersion()
35+
&& process.env.NODE_ENV !== 'development';
36+
}
37+
3838
async function getPhpPort() {
3939
return await getPort({
4040
host: '127.0.0.1',
@@ -253,11 +253,11 @@ function getDefaultEnvironmentVariables(secret, apiPort): EnvironmentVariables {
253253
};
254254

255255
// Only add cache paths if in production mode
256-
if(runningProdVersion()) {
256+
if(runningSecureBuild()) {
257257
variables.APP_SERVICES_CACHE = join(bootstrapCache, 'services.php');
258258
variables.APP_PACKAGES_CACHE = join(bootstrapCache, 'packages.php');
259259
variables.APP_CONFIG_CACHE = join(bootstrapCache, 'config.php');
260-
variables.APP_ROUTES_CACHE = join(bootstrapCache, 'routes.php');
260+
variables.APP_ROUTES_CACHE = join(bootstrapCache, 'routes-v7.php');
261261
variables.APP_EVENTS_CACHE = join(bootstrapCache, 'events.php');
262262
}
263263

@@ -289,7 +289,9 @@ function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
289289
env
290290
};
291291

292-
const store = new Store();
292+
const store = new Store({
293+
name: 'nativephp', // So it doesn't conflict with settings of the app
294+
});
293295

294296
// Make sure the storage path is linked - as people can move the app around, we
295297
// need to run this every time the app starts
@@ -298,18 +300,28 @@ function serveApp(secret, apiPort, phpIniSettings): Promise<ProcessResult> {
298300
}
299301

300302
// Cache the project
301-
if (runningProdVersion()) {
303+
if (shouldOptimize(store)) {
302304
console.log('Caching view and routes...');
303-
// TODO: once per version
304-
callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings)
305+
306+
let result = callPhpSync(['artisan', 'optimize'], phpOptions, phpIniSettings);
307+
308+
if (result.status !== 0) {
309+
console.error('Failed to cache view and routes:', result.stderr.toString());
310+
} else {
311+
store.set('optimized_version', app.getVersion())
312+
}
305313
}
306314

307315
// Migrate the database
308316
if (shouldMigrateDatabase(store)) {
309317
console.log('Migrating database...')
310-
callPhpSync(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings)
311-
// TODO: fail if callPhp fails and don't store migrated version
312-
store.set('migrated_version', app.getVersion())
318+
let result = callPhpSync(['artisan', 'migrate', '--force'], phpOptions, phpIniSettings);
319+
320+
if (result.status !== 0) {
321+
console.error('Failed to migrate database:', result.stderr.toString());
322+
} else {
323+
store.set('migrated_version', app.getVersion())
324+
}
313325
}
314326

315327
if (process.env.NODE_ENV === 'development') {

0 commit comments

Comments
 (0)