Preface
Since the Hexo era, my Waline has been hosted on Vercel, and the data storage is done using LeanCloud International Edition. This is also the most recommended deployment method by the official Waline. Although simple, over time Waline's speed will inevitably be limited by LeanCloud. Previously, Teacher Du commented on my blog post "Talking about Interpersonal Communication" saying that my Waline loading speed was too slow.
Later, when I was browsing articles and replying to comments, I also noticed some issues with the loading speed. So I decided to switch databases and migrate from LeanCloud to optimize the performance of the comment section.
Process
Configuring the Database and Waline
First, I created an Azure free M0 database on MongoDB Cloud located in the Hong Kong region, with a storage space of 512 MB, which is more than enough for Waline. If it's not enough, it can be migrated to the free 5 GB TiDB in the future.
After creating it, allow access requests from all IP addresses on the "Network Access" page, and then copy the SRV format connection string. Use the connection string to connect to MongoDB in Navicat, and then create a database named waline
. Remember the name for later use.
Open the Waline project deployed on Vercel, go to the environment variable settings, delete the existing LeanCloud-related configuration items, and then fill in the MongoDB connection information according to the Waline documentation. One thing to note here is that MongoDB Cloud's M0 database does not have cluster information. If an incorrect MONGO_REPLICASET
is set, Waline will encounter a 500
connection timeout error when retrieving comment information. I got stuck here during the migration and later found out about this issue from an issue in the Waline repository.
The multi-machine connection information can be found here:
Select
Drivers
as the connection method,Driver
as Node.js,Version
as2.2.12 or later
The part highlighted by the yellow marker is the multi-machine connection information. Do not include the port number and fill it into the environment variables in the format specified in the Waline documentation.
:::warning Note
The region of your Vercel project also needs to be set to the same region as the database or as close as possible, otherwise the speed will be greatly reduced!
Database region
Project region
:::
Backup and Restore Data
After configuring, go to the Waline management backend "Import/Export" page to export all data. If the exported JSON file is not automatically downloaded by the browser after a while, it is likely because the serverless function of Vercel timed out. In this case, try exporting again and it should work.
The exported file is a standard JSON file that contains all the comments and registered user information. Keep it and do not delete it. Then redeploy Waline on Vercel. After the deployment is complete, access the management page, register a new account or use OAuth to register. After logging in, perform the import operation again on the "Import/Export" page, and select the JSON file that contains all the Waline data exported earlier. Waline will automatically start the import and index creation process.
Here, it is recommended to open the browser's DevTools and switch to the "Network" panel. This can help determine if the Waline import operation is proceeding normally. If the import suddenly stops and the progress indicator on the management page disappears before completion, it is likely that the serverless function of Vercel timed out (each Vercel serverless function in the Hobby plan can only run for a maximum of 10 seconds, otherwise it will time out and return a 504
error). In this case, no matter what, just perform the import operation again and it should work.
I did not encounter any import failures, so I can only provide this solution.
After the import is complete, everything should be back to normal. Log out of the comment section, and log in using the account and password used when Waline was using LeanCloud as the database. Then browse through each blog post, and you will notice a significant improvement in the loading speed of the comments. Test the comments on the demo page, and you will also notice a significant improvement compared to LeanCloud.
Other Optimizations
MongoDB has an "index" feature that can speed up the response time of the database when reading large collections to some extent. Therefore, setting indexes for some large collections can improve the response speed of Waline.
These large collections are usually page views and comment information (Counter
and Comment
collections), so setting indexes for them is sufficient.
References
Hexo 去 LeanCloud 依赖 | Finisky Garden
Pitfalls
Later, I found that almost all the green checkmarks indicating verified users were gone, which was quite annoying.
Then I checked the database and found that almost all comments had an empty user_id
field. When I opened the User
table for comparison, I realized that the user_id
field here is actually the id
at the beginning of each data in MongoDB. Now, I had to set the user information field for the previous comments again.
Because I don't have much experience in writing scripts, I did all these operations manually in Navicat. It was quite tiring...