Skip to content

Commit 791c7d7

Browse files
committed
Backfill remote asset listing when needed
1 parent 2546c65 commit 791c7d7

File tree

1 file changed

+42
-2
lines changed
  • packages/playground/wordpress/src

1 file changed

+42
-2
lines changed

packages/playground/wordpress/src/boot.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import {
1818
wordPressRewriteRules,
1919
} from '.';
2020
import { joinPaths } from '@php-wasm/util';
21+
import { logger } from '@php-wasm/logger';
22+
import {
23+
getLoadedWordPressVersion,
24+
isSupportedWordPressVersion,
25+
} from './version-detect';
2126

2227
export type PhpIniOptions = Record<string, string>;
2328
export type Hook = (php: PHP) => void | Promise<void>;
@@ -190,13 +195,48 @@ export async function bootWordPress(options: BootOptions) {
190195
php.defineConstant('WP_HOME', options.siteUrl);
191196
php.defineConstant('WP_SITEURL', options.siteUrl);
192197

193-
// @TODO Assert WordPress core files are in place
194-
195198
// Run "before database" hooks to mount/copy more files in
196199
if (options.hooks?.beforeDatabaseSetup) {
197200
await options.hooks.beforeDatabaseSetup(php);
198201
}
199202

203+
// @TODO Assert WordPress core files are in place
204+
205+
const remoteAssetListPath = joinPaths(
206+
requestHandler.documentRoot,
207+
'wordpress-remote-asset-paths'
208+
);
209+
// Use a common WP static asset to guess whether we are using a minified WP build.
210+
const commonStaticAssetPath = joinPaths(
211+
requestHandler.documentRoot,
212+
'wp-admin/css/common.css'
213+
);
214+
if (
215+
!php.fileExists(remoteAssetListPath) &&
216+
!php.fileExists(commonStaticAssetPath)
217+
) {
218+
// We are missing the remote asset listing and a common static file.
219+
// This looks like a minified WP build missing a remote asset listing.
220+
const loadedWordPressVersion = await getLoadedWordPressVersion(
221+
requestHandler
222+
);
223+
if (isSupportedWordPressVersion(loadedWordPressVersion)) {
224+
// TODO: Is this an absolute URI we can count on?
225+
const wpAssetBaseUrl = `/wp-${loadedWordPressVersion}`;
226+
const listUrl = `${wpAssetBaseUrl}/wordpress-remote-asset-paths`;
227+
try {
228+
const remoteAssetPaths = await fetch(listUrl).then((res) =>
229+
res.text()
230+
);
231+
php.writeFile(remoteAssetListPath, remoteAssetPaths);
232+
} catch (e) {
233+
logger.warn(
234+
`Failed to fetch remote asset paths from ${listUrl}`
235+
);
236+
}
237+
}
238+
}
239+
200240
if (options.sqliteIntegrationPluginZip) {
201241
await preloadSqliteIntegration(
202242
php,

0 commit comments

Comments
 (0)