-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): Add .shortCircuit to MiddlewareResponse (#10586)
### `MiddlewaresResponse.shortCircuit()` Adds a helper to generate a intercept/short-circuit response, that will interrupt execution of _all_ middleware and react rendering, and immediately return the response. There's a few different ways you can use this, see examples below: ```ts const shortCircuitMw: Middleware = (req, res) => { // A) You can short circuit after building the response (or use the res param) // This allows you to use all the convenience helpers like cookies of MW Response if (req.url.includes('create-new-response')) { const shortCircuitResponse = new MiddlewareResponse('Short-circuiting') shortCircuitResponse.headers.set('shortCircuit', 'yes') shortCircuitResponse.cookies.set('shortCircuitCookie', 'do-not-allow', { expires: new Date(Date.now() + 1000 * 60 * 60), }) shortCircuitResponse.shortCircuit() } // B) You can directly construct a new short-circuit response // (discarding whatever response was built before) if (req.url.includes('using-existing-res')) { res.shortCircuit('Short-circuiting directly', { headers: { shortCircuitDirect: 'yes' }, }) } } ```
- Loading branch information
Showing
5 changed files
with
197 additions
and
2 deletions.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
- feat(middleware): Add .shortCircuit to MiddlewareResponse (#10586) by @dac09 | ||
|
||
Adds a helper to generate a intercept/short-circuit response, that will interrupt execution of _all_ middleware and react rendering, and immediately return the response. | ||
|
||
There's a few different ways you can use this, see examples below: | ||
|
||
```ts | ||
const shortCircuitMw: Middleware = (req, res) => { | ||
// A) You can short circuit after building the response (or use the res param) | ||
// This allows you to use all the convenience helpers like cookies of MW Response | ||
if (req.url.includes('create-new-response')) { | ||
const shortCircuitResponse = new MiddlewareResponse('Short-circuiting') | ||
shortCircuitResponse.headers.set('shortCircuit', 'yes') | ||
shortCircuitResponse.cookies.set('shortCircuitCookie', 'do-not-allow', { | ||
expires: new Date(Date.now() + 1000 * 60 * 60), | ||
}) | ||
shortCircuitResponse.shortCircuit() | ||
} | ||
|
||
// B) You can directly construct a new short-circuit response | ||
// (discarding whatever response was built before) | ||
if (req.url.includes('using-existing-res')) { | ||
res.shortCircuit('Short-circuiting directly', { | ||
headers: { shortCircuitDirect: 'yes' }, | ||
}) | ||
} | ||
|
||
} | ||
``` | ||
|
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
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