This is the Cloudflare deployment for twikoo comment system. Compared to other deployments like Vercel/Netlify + MongoDB, it greatly improved the cold start latency (6s
-> <0.5s
). The latency improvement largely comes from tremendous optimizations on Cloudflare workers as well as integrated environment between HTTP server and database (Cloudflare D1).
npm install
echo "" > node_modules/jsdom/lib/api.js
echo "" > node_modules/tencentcloud-sdk-nodejs/tencentcloud/index.js
echo "" > node_modules/nodemailer/lib/nodemailer.js
npx wrangler login
npx wrangler d1 create twikoo
database_name
and database_id
from the output of the previous step, and paste them into wrangler.toml
file, replacing the original values.npx wrangler d1 execute twikoo --remote --file=./schema.sql
npx wrangler deploy --minify
https://twikoo-cloudflare.<your user name>.workers.dev
in the commandline. You can visit the address. If everything is set up perfectly, you're expected to see a line like that in your browser:
{"code":100,"message":"Twikoo 云函数运行正常,请参考 https://twikoo.js.org/frontend.html 完成前端的配置","version":"1.6.33"}
https://
prefix) should be used as the envId
field in twikoo.init
.Because Cloudflare workers are only partially compatible with Node.js, there are certain functional limitations for the twikoo Cloudflare deployment due to compatibility issues:
process.env.XXX
) can't be used to control the behavior of the app.@imaegoo/node-ip2region
package).dompurify
can't be used to sanitize the comments due to compatibility issue of jsdom
package. Instead, we're using xss
package for XSS sanitization./some/path/
and /some/path
. This is because it's not easy to write a Cloudflare D1 SQL query to unify these 2 kinds of paths. If your website can have paths with and without the trailing /
for the same page, you can explicitly set the path
field in twikoo.init
.pushoo.js
doesn't work as pushoo.js
depends on axio
which needs NodeJS runtime.Because of the compatibility issues of nodemailer
package, the email integration via SMTP for sending notifications won't work directly. Instead, in this worker, we support email notifications via SendGrid's HTTPS API. To enable the email integration via SendGrid, you can follow the steps below:
SENDER_EMAIL
: The email address of the sender. Needs to verify it in SendGrid.SENDER_NAME
: The name shown as the sender.SMTP_SERVICE
: SendGrid
.SMTP_USER
: Provide some non-empty value.SMTP_PASS
: The API key.Send test email
button to make sure the integration works well.If you encounter any issues, or have any questions for this deployment, you can send an email to tao@vanjs.org.