-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Cloudflare Worker Adapter #717
Merged
Rich-Harris
merged 8 commits into
sveltejs:master
from
halfnelson:feature/cloudflare-worker-adapter-2
Mar 29, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4872210
Sandbox seems to be working
halfnelson f03179d
Merge remote-tracking branch 'upstream/master' into feature/cloudflar…
halfnelson e3803eb
integrate with svelte-kit-demo
halfnelson aff2f3a
Update demo site readme with cloudflare info
halfnelson a6018bb
keep linter happy
halfnelson c676183
Update packages/adapter-cloudflare-workers/README.md
halfnelson 69c1468
Remove redundant comment
halfnelson 0e59273
Update README.md
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
/node_modules | ||
/build | ||
/.svelte | ||
/.vercel_build_output | ||
/.vercel_build_output | ||
/workers-site |
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
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,10 @@ | ||
name = "svelte-kit-demo" | ||
type = "webpack" | ||
account_id = "f60df21486a4f0e5dbd85493882f1d53" | ||
workers_dev = true | ||
route = "" | ||
zone_id = "" | ||
|
||
[site] | ||
bucket = "./build" | ||
entry-point = "./workers-site" |
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,3 @@ | ||
.DS_Store | ||
node_modules | ||
target |
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 @@ | ||
# @sveltejs/adapter-cloudflare-workers |
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,27 @@ | ||
# adapter-cloudflare-workers | ||
|
||
SvelteKit adapter that creates a Cloudflare Workers site using a function for dynamic server rendering. | ||
|
||
This is very experimental; the adapter API isn't at all fleshed out, and things will definitely change. | ||
|
||
## Configuration | ||
|
||
This adapter expects to find a [wrangler.toml](https://developers.cloudflare.com/workers/platform/sites/configuration) file in the project root. It will determine where to write static assets and the worker based on the `site.bucket` and `site.entry-point` settings. | ||
|
||
Generate this file using `wrangler` from your project directory | ||
|
||
```sh | ||
$ wrangler init --site my-site-name | ||
``` | ||
|
||
Then configure your sites build directory in the config file: | ||
|
||
```toml | ||
[site] | ||
bucket = "./build" | ||
entry-point = "./workers-site" | ||
``` | ||
|
||
It's recommended that you add the `build` and `workers-site` folders (or whichever other folders you specify) to your `.gitignore`. | ||
|
||
More info on configuring a cloudflare worker site can be found [here](https://developers.cloudflare.com/workers/platform/sites/start-from-existing) |
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,9 @@ | ||
{ | ||
"private": true, | ||
"version": "0.0.1", | ||
"description": "Worker site generated by SvelteKit", | ||
"main": "index.js", | ||
"dependencies": { | ||
"@cloudflare/kv-asset-handler": "~0.0.11" | ||
} | ||
} |
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,70 @@ | ||
import { render } from './app.js'; // eslint-disable-line import/no-unresolved | ||
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; // eslint-disable-line import/no-unresolved | ||
|
||
// From https://developers.cloudflare.com/workers/examples/read-post | ||
async function readRequestBody(request) { | ||
const { headers } = request; | ||
const contentType = headers.get('content-type') || ''; | ||
if (contentType.includes('application/json')) { | ||
return JSON.stringify(await request.json()); | ||
} else if (contentType.includes('application/text')) { | ||
return await request.text(); | ||
} else if (contentType.includes('text/html')) { | ||
return await request.text(); | ||
} else if (contentType.includes('form')) { | ||
return await request.formData(); | ||
} else { | ||
const myBlob = await request.blob(); | ||
const objectURL = URL.createObjectURL(myBlob); | ||
return objectURL; | ||
} | ||
} | ||
|
||
addEventListener('fetch', (event) => { | ||
event.respondWith(handleEvent(event)); | ||
}); | ||
|
||
async function handleEvent(event) { | ||
//try static files first | ||
if (event.request.method == 'GET') { | ||
try { | ||
return await getAssetFromKV(event); | ||
} catch (e) { | ||
if (!(e instanceof NotFoundError)) { | ||
return new Response('Error loading static asset:' + (e.message || e.toString()), { | ||
status: 500 | ||
}); | ||
} | ||
} | ||
} | ||
|
||
//fall back to an app route | ||
const request = event.request; | ||
const request_url = new URL(request.url); | ||
|
||
try { | ||
const rendered = await render({ | ||
host: request_url.host, | ||
path: request_url.pathname, | ||
query: request_url.searchParams, | ||
body: request.body ? await readRequestBody(request) : null, | ||
headers: request.headers, | ||
method: request.method | ||
}); | ||
|
||
if (rendered) { | ||
const response = new Response(rendered.body, { | ||
status: rendered.status, | ||
headers: rendered.headers | ||
}); | ||
return response; | ||
} | ||
} catch (e) { | ||
return new Response('Error rendering route:' + (e.message || e.toString()), { status: 500 }); | ||
} | ||
|
||
return new Response({ | ||
status: 404, | ||
statusText: 'Not Found' | ||
}); | ||
} |
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,68 @@ | ||
'use strict'; | ||
|
||
const { exec } = require('child_process'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const toml = require('toml'); | ||
|
||
module.exports = function () { | ||
/** @type {import('@sveltejs/kit').Adapter} */ | ||
const adapter = { | ||
name: '@sveltejs/adapter-cloudflare-workers', | ||
async adapt(builder) { | ||
let wrangler_config; | ||
|
||
if (fs.existsSync('wrangler.toml')) { | ||
try { | ||
wrangler_config = toml.parse(fs.readFileSync('wrangler.toml', 'utf-8')); | ||
} catch (err) { | ||
err.message = `Error parsing wrangler.toml: ${err.message}`; | ||
throw err; | ||
} | ||
} else { | ||
// TODO offer to create one? | ||
throw new Error( | ||
'Missing a wrangler.toml file. Consult https://developers.cloudflare.com/workers/platform/sites/configuration on how to setup your site' | ||
); | ||
} | ||
|
||
if (!wrangler_config.site || !wrangler_config.site.bucket) { | ||
throw new Error( | ||
'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration' | ||
); | ||
} | ||
|
||
const bucket = path.resolve(wrangler_config.site.bucket); | ||
const entrypoint = path.resolve(wrangler_config.site['entry-point'] ?? 'workers-site'); | ||
|
||
builder.copy_static_files(bucket); | ||
builder.copy_client_files(bucket); | ||
builder.copy_server_files(entrypoint); | ||
|
||
// copy the renderer | ||
builder.copy(path.resolve(__dirname, 'files/render.js'), `${entrypoint}/index.js`); | ||
builder.copy(path.resolve(__dirname, 'files/_package.json'), `${entrypoint}/package.json`); | ||
|
||
builder.log.info('Prerendering static pages...'); | ||
await builder.prerender({ | ||
dest: bucket | ||
}); | ||
|
||
builder.log.info('Installing Worker Dependencies...'); | ||
exec( | ||
'npm install', | ||
{ | ||
cwd: entrypoint | ||
}, | ||
(error, stdout, stderr) => { | ||
builder.log.info(stderr); | ||
if (error) { | ||
builder.log.error(error); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
|
||
return adapter; | ||
}; |
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,17 @@ | ||
{ | ||
"name": "@sveltejs/adapter-cloudflare-workers", | ||
"version": "0.0.1", | ||
"main": "index.js", | ||
"files": [ | ||
"files" | ||
], | ||
"scripts": { | ||
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format", | ||
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore", | ||
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore" | ||
}, | ||
"dependencies": { | ||
"toml": "^3.0.0", | ||
"@sveltejs/kit": "workspace:*" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any idea why the
eslint-disable-line
might be needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. I don't particularly want to bundle up kv-asset-handler as part of the adapter, and the adapt function itself doesn't have a dependency on it, so lint complains.