-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: middleware for several adapters #13477
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
Draft
dummdidumm
wants to merge
49
commits into
main
Choose a base branch
from
middleware-take-2
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.
Draft
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
8269fd8
provide emulate hook to intercept request early on
dummdidumm 9d0beac
vercel WIP
dummdidumm fd8c296
fix
dummdidumm b5ea739
bundle ourselves instead
dummdidumm ac0ca4b
wrap rewrite and url automatically
dummdidumm 9a38b48
devtimefix
dummdidumm 2d406bd
add capability for adapters to provide additional entry points
dummdidumm 253318a
vercel middleware
dummdidumm 05e9b62
node
dummdidumm 8b8c4ac
lint
dummdidumm 344dd7b
expose new helpers from Kit
dummdidumm cb2d693
incorporate new helpers in Vercel adapter + fix a few bugs
dummdidumm db4ed04
do base path normalization within Vite middleware so adapters don't h…
dummdidumm b6e623f
tweak adapter API: switch allowed to disallowed, restrict imports to …
dummdidumm 1c9b512
tests
dummdidumm fe1b82b
writing adapters docs
dummdidumm 45b4706
fix
dummdidumm c20d423
fix lint / test
dummdidumm 62289cb
vercel-middleware -> edge-middleware
dummdidumm 9a3c6e2
netlify edge middleware
dummdidumm e994082
cloudflare adapter
dummdidumm 05c941e
fix
dummdidumm 1d9839a
fix
dummdidumm 5d5eb26
fix, docs
dummdidumm 70ad0f8
fix, docs tweaks
dummdidumm b159f21
fix
dummdidumm 4335dc5
changesets
dummdidumm b5f68cb
drive-by notes fix
dummdidumm 37bc902
reuse `supports` mechanism, simplify additionalEntryPoints to a recor…
dummdidumm 5585df2
fix
dummdidumm 0b5396e
do normalization inside adapters
dummdidumm 76f5a9b
don't bundle sveltekit
dummdidumm 679759f
put them into src so that they benefit from better intellisense
dummdidumm e39683e
fix docs
dummdidumm c1f7591
Merge branch 'main' into middleware-take-2
dummdidumm 0876716
tweaks
dummdidumm 8524d6f
netlify: make where it runs on configurable; node/preview: run it on …
dummdidumm 6e90adc
beforeRequest -> interceptRequest
dummdidumm 083b22d
fix test config
dummdidumm bec3bed
Merge branch 'main' into middleware-take-2
dummdidumm 4e948fa
Merge remote-tracking branch 'origin/main' into middleware-take-2
dummdidumm b367434
Update tsconfig.json
dummdidumm 1ee1d04
docs tweaks
dummdidumm a2c5222
fix folder path
dummdidumm 30a3680
tweak
dummdidumm a2bb531
Merge branch 'main' into middleware-take-2
Rich-Harris cd116e9
docs feedback
dummdidumm 663d59d
warn on stream read (don't error because you can't guard against it i…
dummdidumm f9efcbd
fix test
dummdidumm 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-vercel': minor | ||
--- | ||
|
||
feat: support edge middleware |
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,5 @@ | ||
--- | ||
'@sveltejs/adapter-node': minor | ||
--- | ||
|
||
feat: add support for polka/express middleware |
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,5 @@ | ||
--- | ||
'@sveltejs/kit': minor | ||
--- | ||
|
||
feat: allow adapters to influence compilation entry points and to intercept requests at dev/preview time |
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,5 @@ | ||
--- | ||
'@sveltejs/adapter-vercel': minor | ||
--- | ||
|
||
feat: add support for edge middleware |
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,5 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare': minor | ||
--- | ||
|
||
feat: add pages-like middleware |
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 |
---|---|---|
|
@@ -107,6 +107,53 @@ Cloudflare Workers specific values in the `platform` property are emulated durin | |
|
||
For testing the build, you should use [Wrangler](https://developers.cloudflare.com/workers/wrangler/) **version 3**. Once you have built your site, run `wrangler pages dev .svelte-kit/cloudflare`. | ||
|
||
## Pages Middleware | ||
|
||
You can deploy one middleware function that closely follows the [Pages Middleware API](https://developers.cloudflare.com/pages/functions/middleware/). Unlike the [handle](/docs/kit/hooks#Server-hooks-handle) hook, middleware runs on all requests, including for static assets and prerendered pages (depending on your configuration). If using [server-side route resolution](configuration#router) this means it runs prior to all navigations, no matter client- or server-side. This allows you to for example run A/B-tests on prerendered pages by rerouting a user to either variant A or B depending on a cookie. | ||
|
||
> [!NOTE] It isn't really Pages Middleware because the adapter compiles to a [single `_worker.js` file](https://developers.cloudflare.com/pages/platform/functions/#advanced-mode) (also see the [Notes](#Notes) section), which ignores middleware, but it closely mirrors its capabilities. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Pages Middleware and Functions are all compiled to a single |
||
|
||
To get started, place a `cloudflare-middleware.js` file in your `src` folder and export an `onRequest` function from it: | ||
|
||
```js | ||
/// file: src/cloudflare-middleware.js | ||
import { normalizeUrl } from '@sveltejs/kit'; | ||
import { parse } from 'cookie'; | ||
|
||
/** | ||
* @param {import('@sveltejs/adapter-cloudflare').EventContext)} context | ||
*/ | ||
export function onRequest({ request, next }) { | ||
const url = new URL(request.url); | ||
|
||
if (url.pathname !== '/') return next(); | ||
|
||
// Retrieve cookies which contain the feature flags. | ||
let flag = parse(request.headers.get('cookie') ?? '').flags; | ||
|
||
// Fall back to random value if this is a new visitor | ||
flag ||= Math.random() > 0.5 ? 'a' : 'b'; | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Get destination URL based on the feature flag | ||
request = new Request(new URL(flag === 'a' ? '/home-a' : '/home-b', url), request); | ||
|
||
const response = await next(request); | ||
|
||
// Set a cookie to remember the feature flags for this visitor | ||
response.headers.set('Set-Cookie', `flags=${flag}; Path=/`); | ||
|
||
return response; | ||
} | ||
``` | ||
|
||
The `context` parameter closely follows the [EventContext](https://developers.cloudflare.com/pages/functions/api-reference/#eventcontext) object but is missing some Pages-specific parameters such as `data`, `params` and `functionPath`. | ||
|
||
> [!NOTE] If you want to run code prior to a request but neither have prerendered pages nor rerouting logic, then it makes more sense to use the [handle hook](hooks#Server-hooks-handle) instead. | ||
|
||
The middleware runs on all requests that your worker is invoked for, which is dependent on the [`include/exclude` options](#Options-routes). | ||
|
||
> [!NOTE] Locally during dev and preview this only approximates the capabilities of middleware. Notably, you cannot read the request or response body, and the `include/exclude` options are not honored. | ||
|
||
## Notes | ||
|
||
Functions contained in the [`/functions` directory](https://developers.cloudflare.com/pages/functions/routing/) at the project's root will _not_ be included in the deployment. Instead, functions should be implemented as [server endpoints](routing#server) in your SvelteKit app, which is compiled to a [single `_worker.js` file](https://developers.cloudflare.com/pages/functions/advanced-mode/). | ||
|
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
Oops, something went wrong.
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.
In real Pages middleware you can export
onRequestGet
andonRequestPost
and so on, and the exports can be arrays of middlewares. How committed are we to making this resemble the real thing?I feel like we should either aim for full fidelity or avoid making it look like something it's not — I'm getting a real uncanny valley sensation to be honest