Shopify / js-buy-sdk

The JS Buy SDK is a lightweight library that allows you to build ecommerce into any website. It is based on Shopify's API and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.

Home Page:https://shopify.github.io/js-buy-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fetching products returns outdated data depending on API Version

dvub opened this issue · comments

commented

Bug details

Describe the bug

When I make a request with the SDK, I'm essentially always getting stale data. For some reason, jumping versions -- from 2023-10 to 2023-07 -- sometimes has fixed the issue (would need to test this more). The only consistently working hack I've found is to just reinstall the app.

To Reproduce
Steps to reproduce the behaviour:

  1. Create a development store on shopify and follow the conventional steps to create a new app. Go through the usual configuration and get the domain and storefront token.
  2. Create a nextjs app: npx create-next-app@latest - Make sure to create an app directory.
  3. Add the SDK: npm i shopify-buy
  4. Create a .env file with the appropriate SHOPIFY_STOREFRONT_TOKEN, SHOPIFY_DOMAIN, and SHOPIFY_API_VERSION variables.
  5. Under the app directory, create the directory/file: api/products/route.ts with this code:
import Client from 'shopify-buy';
export async function GET(request: Request) {
	const client = Client.buildClient({
		storefrontAccessToken: process.env.SHOPIFY_STOREFRONT_TOKEN!,
		domain: process.env.SHOPIFY_DOMAIN!,
		apiVersion: process.env.SHOPIFY_API_VERSION!, //2023-10 ?
	});

	const data = await client.product.fetchAll();
	console.log(data.length);
	return new Response(JSON.stringify(data));
}
  1. Make a request to the route, optionally within the app (I used axios and SWR - you'll need to install these if you do it this way)
  2. Add a product, make a new request, and compare the number of items. In my case, the number doesn't update when updating product information.

Expected behavior

When adding a new product through the shopify admin, the API route should reflect the updated data.

Environment (please complete the following information):

  • OS: Arch Linux (6.6.8-arch1-1)
  • Browser: Brave (Chromium)
  • SDK Version 2.21.1

Additional context
n/a
Bug Report Checklist

  • I have read and agree to the CODE_OF_CONDUCT.md
  • I have read the CONTRIBUTING.md guidelines.
  • I have provided a detailed description of the bug, including code samples, and/or network data.
  • I have provided information about my development environment, including SDK version.
commented

I realized this is an issue with nextjs and route handler caching. Closing this issue.