Skip to content

Commit 382a528

Browse files
authored
Merge pull request #18 from uniformdev/feature/auto-release-5.1.210922-1
Release 5.1
2 parents 77771f7 + 2ffda65 commit 382a528

11 files changed

+1328
-4616
lines changed

README.md

3.29 KB

TL;DR version

  1. Configure .env file according to your environment specifics (see .env-example file).
  2. npm install
  3. Add NPM_TOKEN environment variable with the value we provided you with.
  4. npm run start to start the SSR server.
  5. npm run export:deploynpm run export to run static export if used only with the Uniform DeployDeploy-only capability.
  6. npm run export:deploy-and-optimize to run static export if used with the Optimize capability.
  7. npm run export:esi to run static export if used with the Uniform Optimize capability.

Cloudflare worker setup

  1. Install @cloudflare/wrangler npm package
    npm i @cloudflare/wrangler@1.19.2 -g

  2. Create a Cloudflare account: https://dash.cloudflare.com/login

  3. Create a Cloudflare API token:

    • Follow the link: https://dash.cloudflare.com/profile/api-tokens
    • Select "Create Token" button
    • Select "Edit Cloudflare Workers" among API token templates
    • In a new "Create Token" window don't change any Permissions (they are predefined correctly); indicate "All Accounts" in Account Resources section and "All Zones" in Zone Resources section. Client IP Address Filtering section can be skipped.
    • Press "Continue To Summary" and then "Create Token" buttons.
    • IMPORTANT! Copy and save your API Token somewhere. It only shown once after the initial setup.
    • Finalize the worker setup: navigate to the Workers page (Click Workers link on the right pane on the Cloudflare main page) and click the Setup button next to your worker name; Choose to proceed with free account on the next page
  4. Enable config: uniform-mvc-kit.Uniform.Deployment.Hosted.z.Cloudflare.config.disabled and specify required variables:

    • update CF_ACCOUNT_ID with your Cloudflare account ID
    • update CF_API_TOKEN with created Cloudflare API token
    • update CF_WORKER_NAME with preferable worker name
  5. If incremental deploy configured: enable config: uniform-mvc-kit.Uniform.Deployment.Incremental.z.Cloudflare.config.disabled and specify required variables:

    • update PublicUrl with your Cloudflare public url
  6. Default Cloudflare worker domain: https://<WORKER-NAME>.<CLOUDFLARE-ACCOUNT-NAME>.workers.dev

cloudflare-worker/worker.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
2+
3+
import { rewriteEsiResponse, createEsiContext, isEsiEnabledForRequest } from '@uniformdev/esi-edge-cloudflare';
4+
5+
const DEBUG = (_DEBUG === '1');
6+
const SHOW_ERRORS = (_SHOW_ERRORS === '1');
7+
const WAIT_FOR_RESPONSE = (_WAIT_FOR_RESPONSE === '1');
8+
9+
addEventListener('fetch', event => {
10+
try {
11+
event.respondWith(handleEvent(event))
12+
} catch (e) {
13+
if (SHOW_ERRORS) {
14+
return event.respondWith(
15+
new Response(e.message || e.toString(), {
16+
status: 500,
17+
}),
18+
)
19+
}
20+
event.respondWith(new Response('Internal Error', { status: 500 }))
21+
}
22+
})
23+
24+
const staticKVOptions = {
25+
cacheControl : {
26+
browserTTL: 7 * 60 * 60 * 24, // 7 days
27+
edgeTTL: 7 * 60 * 60 * 24, // 7 days
28+
bypassCache: false,
29+
}
30+
};
31+
32+
const esiKVOptions = {
33+
cacheControl : {
34+
bypassCache: true,
35+
}
36+
};
37+
38+
async function handleEvent(event) {
39+
try {
40+
// `event.request.url` can be used to determine response with ESI instead of `Content-Type`
41+
const esiEnabled = isEsiEnabledForRequest(event.request);
42+
const response = await getKVResponse(event, esiEnabled);
43+
44+
// do not process content without ESI
45+
if (!esiEnabled) {
46+
return response;
47+
}
48+
49+
//
50+
//START: Uniform code
51+
//
52+
const esiContext = createEsiContext(event.request, {debug: DEBUG});
53+
const rewritedResponse = rewriteEsiResponse(esiContext, response,{debug: DEBUG});
54+
//
55+
//END: Uniform code
56+
//
57+
58+
if (WAIT_FOR_RESPONSE) {
59+
// read the stream to the end to catch errors
60+
const blob = await rewritedResponse.blob();
61+
return new Response(blob, rewritedResponse);
62+
}
63+
64+
return rewritedResponse;
65+
66+
} catch (e) {
67+
if (!SHOW_ERRORS) {
68+
try {
69+
let notFoundResponse = await getAssetFromKV(event, {
70+
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req),
71+
})
72+
73+
return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 })
74+
} catch (e) { }
75+
}
76+
77+
return new Response(e.message || e.toString(), { status: 500 })
78+
}
79+
}
80+
81+
async function getKVResponse(event, esiEnabled) {
82+
if (!esiEnabled) {
83+
return await getAssetFromKV(event, staticKVOptions);
84+
}
85+
86+
// HTML with ESI must not be cached
87+
// cacheControl : {bypassCache: true}
88+
// Otherwise page can return 304 response code
89+
return await getAssetFromKV(event, esiKVOptions);
90+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//const path = require('path')
2+
3+
module.exports = {
4+
context: __dirname,
5+
target: "webworker",
6+
entry: {
7+
worker: './worker.js'
8+
},
9+
mode: "production",
10+
optimization: {
11+
usedExports: true,
12+
},
13+
node: {
14+
// global: false,
15+
// __filename: false,
16+
// __dirname: false,
17+
fs: "empty",
18+
net: "empty",
19+
tls: "empty",
20+
},
21+
}

cloudflare-worker/wrangler.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
account_id = "__ACCOUNT_ID__"
2+
name = "__WORKER_NAME__"
3+
route = ""
4+
type = "webpack"
5+
webpack_config = "./wrangler-webpack.config.js"
6+
workers_dev = true
7+
zone_id = ""
8+
vars = { _DEBUG = "0", _SHOW_ERRORS = "1", _WAIT_FOR_RESPONSE = "0" }
9+
10+
[site]
11+
bucket = "../"
12+
entry-point = "./"
13+
# include everything except `.wrangler` folder
14+
include = ["!.wrangler/"]
15+
16+
[env.debug]
17+
vars = { _DEBUG = "1", _SHOW_ERRORS = "1", _WAIT_FOR_RESPONSE = "1" }
18+

0 commit comments

Comments
 (0)