generated from flotiq/gatsby-starter-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from flotiq/feature/cloudflare-workers
add deploy to Cloudflare Workers button
- Loading branch information
Showing
4 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Deploy Cloudflare Worker | ||
on: | ||
repository_dispatch: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: [ 18.x ] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js {{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: Check if Flotiq API key is set. | ||
env: | ||
GATSBY_FLOTIQ_API_KEY: ${{ secrets.GATSBY_FLOTIQ_API_KEY }} | ||
run: | | ||
if [ -z "$GATSBY_FLOTIQ_API_KEY" ]; then | ||
echo "Flotiq API Key is not set, using a starter one." && echo "GATSBY_FLOTIQ_API_KEY=9a7dc942c9451f67d86a4cac241453c8" >> $GITHUB_ENV | ||
else | ||
echo "Using user-provided Flotiq API Key. Importing starter data to Flotiq." && echo "GATSBY_FLOTIQ_API_KEY=$GATSBY_FLOTIQ_API_KEY" >> $GITHUB_ENV | ||
npm install -g flotiq-cli | ||
flotiq import . $GATSBY_FLOTIQ_API_KEY | ||
fi | ||
- run: npm install -g gatsby-cli | ||
- run: yarn install | ||
- run: gatsby build | ||
- name: Build & Deploy Worker | ||
uses: cloudflare/wrangler-action@v3 | ||
with: | ||
apiToken: ${{ secrets.CF_API_TOKEN }} | ||
accountId: ${{ secrets.CF_ACCOUNT_ID }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' | ||
|
||
/** | ||
* The DEBUG flag will do two things that help during development: | ||
* 1. we will skip caching on the edge, which makes it easier to | ||
* debug. | ||
* 2. we will return an error message on exception in your Response rather | ||
* than the default 404.html page. | ||
*/ | ||
const DEBUG = false | ||
|
||
addEventListener('fetch', event => { | ||
try { | ||
event.respondWith(handleEvent(event)) | ||
} catch (e) { | ||
if (DEBUG) { | ||
return event.respondWith( | ||
new Response(e.message || e.toString(), { | ||
status: 500, | ||
}), | ||
) | ||
} | ||
event.respondWith(new Response('Internal Error', { status: 500 })) | ||
} | ||
}) | ||
|
||
async function handleEvent(event) { | ||
const url = new URL(event.request.url) | ||
let options = {} | ||
|
||
/** | ||
* You can add custom logic to how we fetch your assets | ||
* by configuring the function `mapRequestToAsset` | ||
*/ | ||
// options.mapRequestToAsset = handlePrefix(/^\/docs/) | ||
|
||
try { | ||
if (DEBUG) { | ||
// customize caching | ||
options.cacheControl = { | ||
bypassCache: true, | ||
} | ||
} | ||
return await getAssetFromKV(event, options) | ||
} catch (e) { | ||
// if an error is thrown try to serve the asset at 404.html | ||
if (!DEBUG) { | ||
try { | ||
let notFoundResponse = await getAssetFromKV(event, { | ||
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req), | ||
}) | ||
|
||
return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 }) | ||
} catch (e) {} | ||
} | ||
|
||
return new Response(e.message || e.toString(), { status: 500 }) | ||
} | ||
} | ||
|
||
/** | ||
* Here's one example of how to modify a request to | ||
* remove a specific prefix, in this case `/docs` from | ||
* the url. This can be useful if you are deploying to a | ||
* route on a zone, or if you only want your static content | ||
* to exist at a specific path. | ||
*/ | ||
function handlePrefix(prefix) { | ||
return request => { | ||
// compute the default (e.g. / -> index.html) | ||
let defaultAssetKey = mapRequestToAsset(request) | ||
let url = new URL(defaultAssetKey.url) | ||
|
||
// strip the prefix from the path for lookup | ||
url.pathname = url.pathname.replace(prefix, '/') | ||
|
||
// inherit all other props from the default request | ||
return new Request(url.toString(), defaultAssetKey) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name = "flotiq-gatsby-shop-2" | ||
main = "worker-index.js" | ||
compatibility_date = "2024-03-27" | ||
workers_dev = true | ||
|
||
[site] | ||
bucket = "./public" |