From 16293ba57cea96e0f7f3eca12b0e6aef8f3b2f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Fri, 17 May 2024 16:57:56 +0200 Subject: [PATCH] Remote.html: Preload the SQLite database plugin, but only execute it if there's no custom db.php inside wp-content --- .../remote/src/lib/worker-thread.ts | 20 ++++++++++++++- packages/playground/wordpress/src/index.ts | 25 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/playground/remote/src/lib/worker-thread.ts b/packages/playground/remote/src/lib/worker-thread.ts index a29a099e84..cf56ceca62 100644 --- a/packages/playground/remote/src/lib/worker-thread.ts +++ b/packages/playground/remote/src/lib/worker-thread.ts @@ -6,8 +6,12 @@ import { getWordPressModuleDetails, LatestSupportedWordPressVersion, SupportedWordPressVersions, + sqliteDatabaseIntegration, } from '@wp-playground/wordpress-builds'; -import { wordPressRewriteRules } from '@wp-playground/wordpress'; +import { + preloadSqliteIntegration, + wordPressRewriteRules, +} from '@wp-playground/wordpress'; import { PHPRequestHandler } from '@php-wasm/universal'; import { SyncProgressCallback, @@ -57,6 +61,14 @@ if ( wordPressAvailableInOPFS = await playgroundAvailableInOpfs(virtualOpfsDir!); } +// The SQLite integration must always be downloaded, even when using OPFS or Native FS, +// because it can't be assumed to exist in WordPress document root. Instead, it's installed +// in the /internal directory to avoid polluting the mounted directory structure. +downloadMonitor.expectAssets({ + [sqliteDatabaseIntegration.url]: sqliteDatabaseIntegration.size, +}); +const sqliteIntegrationRequest = monitoredFetch(sqliteDatabaseIntegration.url); + // Start downloading WordPress if needed let wordPressRequest = null; if (!wordPressAvailableInOPFS) { @@ -210,6 +222,12 @@ try { }); } + const sqliteIntegrationZip = await (await sqliteIntegrationRequest).blob(); + await preloadSqliteIntegration( + primaryPhp, + new File([sqliteIntegrationZip], 'sqlite.zip') + ); + // Always setup the current site URL. await defineSiteUrl(primaryPhp, { siteUrl: scopedSiteUrl, diff --git a/packages/playground/wordpress/src/index.ts b/packages/playground/wordpress/src/index.ts index 7160af09c8..c20212c13f 100644 --- a/packages/playground/wordpress/src/index.ts +++ b/packages/playground/wordpress/src/index.ts @@ -202,12 +202,21 @@ export async function preloadSqliteIntegration( "'{SQLITE_PLUGIN}'", phpVar(joinPaths(SQLITE_PLUGIN_FOLDER, 'load.php')) ); + const dbPhpPath = joinPaths(await php.documentRoot, 'wp-content/db.php'); + const stopIfDbPhpExists = ``; const SQLITE_MUPLUGIN_PATH = '/internal/shared/mu-plugins/sqlite-database-integration.php'; - await php.writeFile(SQLITE_MUPLUGIN_PATH, dbPhp); + await php.writeFile(SQLITE_MUPLUGIN_PATH, stopIfDbPhpExists + dbPhp); await php.writeFile( `/internal/shared/preload/0-sqlite.php`, - `