Skip to content
This repository was archived by the owner on Apr 12, 2023. It is now read-only.

Commit 2dbbecd

Browse files
authored
Workers deploy (#331)
1 parent fd08bd2 commit 2dbbecd

File tree

16 files changed

+264
-3
lines changed

16 files changed

+264
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ npm run now-deploy
111111
>
112112
> Fork or clone the example to your GitHub account. After adding your repo to Netlify you’ll get automatic builds & deploys when pushing to master. You can also add a [webhook](https://www.sanity.io/docs/webhooks) to get deploys on content changes.
113113
114+
115+
**Deploy on Cloudflare:** If you want to deploy the Gatsby site to Cloudflare we added a wrangler.toml and `workers-site/` to both studio and web.
116+
117+
* Follow quickstart for wrangler: https://developers.cloudflare.com/workers/quickstart
118+
* Edit wrangler.toml's according to where you'd like studio and web to get deployed to
119+
* Run `npm run worker-deploy`
120+
114121
## Contributing
115122

116123
1. [Fork it](https://https://github.com/sanity-io/example-company-website-gatsby-sanity-combo/fork)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"graphql-deploy": "lerna run graphql-deploy",
1616
"lint": "lerna run lint",
1717
"now-deploy": "now && now alias",
18+
"workers-start": "lerna run workers-start",
19+
"workers-deploy": "lerna run workers-deploy",
1820
"postinstall": "lerna bootstrap",
1921
"sanity-deploy": "lerna run sanity-deploy",
2022
"start": "lerna run empty-cache && lerna run start --parallel",

studio/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"start": "sanity start",
1616
"build": "sanity build",
1717
"now-build": "npm run build",
18+
"workers-start": "npm run build && wrangler preview --watch",
19+
"workers-deploy": "npm run build && wrangler publish",
1820
"now-dev": "npm run build",
1921
"test": "sanity check"
2022
},

studio/sanity.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"root": true,
33
"api": {
4-
"projectId": "7j9cnsfo",
4+
"projectId": "9nhae6b7",
55
"dataset": "production"
66
},
77
"project": {
@@ -26,4 +26,3 @@
2626
}
2727
]
2828
}
29-

studio/workers-site/.cargo-ok

Whitespace-only changes.

studio/workers-site/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
worker

studio/workers-site/index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
2+
3+
/**
4+
* The DEBUG flag will do two things that help during development:
5+
* 1. we will skip caching on the edge, which makes it easier to
6+
* debug.
7+
* 2. we will return an error message on exception in your Response rather
8+
* than the default 404.html page.
9+
*/
10+
const DEBUG = false
11+
12+
addEventListener('fetch', event => {
13+
try {
14+
event.respondWith(handleEvent(event))
15+
} catch (e) {
16+
if (DEBUG) {
17+
return event.respondWith(
18+
new Response(e.message || e.toString(), {
19+
status: 500,
20+
}),
21+
)
22+
}
23+
event.respondWith(new Response('Internal Error', { status: 500 }))
24+
}
25+
})
26+
27+
async function handleEvent(event) {
28+
const url = new URL(event.request.url)
29+
let options = {}
30+
31+
/**
32+
* You can add custom logic to how we fetch your assets
33+
* by configuring the function `mapRequestToAsset`
34+
*/
35+
// options.mapRequestToAsset = handlePrefix(/^\/docs/)
36+
37+
try {
38+
if (DEBUG) {
39+
// customize caching
40+
options.cacheControl = {
41+
bypassCache: true,
42+
}
43+
}
44+
return await getAssetFromKV(event, options)
45+
} catch (e) {
46+
// if an error is thrown try to serve the asset at 404.html
47+
if (!DEBUG) {
48+
try {
49+
let notFoundResponse = await getAssetFromKV(event, {
50+
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req),
51+
})
52+
53+
return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 })
54+
} catch (e) {}
55+
}
56+
57+
return new Response(e.message || e.toString(), { status: 500 })
58+
}
59+
}
60+
61+
/**
62+
* Here's one example of how to modify a request to
63+
* remove a specific prefix, in this case `/docs` from
64+
* the url. This can be useful if you are deploying to a
65+
* route on a zone, or if you only want your static content
66+
* to exist at a specific path.
67+
*/
68+
function handlePrefix(prefix) {
69+
return request => {
70+
// compute the default (e.g. / -> index.html)
71+
let defaultAssetKey = mapRequestToAsset(request)
72+
let url = new URL(defaultAssetKey.url)
73+
74+
// strip the prefix from the path for lookup
75+
url.pathname = url.pathname.replace(prefix, '/')
76+
77+
// inherit all other props from the default request
78+
return new Request(url.toString(), defaultAssetKey)
79+
}
80+
}

studio/workers-site/package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

studio/workers-site/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"private": true,
3+
"name": "worker",
4+
"version": "1.0.0",
5+
"description": "A template for kick starting a Cloudflare Workers project",
6+
"main": "index.js",
7+
"license": "MIT",
8+
"dependencies": {
9+
"@cloudflare/kv-asset-handler": "^0.0.9"
10+
}
11+
}

studio/wrangler.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name = "studio"
2+
type = "webpack"
3+
account_id = ""
4+
workers_dev = true
5+
route = ""
6+
zone_id = ""
7+
8+
[site]
9+
bucket = "dist"
10+
entry-point = "workers-site"

0 commit comments

Comments
 (0)