Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add page functions proxy to make response faster #234

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/.env.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VITE_API_BASE=
VITE_CF_WEB_ANALY_TOKEN=
2 changes: 2 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ coverage
*.sw?

.env.*
!.env.example
!.env.pages
*-dist/
components.d.ts
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite",
"build": "vite build -m prod --emptyOutDir",
"build:release": "vite build -m example --emptyOutDir",
"build:pages": "vite build -m pages --emptyOutDir",
"preview": "vite preview",
"deploy:preview": "npm run build && wrangler pages deploy ./dist --branch preview",
"deploy": "npm run build && wrangler pages deploy ./dist --branch production"
Expand Down
34 changes: 34 additions & 0 deletions pages/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
.DS_Store
dist
dist-ssr
coverage
*.local

/cypress/videos/
/cypress/screenshots/

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

.env.*
*-dist/
components.d.ts
.wrangler/
pnpm-lock.yaml
9 changes: 9 additions & 0 deletions pages/functions/_middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const API_PATHS = ["/api/", "/open_api/", "/user_api/", "/admin/"]

export async function onRequest(context) {
const reqPath = new URL(context.request.url).pathname;
if (API_PATHS.map(path => reqPath.startsWith(path)).some(Boolean)) {
return context.env.BACKEND.fetch(context.request);
}
return await context.next();
}
16 changes: 16 additions & 0 deletions pages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "temp-email-pages",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "wrangler pages dev",
"deploy": "wrangler pages deploy --branch production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"wrangler": "^3.55.0"
}
}
8 changes: 8 additions & 0 deletions pages/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name = "temp-email-pages"
pages_build_output_dir = "../frontend/dist"
compatibility_date = "2024-05-13"

[[services]]
binding = "BACKEND"
service = "cloudflare_temp_email"
environment = "production"
22 changes: 22 additions & 0 deletions vitepress-docs/docs/zh/guide/cli/pages.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Cloudflare Pages 前端

::: warning
下面两种方式选择一种即可
:::

## 前后端分离部署

第一次部署会提示创建项目, `production` 分支请填写 `production`

```bash
Expand All @@ -23,3 +29,19 @@ pnpm run deploy
部署完成之后你可以在 Cloudflare 控制台看到你的项目, 可以为 `pages` 配置自定义域名

![pages](/readme_assets/pages.png)

## 通过 page functions 转发后端请求

从 page functions 转发请求到 worker 后端, 可以获取更快的响应速度

第一次部署会提示创建项目, `production` 分支请填写 `production`

如果你的 worker 后端 名称不为 `cloudflare_temp_email` 请修改 `pages/wrangler.toml`

```bash
cd frontend
pnpm install
pnpm build:pages
cd ../pages
pnpm run deploy
```