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 returning new requests from API routes #1057

Merged
merged 4 commits into from
Apr 12, 2022

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented Apr 8, 2022

Description

Exploring here a simple way to concatenate requests in an API route. This works with raw HTML forms. We could also return an RSC response instead, but we'd need a custom Form component to interpret the result.

Instead of returning a new Request we could use a helper function or anything like that.

@blittle @jplhomer do you think this concept could be interesting?


Before submitting the PR, please make sure you do the following:

  • Read the Contributing Guidelines
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123)
  • Update docs in this repository according to your change
  • Run yarn changeset add if this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning

@blittle
Copy link
Contributor

blittle commented Apr 8, 2022

Holy cow, that turned out to be super simple. I was thinking this would be more complex. In my prior experimentation, I just returned a redirect. This is much better. Maybe when we implement the Form, we could also implement a helper for server components.

@jplhomer
Copy link
Contributor

jplhomer commented Apr 8, 2022

This is awesome! Very similar to what we've discussed in #883

@frandiox
Copy link
Contributor Author

frandiox commented Apr 8, 2022

Ohh, I missed that conversation, but glad to see we are on the same page.
Should I clean this up to build on top of it? Or wait until we have a Form component that can also handle RSC responses?

@blittle
Copy link
Contributor

blittle commented Apr 8, 2022

@frandiox I think this could be useful independent of the Form component.

@blittle blittle force-pushed the v1.x-2022-07 branch 3 times, most recently from 2976ae8 to 13376ef Compare April 11, 2022 15:27
@frandiox frandiox changed the title Exploration: Allow returning new requests from API routes Allow returning new requests from API routes Apr 12, 2022
@frandiox frandiox marked this pull request as ready for review April 12, 2022 12:00
@frandiox frandiox requested review from blittle and mcvinci April 12, 2022 12:00
Copy link
Contributor

@blittle blittle left a comment

Choose a reason for hiding this comment

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

Great job!

);

return apiResponse instanceof Request
? handleRequest(apiResponse, options)
Copy link
Contributor

Choose a reason for hiding this comment

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

So beautiful :)

@frandiox frandiox merged commit 06d92dd into v1.x-2022-07 Apr 12, 2022
@frandiox frandiox deleted the fd-concatenate-request-api-route branch April 12, 2022 12:16
@frandiox
Copy link
Contributor Author

@mcvinci I'm merging this because we need to build on top of it but please, feel free to give feedback about the docs!

@@ -188,6 +188,30 @@ export async function api(request, {params}) {
> Tip:
> Explore an [example implementation in GitHub](https://github.com/Shopify/hydrogen/blob/main/examples/template-hydrogen-default/src/routes/countries.server.jsx) that lazy loads [available countries](https://github.com/Shopify/hydrogen/blob/main/examples/template-hydrogen-default/src/components/CountrySelector.client.jsx) by an API route (`/api/countries`).

### Concatenating requests
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for updating the docs, @frandiox! I just have some minor suggestions regarding the codeblock syntax and some sentence tweaks to align with our style guide.

### Concatenating requests

You can concatenate requests in API routes to avoid extra network trips (waterfall requests). Concatenating requests is useful for HTML forms, where it's common to refresh the current page after the form submission is completed.

{% codeblock file, filename: "src/routes/my-page.server.jsx" %}

```jsx
export async function api(request) {
  if (request.method === 'POST') {
    // do some work here...
  }
  return new Request(request.url, {method: 'GET'});
}
export default function Page() {
  return (
    <form action="/my-page" method="POST">
      ...
    </form>
  );
}
```

{% endcodeblock %}

Copy link
Contributor

Choose a reason for hiding this comment

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

blittle added a commit that referenced this pull request Apr 19, 2022
* v1.x-2022-07: (37 commits)
  Adds Shopify/hydrogen-cli for development server (#1089)
  Make `useUrl()` reactive for subscribing to navigation events (#1082)
  fix e2e tests
  streaming and suspense draft (#1033)
  [Hydrogen docs]: Restructure and simplify IA (#1079)
  Use new storefront API headers in queryShop API helper (#1075)
  Remove the dep of hydrogen-ui for now until we get it publishing (#1076)
  [ci] release v1.x-2022-07 (#1048)
  Introduce a synchronous-looking version of fetch for server and client (#983)
  improve the current type of ShopifyContextValue to be based on ShopifyConfig (#1072)
  Foundational work to split hydrogen-ui to its own package (#1060)
  Allow returning new requests from API routes (#1057)
  [Hydrogen docs]: Analytics (#962)
  Analytics (#890)
  Allow line breaks inside script tags (#1061)
  Fix encoding of CSS Modules to solve hydration errors (#1062)
  [Hydrogen docs]: Update guidance on troubleshooting third-party dependencies (#1055)
  Update Vite RSC Plugin (#1054)
  Fix link prefetch (#1059)
  Initial devTools component (#998)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants