Overview
Chrome extensions must explicitly declare resources that web pages can access. CRXJS automatically handles this for content script dependencies, so you don’t need to manually configureweb_accessible_resources in most cases.
Automatic Resource Detection
When you import assets in content scripts, CRXJS automatically:- Detects imported files (images, fonts, CSS, etc.)
- Tracks all dependencies and dynamic imports
- Declares them as web accessible resources
- Associates them with the correct match patterns
src/content.ts
How It Works
Development Mode
During development, CRXJS makes all resources web accessible with<all_urls> to support hot module replacement:
From the source code (plugin-webAccessibleResources.ts:54):
Production Mode
In production, CRXJS analyzes the Vite build manifest to determine exact dependencies: From the source code (plugin-webAccessibleResources.ts:151):Manual Configuration
For resources not imported by content scripts, manually declare them:manifest.json
Dynamic Content Scripts
For dynamically injected scripts, use thedefineDynamicResource helper:
manifest.config.ts
<dynamic_resource> placeholder is replaced with actual imported files during the build.
Resource Combination
CRXJS combines redundant resources to minimize manifest size: From the source code (plugin-webAccessibleResources.ts:214):use_dynamic_url settings are merged into a single entry.
Match Pattern Origins
CRXJS simplifies match patterns to origins for loader-based content scripts: From the source code (plugin-webAccessibleResources.ts:184):https://example.com/path/*→https://example.com/*https://*.github.com/user/repo→https://*.github.com/*
Source Maps
CRXJS automatically includes source map files as web accessible resources: From the source code (plugin-webAccessibleResources.ts:237):CSS Injection
By default, CRXJS injects content script CSS automatically. To disable automatic injection:vite.config.ts
injectCss: false, CSS files are made web accessible but not automatically injected.
Firefox Compatibility
Firefox doesn’t supportuse_dynamic_url. CRXJS removes it automatically:
From the source code (plugin-webAccessibleResources.ts:226):
Extension IDs
To restrict resources to specific extensions, useextension_ids:
manifest.json
Security Considerations
Best Practices
- Use specific match patterns instead of
<all_urls>in production - Minimize exposed resources by only importing what you need
- Use
use_dynamic_urlfor sensitive resources to make URLs unpredictable - Validate origins when loading resources from content scripts
src/content.ts
Debugging
Inspect web accessible resources in the built extension:- Build your extension:
npm run build - Open
dist/manifest.json - Check the
web_accessible_resourcesarray
dist/manifest.json
Related
Content Scripts
Import assets in content scripts
Manifest
Configure web accessible resources