Using Hydrogen components in Next.js #240
Replies: 9 comments 34 replies
-
Have you tested these methods? While using the stable Next.js (without server components) I am unable to import anything from
And when I'm using the alpha Next.js server components I am unable to use the Not sure what to do here. |
Beta Was this translation helpful? Give feedback.
-
When using I have created my own const storeDomain = "hydrogen-preview.myshopify.com"
const storefrontToken = "3b580e70970c4528da70c98e097c2fa0"
export async function getShopifyData(query, variables) {
return await fetch(`https://${storeDomain}/api/2021-10/graphql.json`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"X-Shopify-Storefront-Access-Token": storefrontToken
},
body: JSON.stringify({
query,
variables
})
}).then((response) => {
return response.json()
})
} But now attempting to copy and paste the queries from the preview will result in:
Here is my full export async function getStaticProps() {
const data = await getShopifyData(`
query product(
$country: CountryCode
$handle: String!
$numProductMetafields: Int = 20
$numProductVariants: Int = 250
$numProductMedia: Int = 6
$numProductVariantMetafields: Int = 10
$numProductVariantSellingPlanAllocations: Int = 0
$numProductSellingPlanGroups: Int = 0
$numProductSellingPlans: Int = 0
) @inContext(country: $country) {
product: product(handle: $handle) {
id
vendor
seo {
title
description
}
images(first: 1) {
edges {
node {
url
}
}
}
...ProductProviderFragment
}
}
${ProductProviderFragment}
`,
{
country: "US",
handle: "mail-it-in-freestyle-snowboard"
}
)
console.log(data)
return {
props: {
data: data
},
revalidate: 60 * 5
}
} |
Beta Was this translation helpful? Give feedback.
-
Can we get any more information about this new mechanism? When using |
Beta Was this translation helpful? Give feedback.
-
I'm thinking of using Vue with Nuxt, will that mechanism be easy to implement in that or other frameworks? |
Beta Was this translation helpful? Give feedback.
-
I am importing renderHydrogen into my app and calling it around my App export just like in the example here: https://stackblitz.com/edit/shopify-hydrogen-ajtgy7?file=src%2FApp.server.jsx&title=Hydrogen When I do, I receive the attached error: How can I use renderHydrogen and the ShopifyProvider and CartProvider in NextJS without the virtual__hydrogen.config.ts and virtual__hydrogen-routes.server.jsx files? |
Beta Was this translation helpful? Give feedback.
-
Are there any news or updates on using Hydrogen components in Next.js? Are the workarounds mentioned still required? |
Beta Was this translation helpful? Give feedback.
-
I'd also love to hear about any updates on this front. Also a demo repo would be 👌 |
Beta Was this translation helpful? Give feedback.
-
Hi, Are we supposed to be able to use hooks? like The const is defined in vite-plugin-hydrogen-config.ts Edit: seems like that's what this issue and that one are about. |
Beta Was this translation helpful? Give feedback.
-
👋 Hey friends! Good news: Hydrogen UI is now in beta See the repo here: https://github.com/Shopify/hydrogen-ui Note that the API is subject to change, so use it at your own risk. We're currently targeting Next.js and Remix as examples as we build out the package. |
Beta Was this translation helpful? Give feedback.
-
Update 10/31/2022
Good news: Hydrogen UI is now in beta. See the repo here: https://github.com/Shopify/hydrogen-ui
Moving forward, Hydrogen UI is the recommended way to use Hydrogen components in frameworks that are not Hydrogen itself.
Original Post (outdated)
The majority of Hydrogen components accept data from the Storefront API and render just like normal React components. This means you can use Hydrogen hooks and components in other frameworks like Next.js and Gatsby.
The Hydrogen team has not optimized for this use case for the developer preview, so you will need to follow these special instructions to make it work. While the instructions below are specific to Next.js, you can follow a similar pattern to support other frameworks.
Getting started with Next.js
In your Next.js app, you will need to install Hydrogen, react-router-dom, and next-transpile-modules:
Next, you'll need to tell Next to compile
@shopify/hydrogen
from ESM to CJS by editing yournext.config.js
:A couple important notes about this process:
Fetching data in Next.js
We recommend calling the Storefront API on the server. Hydrogen will soon introduce a mechanism to make server-to-server calls without exhausting rate limits, so it's best to structure your apps this way from the start.
Here is an example of a product page in Next.js (pseudo-code — copy/pasting this will NOT work out of the box):
Limitation: No
useShopQuery
You cannot use
useShopQuery
in Next.js as it is a hook meant to be run in Hydrogen's server components. Since Next.js (stable) does does not allow you to render a component only on the server, you must fetch data in agetStaticProps
orgetServerSideProps
function instead.We recommend writing a utility like
getShopifyData({query, variables})
which takes your storefront API credentials and makes a GraphQL query. You can then use this in the Next.js functions.Hydrogen might introduce a utility function like this in the future.
Limitation: No server components
Hydrogen is currently using a modified version of server components which supports context and SSR. This is not yet supported in the version of server components that Next.js uses.
If you want to use the alpha version of server components in Next.js, you will need to wrap any Hydrogen component that use Context (most of them) in
*.client.js
components. This is the only way to currently use context Next.js server components.Once React introduces Server Context, this limitation will no longer apply.
Beta Was this translation helpful? Give feedback.
All reactions