Skip to content
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

Support Functions in non-Pages environments #213

Merged
merged 3 commits into from
Jan 8, 2022

Conversation

GregBrimble
Copy link
Member

Adds support for building a Worker from a folder of functions which isn't tied to the Pages platform.

This lets developers use the same file-based routing system an simplified syntax when developing their own Workers!


For example, I can create a folder of functions:

// functions/hello.ts

export const onRequest = () => {
  return new Response("Hello, world!")
}

And run npx wrangler pages functions build --fallback-service='' --script-path=worker.js.

This will generate a Worker worker.js which will respond with "Hello, world!" for requests coming to /hello.

The --fallback-service determines the handling of requests which either don't match, or hit the end of their next chain. In Pages world, we fallback to env.ASSETS.fetch which serves a project's static assets.

In this case, when I pass --fallback-service='', I'm signally that I don't want to fallback to any service, and instead, I fallback to the global fetch function. So if this Worker was deployed on my zone, when I make a request to anything other than /hello, I'd hit my origin.

If I wanted to fallback to another service or Durable Object, I could specify --fallback-service MY_SERVICE.

Middleware, nested/routes, [segmented routes] are all supported with this.

@changeset-bot
Copy link

changeset-bot bot commented Jan 7, 2022

🦋 Changeset detected

Latest commit: 618292c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning on exposing this for regular developers, or is this just for internal usage?

packages/wrangler/pages/functions/template-worker.ts Outdated Show resolved Hide resolved
packages/wrangler/package.json Show resolved Hide resolved
@@ -1,11 +1,13 @@
import path from "path";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it's a good time to make a build of this file too. I'd like to keep packages out of dependencies, and would rather deliver bundled files. In a followup PR is fine, but before GA.

@GregBrimble
Copy link
Member Author

Regular developers, yeah. There's a few other things I want to do before its ready for prime-time, but the hope is that some people chose to write Workers with this file-based routing.

@GregBrimble GregBrimble force-pushed the support-functions-in-non-pages-environments branch from 23393cf to 618292c Compare January 8, 2022 00:02
@threepointone
Copy link
Contributor

That package lock thing is a little worrisome, but maybe it's fine now. I'll test myself tomorrow. Feel free to land.

@GregBrimble
Copy link
Member Author

Regenerated it properly in the latest commit. The changes now are just due to updated packages because we're not pinning versions.

@GregBrimble GregBrimble merged commit 5e1222a into main Jan 8, 2022
@GregBrimble GregBrimble deleted the support-functions-in-non-pages-environments branch January 8, 2022 00:26
@github-actions github-actions bot mentioned this pull request Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants