Skip to content

Commit 4fc3a64

Browse files
FindHaofacebook-github-bot
authored andcommitted
Fix Dynamic Import Path Resolution for Internal Utils
Summary: ## Problem * Dynamic import of `./utils/fb/internal_utils` failed with 404 errors due to incorrect path resolution * `checkFbDirectoryExists()` returned `true` but subsequent import failed because of double `utils` in resolved URL ## Root Causes 1. **Path Context Issue**: `safeImport.ts` in `utils/` directory resolved `./utils/fb/internal_utils` as `utils/utils/fb/internal_utils` 2. **Detection Logic Issue**: Checked non-existent `fb/exists` file, got HTML fallback instead of actual file ## Changes Made ------------ ### 1. Fixed `safeImport.ts` `// Added path adjustment for utils/ context let adjustedPath = modulePath; if (modulePath.startsWith('./utils/')) { adjustedPath = './' + modulePath.substring('./utils/'.length); }` ### 2. Fixed `**fbDetection.ts**` ``` // Check actual file instead of non-existent marker - const response = await fetch('./fb/exists', { method: 'HEAD' }); + const response = await fetch('./fb/internal_utils.ts', { method: 'HEAD' }); // Detect HTML fallback vs real file const contentType = response.headers.get('content-type') || ''; const isHtmlFallback = contentType.includes('text/html'); return response.ok && !isHtmlFallback; ``` Reviewed By: Sibylau Differential Revision: D77977718 fbshipit-source-id: 8b017a0af417e8bdc27ddc997b665cdd928cb0a5
1 parent 6ef4072 commit 4fc3a64

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

website/src/utils/fbDetection.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
/**
22
* Utility to detect if the fb directory exists
3-
* This checks for the existence of the exists marker file in the fb directory
3+
* This checks for the existence of the internal_utils file in the fb directory
44
*/
55

66
export const checkFbDirectoryExists = async (): Promise<boolean> => {
77
try {
8-
// Try to fetch the exists marker file in the fb directory
9-
const response = await fetch('/fb/exists', { method: 'HEAD' });
10-
return response.ok;
8+
// Try to fetch the actual internal_utils file in the fb directory
9+
const response = await fetch('./fb/internal_utils.ts', { method: 'HEAD' });
10+
11+
// Check if we got a successful response AND it's not HTML content
12+
// Vite dev server returns 200 + HTML (index.html) for missing files as SPA fallback
13+
const contentType = response.headers.get('content-type') || '';
14+
const isHtmlFallback = contentType.includes('text/html');
15+
16+
// Only consider it successful if we get 200 AND it's not HTML fallback
17+
return response.ok && !isHtmlFallback;
1118
} catch (error) {
1219
// If the request fails, the directory likely doesn't exist
1320
return false;
1421
}
15-
};
22+
};

website/src/utils/safeImport.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
/**
22
* Safe dynamic import utility that handles missing modules gracefully
3-
* This avoids build-time module resolution issues
3+
* This avoids build-time module resolution issues for OSS/internal builds
44
*/
55

66
/**
77
* Safely imports a module by path, returning null if it doesn't exist
8-
* Uses Function constructor to avoid static analysis
8+
* Uses string concatenation to bypass static analysis while maintaining proper path resolution
99
*/
1010
export async function safeImport(modulePath: string): Promise<any | null> {
1111
try {
12-
// Use Function constructor to create dynamic import
13-
// This completely bypasses static analysis
14-
const dynamicImport = new Function('path', 'return import(path)');
15-
const module = await dynamicImport(modulePath);
12+
// Adjust path to account for safeImport.ts being in the utils directory
13+
// When called from App.tsx with './utils/fb/internal_utils',
14+
// we need to convert it to './fb/internal_utils' since we're already in utils/
15+
let adjustedPath = modulePath;
16+
if (modulePath.startsWith('./utils/')) {
17+
adjustedPath = './' + modulePath.substring('./utils/'.length);
18+
}
19+
20+
// Use string concatenation to create dynamic import path
21+
// This bypasses static analysis while maintaining proper module resolution
22+
const dynamicPath = '' + adjustedPath;
23+
const module = await import(/* @vite-ignore */dynamicPath);
1624
return module;
1725
} catch (error) {
1826
console.warn(`Module ${modulePath} not available:`, error);
1927
return null;
2028
}
21-
}
29+
}

0 commit comments

Comments
 (0)