-
Notifications
You must be signed in to change notification settings - Fork 323
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
Conversation
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. |
This is awesome! Very similar to what we've discussed in #883 |
Ohh, I missed that conversation, but glad to see we are on the same page. |
@frandiox I think this could be useful independent of the |
2976ae8
to
13376ef
Compare
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.
Great job!
); | ||
|
||
return apiResponse instanceof Request | ||
? handleRequest(apiResponse, options) |
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.
So beautiful :)
@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 |
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.
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 %}
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.
I've added a corresponding commit on the Shopify.dev side here: https://github.com/Shopify/shopify-dev/pull/18657/commits/82a68707be4c6a2c772b4bfbfd1467220b7db062
* 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) ...
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:
fixes #123
)yarn changeset add
if this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning