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

feat(middleware): Add .shortCircuit to MiddlewareResponse #10586

Merged
merged 8 commits into from
May 20, 2024

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented May 16, 2024

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:

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' },
    })
  }

}

@dac09 dac09 added the release:feature This PR introduces a new feature label May 16, 2024
@dac09 dac09 added this to the SSR milestone May 16, 2024
@dac09 dac09 requested a review from dthyresson May 16, 2024 12:28
Update comments
@dac09 dac09 marked this pull request as draft May 16, 2024 15:59
@dac09
Copy link
Collaborator Author

dac09 commented May 16, 2024

Feedback from DT:

  • shortCircuit shouldn't need to be returned. i.e. behaves like a throw e.g.
const rateLimitMIddleware = () => {
	if (dontAllowRequest) {
		MiddlewareResponse.shortCircuit({
		body: optionalBody, 
		status: requiredStatusCode, 
		headers: optionalHeaders
    })
    }
}
  • it should stop execution of ALL middleware, not JUST react render

@dac09 dac09 marked this pull request as ready for review May 17, 2024 09:59
@dac09 dac09 self-assigned this May 17, 2024
@dac09
Copy link
Collaborator Author

dac09 commented May 17, 2024

@dthyresson updated the implementation based on your feedback. Thanksssss ✌️

Copy link
Contributor

@dthyresson dthyresson left a comment

Choose a reason for hiding this comment

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

LGTM. This will be really useful

@dthyresson dthyresson merged commit ab3fb1a into redwoodjs:main May 20, 2024
46 checks passed
@dac09 dac09 deleted the feat/middleware-shortcircuit branch May 20, 2024 14:22
dac09 added a commit to dac09/redwood that referenced this pull request May 21, 2024
…tuples-dbauth-mw

* 'main' of github.com:redwoodjs/redwood:
  fix(deps): update dependency isbot to v5 (redwoodjs#10340)
  chore(deps): update dependency vscode-languageserver-protocol to v3.17.5 (redwoodjs#10351)
  chore(deps): update node.js to >=14.17 <=20.13 (redwoodjs#10368)
  fix(deps): update dependency graphql-sse to v2.5.3 (redwoodjs#10364)
  chore(deps): update dependency @supabase/supabase-js to v2.43.2 (redwoodjs#10365)
  chore(deps): update babel monorepo to v7.24.5 (redwoodjs#10614)
  fix(vite): Rename `serverAuthContext` to `serverAuthState` (redwoodjs#10653)
  feat(middleware): Add .shortCircuit to MiddlewareResponse (redwoodjs#10586)
  chore(ssr): Make entry.client.tsx better match standard entry.client (redwoodjs#10652)
  patch(crwa): Fix small annoyances (formatting, spell-check) (redwoodjs#10651)
  chore(ci): Update rsc readme (redwoodjs#10650)
  chore(serverAuth): Rename serverAuthContext to serverAuthState where relevant (redwoodjs#10643)
  chore(deps): Remove unused deps from prerender (redwoodjs#10649)
  Add missing rwjs/auth deps (redwoodjs#10648)
  feat(rsc-auth): Implement serverStore to hold and pass req info to RSC (redwoodjs#10585)
  chore(deps): Remove unused octokit package (redwoodjs#10645)
  chore(deps): bump @fastify/reply-from from 9.4.0 to 9.8.0 (redwoodjs#10644)
@Josh-Walker-GM Josh-Walker-GM modified the milestones: SSR, v8.0.0 Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants