banner
dgtea

冬瓜blog

希望与未来同行

Foolproof hexo blog setup method (CF Workers) that anyone can learn

Cloudflare has updated their wrangler 2, causing some tutorials to become invalid. Here, I will write about the pitfalls I encountered.
The following content is available on the official software source of Ubuntu 22.04.

1. Configure Hexo#

Configuring Hexo is very simple, just follow the official tutorial.

1. Configure Node.js/Git#

sudo apt install git-core
sudo apt install nodejs

2. Install Hexo#

npm install hexo-cli -g
You may encounter errors, execute the following commands:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
npm install -g jshint
After completion, reinstall Hexo.
After the installation is complete, execute:
echo 'PATH="$PATH:./node_modules/.bin"' >> ~/.profile
to enable global use of Hexo.

2. Configure Wrangler#

Wrangler is quite difficult to deal with, and I still haven't figured out how to use Wrangler 2.
So, install Wrangler 1.
npm i @cloudflare/wrangler -g
Configure Wrangler, you will need a valid Cloudflare account with a bound domain name (optional) for this.
First, create a Workers, name it "blog" (or any other name, referred to as "blog" in the following text).
Go to Cloudflare Dashboard, click on the top right corner - My Profile - API Tokens.
Choose to create a token, use the template "Edit Cloudflare Workers", select "Your ID Cloudflare" for Account Resources, select "Your Domain Name (if any)" for Zone Resources, TTL can be any value.
After confirmation, a Token will appear, this token will only appear once.
Execute the command:
wrangler config
Enter your Token, there should be no errors.

3. Configure the Project#

Create a new directory, this directory will be your blog's working folder.
mkdir ur_dir
cd ur_dir
Initialize each tool:
hexo init
wrangler init
Hexo's configuration file is located at _config.yml, please refer to the Hexo official documentation for modifications.
Wrangler's configuration file is located at wrangler.toml, please refer to the following configuration for modifications as needed:

name = "Your workers name"
type = "webpack"
route = 'Your domain name (including workers.dev)/*'
zone_id = ''
usage_model = ''
compatibility_flags = []
workers_dev = true
compatibility_date = "2022-12-13"

[site]
bucket = "./public"
entry-point = "workers-site"

Save the changes.

4. Writing and Uploading#

For changing themes, configurations, and creating new articles in Hexo, please refer to the Hexo official documentation.
After finishing writing your article, proceed with the upload:
hexo g
wrangler publish
You can also use a local server for preview:
hexo server

5. Issues and Pitfalls#

1. wrangler command not found#

This is a variable issue, simply add npx before wrangler.

2. wrangler upload failure#

Cloudflare may have temporary issues, try using a VPN or wait for a while.

3. Domain cannot be accessed#

If your Workers is not bound to your domain, go to your Workers - Custom Domain and add your domain name, including both www and non-www versions.
If everything is normal, your DNS resolution should have two additional Workers resolutions.

6. Tips and Tricks#

I usually combine hexo g and wrangler publish into one shell script for convenience.
Here it is:

#!/bin/bash
hexo g
npx wrangler publish

After saving, add executable permissions to the script, and you can generate and upload with just one script.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.