Skip to content

Commit

Permalink
Fix concatenating requests (#2536)
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Sep 1, 2023
1 parent fcabc75 commit 532c5f3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .changeset/wet-boats-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@shopify/hydrogen': patch
---

Allow concatenating requests with a header. For example, if you want the route `/shop` to proxy render everything on `/products`, setup an API route at `/routes/shop.server.jsx` with the following:

```jsx
export async function api(request) {
return new Request(new URL(request.url).origin + '/products', {
headers: {
'Hydrogen-Concatenate': 'true',
},
});
}
```
2 changes: 1 addition & 1 deletion packages/hydrogen/src/utilities/apiRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export async function renderApiRoute(
return new Request(getRscUrl(url, newUrl), {
headers: response.headers,
});
} else {
} else if (!response.headers.get('hydrogen-concatenate')) {
// This request was made by a native form presumably because the client components had yet to hydrate,
// Because of this, we need to redirect instead of just rendering the response.
// Doing so prevents odd refresh / back behavior. The redirect response also should *never* be cached.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export async function api(request) {
return new Request(new URL(request.url).origin + '/about', {
headers: {
'Hydrogen-Concatenate': 'true',
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ export default async function testCases({
expect(await page.textContent('*')).toContain('fname=sometext');
});

it('can concatenate requests', async () => {
it('can concatenate form requests', async () => {
await page.goto(getServerUrl() + '/html-form');
expect(await page.textContent('#counter')).toEqual('0');
await page.click('#increase');
Expand All @@ -633,6 +633,11 @@ export default async function testCases({
expect(await page.textContent('#counter')).toEqual('2');
});

it('can concatenate requests', async () => {
await page.goto(getServerUrl() + '/concatenate');
expect(await page.textContent('body')).toContain('About');
});

it('responds with RSC', async () => {
const response = await page.request.post(getServerUrl() + '/account', {
data: `username=alincoln%40example.com&password=somepass`,
Expand Down

0 comments on commit 532c5f3

Please sign in to comment.