Using Tuya CDN Resources
Version Requirements
- Base library version
>=2.26.0 - @ray-js/ray version
>=1.6.10 - Developer tools version
>=0.10.0 - Cannot be used together with third-party CDN feature
Feature Description
To reduce the mini program package size, the mini program provides CDN resource functionality. Developers can upload static resources (such as images, audio, etc.) in the project to the CDN, which will be automatically replaced with the corresponding CDN address at runtime.
Configure CDN
Project Configuration
Declare the storage directory for the CDN configuration file in project.tuya.json.
Create a new cdn directory in the project root directory with the following structure:
Upload to CDN via Developer Tools
After uploading files in the developer tools, a cdnImage.json file will be automatically generated in the cdn directory, for example:
Usage Limits
- Resources are first pushed to the China region, then synchronized to other regions
- Currently supports storing up to 100MB of static resources
Feature Description
Upload files:
- IDE upload: Automatically generates Key-Value pairs and writes them to
cdnImage.json. To modify the key, manually edit the file - Platform upload: Manually add the corresponding Key-Value mapping in
cdnImage.json
Delete files:
- First delete the corresponding key-value in
cdnImage.json - Go to Mini Program Developer Platform to delete the file and free up quota
Manual Management Examples
Example 1: Upload Files via IDE
Steps:
- Click the "Upload CDN File" button in the IDE and select the local image file
logo.png - The IDE automatically generates the
cdnImage.jsonfile with the following content:
- Use in code:
Example 2: Upload Files via Developer Platform
Steps:
- Go to Mini Program Developer Platform to upload image files
- After successful upload, click the "Copy" button to get the CDN path:
For example, the copied link is:
-
If there is no local
cdnImage.jsonfile, initialize it firstClick the "Initialize cdnImage.json file" button in the IDE:
-
Manually add the mapping in the project's
cdnImage.json:
- Use in code:
Example 3: Delete or Replace CDN Files
Scenario 1: Completely Delete Files
Assuming you no longer need banner.png:
- First delete the corresponding record in
cdnImage.json:
-
Go to Mini Program Developer Platform to delete the corresponding CDN file and free up storage quota
-
Ensure that
banner.pngis no longer referenced in the code to avoid image loading failures
Scenario 2: Replace Files
Assuming you need to update banner.png with a new image:
-
Go to Mini Program Developer Platform to upload the new image
-
Delete the old CDN file (to free up quota)
-
Update the CDN path in
cdnImage.json:
- The code reference remains unchanged, and the image will automatically use the new CDN address:
Using in Projects
Basic Usage
Ray provides the getCdnUrl method, which automatically replaces images with the corresponding CDN address based on the current region.
Step 1: Import dependencies
Step 2: Get CDN address
Step 3: Use in components
Complete Example
Assuming the cdnImage.json content is as follows:
Use in React components:
Notes
- It is recommended to pre-process URLs outside the component or in
useMemoto avoid calling on every render - CDN addresses will automatically select the nearest node based on the user's region, improving loading speed
FAQ
Q1: Is the 100MB quota per project or shared across all projects?
A: The 100MB is the total quota for all mini program projects within the current workspace, not per project.
- Multiple mini program projects within the same workspace share this 100MB of storage space
- If there is workspace authorization, the quota of the parent account (workspace owner) applies
- For example: If A authorizes B, when B operates in A's workspace, A's CDN quota is used
Q2: How to free up CDN quota?
A: You must go to the Mini Program Developer Platform to delete files to free up quota. Simply deleting the cdn/cdnImage.json file or its entries in your project will NOT free up CDN quota.
Correct deletion process:
- First delete the corresponding key-value in the project's
cdnImage.json(to avoid broken references in code) - Go to the developer platform to delete the corresponding CDN file (to free up quota)
Q3: What if I run out of quota?
A:
- Go to the developer platform to view uploaded files and delete unused resources to free up quota
- Optimize image sizes before uploading, use compression tools (such as TinyPNG) to reduce file size
Q4: What if I accidentally deleted the cdnImage.json file?
A: cdnImage.json is only a local mapping file. After deletion:
- CDN files on the developer platform still exist, and quota is not freed
- You can click the "Initialize cdnImage.json file" button in the IDE to generate an empty template
- Go to the developer platform to view uploaded files and manually rebuild the mapping