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

Allow components to return a Response #2944

Merged
merged 2 commits into from
Mar 30, 2022
Merged

Allow components to return a Response #2944

merged 2 commits into from
Mar 30, 2022

Conversation

matthewp
Copy link
Contributor

Changes

  • Allows components to return a Response, for redirects, etc.
  • This is meant so that Layout components can act as gates for auth flows, etc.

Testing

  • Tests added

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Mar 30, 2022

🦋 Changeset detected

Latest commit: a66349a

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

This PR includes changesets to release 1 package
Name Type
astro 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

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) test labels Mar 30, 2022
@@ -412,7 +412,8 @@ async function replaceHeadInjection(result: SSRResult, html: string): Promise<st
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any): Promise<string> {
const Component = await componentFactory(result, props, children);
if (!isAstroComponent(Component)) {
throw new Error('Cannot return a Response from a nested component.');
const response: Response = Component;
throw response;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You got what you wanted @natemoo-re

Copy link
Member

Choose a reason for hiding this comment

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

🤣 It makes sense!

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

LGTM! I'm trying to think through all the implications of this, but I think this is the "correct" way to allow this.

Nobody needs to know that we're throwing internally!

@@ -412,7 +412,8 @@ async function replaceHeadInjection(result: SSRResult, html: string): Promise<st
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any): Promise<string> {
const Component = await componentFactory(result, props, children);
if (!isAstroComponent(Component)) {
throw new Error('Cannot return a Response from a nested component.');
const response: Response = Component;
throw response;
Copy link
Member

Choose a reason for hiding this comment

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

🤣 It makes sense!

@matthewp matthewp merged commit c989f10 into main Mar 30, 2022
@matthewp matthewp deleted the throw-response branch March 30, 2022 19:55
@github-actions github-actions bot mentioned this pull request Mar 30, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Allow components to return a Response

* Changeset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants