- 
                Notifications
    You must be signed in to change notification settings 
- Fork 57
feat: add sveltekit support #131
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
          
     Open
      
      
            adriandlam
  wants to merge
  61
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feat/add-sveltekit-support
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +1,531
        
        
          −39
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            61 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      4153e42
              
                add workbench sveltekit
              
              
                adriandlam ff11f59
              
                add sveltekit package
              
              
                adriandlam 49d696c
              
                fix: use js for generated server routes in svelte
              
              
                adriandlam d90e965
              
                add test routes for sveltekit workbench
              
              
                adriandlam 777db32
              
                updates from debugging
              
              
                ijjk aa53572
              
                feat: add sveltekit build target
              
              
                adriandlam 99a03c3
              
                update user sign up workflow workbench sveltekit
              
              
                adriandlam f436c92
              
                fix: base builder missing GET route handler for sveltekit
              
              
                adriandlam 5d844a6
              
                feat: add vercel builder and check for vercel env
              
              
                adriandlam 1438ac6
              
                refactor: workflowPlugin for sveltekit
              
              
                adriandlam cd161c6
              
                ci: add tests to workbench sveltekit
              
              
                adriandlam 919ce3b
              
                fix: no start command and vercel builder outputs
              
              
                adriandlam 3f6b249
              
                fix: base builder conditions
              
              
                adriandlam d4ca09f
              
                .
              
              
                adriandlam 8c0cad1
              
                add auto adapter sveltekit
              
              
                adriandlam 3da001e
              
                fix: disable checking origin for cross site ci stuff
              
              
                adriandlam 3ac85f2
              
                allow all trusted origins in svelte config
              
              
                adriandlam 03997dd
              
                fix: add catch for error thrown when waiting for ops
              
              
                adriandlam 00d487d
              
                fix: sveltekit workbench adapter
              
              
                adriandlam 92787ef
              
                test
              
              
                adriandlam 7febb8c
              
                test
              
              
                adriandlam 2243e0c
              
                refactor: move vercel builder on config resolved
              
              
                adriandlam ac4000e
              
                update package json
              
              
                adriandlam b0ecab6
              
                add demo workflow trigger
              
              
                adriandlam 4f1fc09
              
                test letting sveltekit handle route generation
              
              
                adriandlam 289bc3e
              
                fix: exclude git ignore for local builder outputs
              
              
                adriandlam ec35feb
              
                update config
              
              
                ijjk 4bd162b
              
                add debug logs
              
              
                ijjk 9b10ed4
              
                update debug
              
              
                ijjk f3a71ce
              
                update turbo.json
              
              
                ijjk e45e258
              
                fix env check
              
              
                ijjk d935d87
              
                another output
              
              
                ijjk 9003f84
              
                use top-level await
              
              
                ijjk c8915d9
              
                ensure vercel functions are patched
              
              
                ijjk 10a564b
              
                change hook
              
              
                ijjk 7fa2da9
              
                update
              
              
                ijjk 26e1ac5
              
                fix spread
              
              
                ijjk 35ee459
              
                debug
              
              
                ijjk d0a38b2
              
                update
              
              
                ijjk 22ed0ca
              
                chore: remove vercel builder
              
              
                adriandlam 65c9cb5
              
                chore: update comments in sveltekit workflow package
              
              
                adriandlam 85b2e12
              
                chore: remove console comment
              
              
                adriandlam ca5a5d2
              
                refactor: workflow svelte plugin
              
              
                adriandlam e54ec44
              
                refactor: simplify local builder config
              
              
                adriandlam a75e298
              
                chore: remove unused deps
              
              
                adriandlam 4b808af
              
                chore: add comment for workbench svelte app
              
              
                adriandlam 5c23668
              
                chore: add sveltekit export to workflow
              
              
                adriandlam fe82169
              
                export sveltekit from workflow
              
              
                adriandlam 0c9471f
              
                lockfile
              
              
                adriandlam ae6c230
              
                chore: remove unused deps svelte workbench
              
              
                adriandlam 38a2b7c
              
                update styling
              
              
                adriandlam c49cac1
              
                add sveltekit getting started docs
              
              
                adriandlam 53ef3f9
              
                update sveltekit docs getting started
              
              
                adriandlam 479ce93
              
                changeset
              
              
                adriandlam 1729fe5
              
                Apply suggestion from @vercel[bot]
              
              
                adriandlam 2e8c7f5
              
                use proper env var
              
              
                adriandlam 604db69
              
                Update packages/sveltekit/src/index.ts
              
              
                adriandlam c073b97
              
                Update docs/content/docs/getting-started/sveltekit.mdx
              
              
                adriandlam 6fff1b7
              
                Document CSRF protection bypass in SvelteKit workbench config (#157)
              
              
                Copilot c894384
              
                refactor: deduplicate SvelteKit request conversion logic (#156)
              
              
                Copilot 1e0297d
              
                Update packages/core/e2e/local-build.test.ts
              
              
                adriandlam 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| --- | ||
| "@workflow/world-local": patch | ||
| "@workflow/sveltekit": patch | ||
| "workflow": patch | ||
| "@workflow/core": patch | ||
| "@workflow/cli": patch | ||
| --- | ||
|  | ||
| Add sveltekit workflow integration | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,258 @@ | ||
| --- | ||
| title: SvelteKit | ||
| --- | ||
|  | ||
| # SvelteKit | ||
|  | ||
| This guide will walk through setting up your first workflow in a SvelteKit app. Along the way, you'll learn more about the concepts that are fundamental to using the development kit in your own projects. | ||
|  | ||
| --- | ||
|  | ||
| <Steps> | ||
|  | ||
| <Step> | ||
| ## Create Your SvelteKit Project | ||
|  | ||
| Start by creating a new SvelteKit project. This command will create a new directory named `my-workflow-app` with a minimal setup and setup a SvelteKit project inside it. | ||
|  | ||
| ```bash | ||
| npx sv create my-workflow-app --template=minimal --types=ts --no-add-ons | ||
| ``` | ||
|  | ||
| Enter the newly made directory: | ||
|  | ||
| ```bash | ||
| cd my-workflow-app | ||
| ``` | ||
|  | ||
| ### Install `workflow` | ||
|  | ||
| <Tabs items={['npm', 'pnpm', 'yarn']}> | ||
| <Tab value="npm"> | ||
| <CodeBlock> | ||
| npm i workflow | ||
| </CodeBlock> | ||
| </Tab> | ||
| <Tab value="pnpm"> | ||
| <CodeBlock> | ||
| pnpm i workflow | ||
| </CodeBlock> | ||
| </Tab> | ||
| <Tab value="yarn"> | ||
| <CodeBlock> | ||
| yarn add workflow | ||
| </CodeBlock> | ||
| </Tab> | ||
| </Tabs> | ||
|  | ||
| ### Configure Vite | ||
|  | ||
| Add `workflowPlugin()` to your Vite config. This enables usage of the `"use workflow"` and `"use step"` directives. | ||
|  | ||
| ```typescript title="vite.config.ts" lineNumbers | ||
| import { sveltekit } from "@sveltejs/kit/vite"; | ||
| import { defineConfig } from "vite"; | ||
| import { workflowPlugin } from "workflow/sveltekit"; // [!code highlight] | ||
|  | ||
| export default defineConfig({ | ||
| plugins: [sveltekit(), workflowPlugin()], // [!code highlight] | ||
| }); | ||
| ``` | ||
|  | ||
| ### Update `package.json` | ||
|  | ||
| Update your `package.json` to include port `3000` for the development server: | ||
|  | ||
| ```json title="package.json" lineNumbers | ||
| { | ||
| // ... | ||
| "scripts": { | ||
| "dev": "vite dev --port 3000" | ||
| // ... | ||
| }, | ||
| } | ||
| ``` | ||
|  | ||
| <Accordion type="single" collapsible> | ||
| <AccordionItem value="typescript-intellisense" className="[&_h3]:my-0"> | ||
| <AccordionTrigger className="text-sm"> | ||
| ### Setup IntelliSense for TypeScript (Optional) | ||
| </AccordionTrigger> | ||
| <AccordionContent className="[&_p]:my-2"> | ||
|  | ||
| To enable helpful hints in your IDE, setup the workflow plugin in `tsconfig.json`: | ||
|  | ||
| ```json title="tsconfig.json" lineNumbers | ||
| { | ||
| "compilerOptions": { | ||
| // ... rest of your TypeScript config | ||
| "plugins": [ | ||
| { | ||
| "name": "workflow" // [!code highlight] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
|  | ||
| </AccordionContent> | ||
| </AccordionItem> | ||
| </Accordion> | ||
|  | ||
| </Step> | ||
|  | ||
| <Step> | ||
|  | ||
| ## Create Your First Workflow | ||
|  | ||
| Create a new file for our first workflow: | ||
|  | ||
| ```typescript title="workflows/user-signup.ts" lineNumbers | ||
| import { sleep } from "workflow"; | ||
|  | ||
| export async function handleUserSignup(email: string) { | ||
| "use workflow"; // [!code highlight] | ||
|  | ||
| const user = await createUser(email); | ||
| await sendWelcomeEmail(user); | ||
|  | ||
| await sleep("5s"); // Pause for 5s - doesn't consume any resources | ||
| await sendOnboardingEmail(user); | ||
|  | ||
| return { userId: user.id, status: "onboarded" }; | ||
| } | ||
|  | ||
| ``` | ||
|  | ||
| We'll fill in those functions next, but let's take a look at this code: | ||
|  | ||
| * We define a **workflow** function with the directive `"use workflow"`. Think of the workflow function as the _orchestrator_ of individual **steps**. | ||
| * The Workflow DevKit's `sleep` function allows us to suspend execution of the workflow without using up any resources. A sleep can be a few seconds, hours, days, or even months long. | ||
|  | ||
| ## Create Your Workflow Steps | ||
|  | ||
| Let's now define those missing functions. | ||
|  | ||
| ```typescript title="workflows/user-signup.ts" lineNumbers | ||
| import { FatalError } from "workflow" | ||
|  | ||
| // Our workflow function defined earlier | ||
|  | ||
| async function createUser(email: string) { | ||
| "use step"; // [!code highlight] | ||
|  | ||
| console.log(`Creating user with email: ${email}`); | ||
|  | ||
| // Full Node.js access - database calls, APIs, etc. | ||
| return { id: crypto.randomUUID(), email }; | ||
| } | ||
|  | ||
| async function sendWelcomeEmail(user: { id: string; email: string; }) { | ||
| "use step"; // [!code highlight] | ||
|  | ||
| console.log(`Sending welcome email to user: ${user.id}`); | ||
|  | ||
| if (Math.random() < 0.3) { | ||
| // By default, steps will be retried for unhandled errors | ||
| throw new Error("Retryable!"); | ||
| } | ||
| } | ||
|  | ||
| async function sendOnboardingEmail(user: { id: string; email: string}) { | ||
| "use step"; // [!code highlight] | ||
|  | ||
| if (!user.email.includes("@")) { | ||
| // To skip retrying, throw a FatalError instead | ||
| throw new FatalError("Invalid Email"); | ||
| } | ||
|  | ||
| console.log(`Sending onboarding email to user: ${user.id}`); | ||
| } | ||
| ``` | ||
|  | ||
| Taking a look at this code: | ||
|  | ||
| * Business logic lives inside **steps**. When a step is invoked inside a **workflow**, it gets enqueued to run on a separate request while the workflow is suspended, just like `sleep`. | ||
| * If a step throws an error, like in `sendWelcomeEmail`, the step will automatically be retried until it succeeds (or hits the step's max retry count). | ||
| * Steps can throw a `FatalError` if an error is intentional and should not be retried. | ||
|  | ||
| <Callout> | ||
| We'll dive deeper into workflows, steps, and other ways to suspend or handle events in [Foundations](/docs/foundations). | ||
| </Callout> | ||
|  | ||
| </Step> | ||
|  | ||
| <Step> | ||
|  | ||
| ## Create Your Route Handler | ||
|  | ||
| To invoke your new workflow, we'll have to add your workflow to a `POST` API route handler, `src/routes/api/signup/+server.ts` with the following code: | ||
|  | ||
| ```typescript title="src/routes/api/+server.ts" | ||
| import { start } from "workflow/api"; | ||
| import { handleUserSignup } from "../../../../workflows/user-signup"; | ||
| import { json, type RequestHandler } from "@sveltejs/kit"; | ||
|  | ||
| export const POST: RequestHandler = async ({ | ||
| request, | ||
| }: { | ||
| request: Request; | ||
| }) => { | ||
| const { email } = await request.json(); | ||
|  | ||
| // Executes asynchronously and doesn't block your app | ||
| await start(handleUserSignup, [email]); | ||
|  | ||
| return json({ message: "User signup workflow started" }); | ||
| }; | ||
|  | ||
| ``` | ||
|  | ||
| This route handler creates a `POST` request endpoint at `/api/signup` that will trigger your workflow. | ||
|  | ||
| <Callout> | ||
| Workflows can be triggered from API routes or any server-side code. | ||
| </Callout> | ||
|  | ||
| </Step> | ||
|  | ||
| </Steps> | ||
|  | ||
| ## Run in development | ||
|  | ||
| To start your development server, run the following command in your terminal in the SvelteKit root directory: | ||
|  | ||
| ```bash | ||
| npm run dev | ||
| ``` | ||
|  | ||
| Once your development server is running, you can trigger your workflow by running this command in the terminal: | ||
|  | ||
| ```bash | ||
| curl -X POST --json '{"email":"hello@example.com"}' http://localhost:3000/api/signup | ||
| ``` | ||
|  | ||
| Check the SvelteKit development server logs to see your workflow execute as well as the steps that are being processed. | ||
|  | ||
| Additionally, you can use the [Workflow DevKit CLI or Web UI](/docs/observability) to inspect your workflow runs and steps in detail. | ||
|  | ||
| ```bash | ||
| npx workflow inspect runs | ||
| # or add '--web' for an interactive Web based UI | ||
| ``` | ||
|  | ||
| <img src="/o11y-ui.png" alt="Workflow DevKit Web UI" /> | ||
|  | ||
| --- | ||
|  | ||
| ## Deploying to production | ||
|  | ||
| Workflow DevKit apps currently work best when deployed to [Vercel](https://vercel.com/home) and needs no special configuration. | ||
|  | ||
| Check the [Deploying](/docs/deploying) section to learn how your workflows can be deployed elsewhere. | ||
|  | ||
| ## Next Steps | ||
|  | ||
| * Learn more about the [Foundations](/docs/foundations). | ||
| * Check [Errors](/docs/errors) if you encounter issues. | ||
| * Explore the [API Reference](/docs/api-reference). | 
      
      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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.