Skip to content

Commit

Permalink
Wrangler Init
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunmangukiya committed Sep 29, 2019
1 parent e76f2d3 commit 3f73ed9
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
my-nuxt-app
</h1>
<h2 class="subtitle">
My best Nuxt.js project
My best Nuxt.js project deployed on Cloudflare Workers
</h2>
<div class="links">
<a
Expand Down
Empty file added workers-site/.cargo-ok
Empty file.
2 changes: 2 additions & 0 deletions workers-site/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
worker
80 changes: 80 additions & 0 deletions workers-site/index.js
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)
}
}
21 changes: 21 additions & 0 deletions workers-site/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions workers-site/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"name": "worker",
"version": "1.0.0",
"description": "A template for kick starting a Cloudflare Workers project",
"main": "index.js",
"author": "Ashley Lewis <ashleymichal@gmail.com>",
"license": "MIT",
"dependencies": {
"@cloudflare/kv-asset-handler": "^0.0.5"
}
}
10 changes: 10 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
account_id = "823a7a9b352ff317b2a4ca9e22c96b8f"
name = "my-nuxt-app"
type = "webpack"
route = ""
workers_dev = true
zone_id = ""

[site]
bucket = "dist"
entry-point = "workers-site"

0 comments on commit 3f73ed9

Please sign in to comment.