Fix add-to-cart session event in Live View#614
Conversation
frandiox
left a comment
There was a problem hiding this comment.
Thanks for the fixes! I've added some comments below.
In order to decouple the request object and its headers from Hydrogen, would it make sense to have a utility imported from Oxygen that returns all the necessary headers for Hydrogen? Something like:
import {getHeadersForHydrogen} from '@shopify/remix-oxygen';
//...
createStorefrontClient({
cache,
waitUntil,
i18n: getLocaleFromRequest(request),
publicStorefrontToken: '...',
privateStorefrontToken: '...,
storeDomain: '...',
storefrontApiVersion: '...',
// This populates buyerIp, requestGroupId, cookies, etc.
...getHeadersForHydrogen(request),
})This way it's the platform adapter the one in charge to access the platform request. When deploying to Node/CFW/etc. devs can just get the headers manually and pass them as parameters like they do now. This would solve the comment below regarding request type.
The name of the utility is just an example of course.
Note: the call to getShopifyCookies can stay in Hydrogen, just the cookie header comes from getHeadersForHydrogen.
Alternatively, we could add a headers: getHeadersForHydrogen(request) property if we dislike the spread operator here, and just ignore the current buyerIp and requestGroupId if provided. I think I'd slightly prefer the spread operator, though.
|
I like this approach. This guarantees the import {getHeadersForHydrogen} from '@shopify/remix-oxygen';
//...
createStorefrontClient({
cache,
waitUntil,
i18n: getLocaleFromRequest(request),
publicStorefrontToken: '...',
privateStorefrontToken: '...,
storeDomain: '...',
storefrontApiVersion: '...',
// This populates buyerIp, requestGroupId, cookies, etc.
...getHeadersForHydrogen(request),
})As for the parameter layout, I don't like spread because it make typescript angry. So I am leaning towards the createStorefrontClient({
cache,
waitUntil,
i18n: getLocaleFromRequest(request),
publicStorefrontToken: '...',
privateStorefrontToken: '...,
storeDomain: '...',
storefrontApiVersion: '...',
// This populates buyerIp, requestGroupId, cookies, and developer can further extend to add user defined headers
header: getHeadersForHydrogen(request),
buyerIp, // deprecation warning for next calver release
requestGroupId, // deprecation warning for next calver release
}) |
Sure, I don't have a big opinion on this. I'm not sure if @Shopify/hydrogen @gfscott tl;dr Team, can we think of a better name for this prop and function? import {getHeadersForHydrogen} from '@shopify/remix-oxygen';
createStorefrontClient({
// ...
headers: getHeadersForHydrogen(request),
})The function is extracting information from the request. For now they are just headers (and will likely be always headers... right?). Examples are Other ideas:
|
Couple questions just to make sure I'm getting the full picture here:
|
|
I like These are specific headers for making a SFAPI call |
|
Decided to go with this: + import {getStorefrontHeaders} from '@shopify/remix-oxygen';
import {createStorefrontClient, storefrontRedirect} from '@shopify/hydrogen';
export default {
async fetch(
request: Request,
env: Env,
executionContext: ExecutionContext,
): Promise<Response> {
const {storefront} = createStorefrontClient({
cache,
waitUntil,
- buyerIp: getBuyerIp(request),
i18n: {language: 'EN', country: 'US'},
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: `https://${env.PUBLIC_STORE_DOMAIN}`,
storefrontApiVersion: env.PUBLIC_STOREFRONT_API_VERSION || '2023-01',
storefrontId: env.PUBLIC_STOREFRONT_ID,
- requestGroupId: request.headers.get('request-id'),
+ storefrontHeaders: getStorefrontHeaders(request),
});
To-do
|
frandiox
left a comment
There was a problem hiding this comment.
Added a few minor suggestions 👍
We'll need to update the docs as well when releasing this.
| } | ||
|
|
||
| // Deprecation warning | ||
| if (!storefrontHeaders) { |
There was a problem hiding this comment.
Since I guess storefrontHeaders will still be optional, perhaps better to warn only when the other properties are used?
| if (!storefrontHeaders) { | |
| if (!storefrontHeaders && (buyerIp || requestGroupId)) { |
There was a problem hiding this comment.
I initially was gonna detect by buyerIp and requestGroupId.. but both of these are null in dev mode
and .. don't we need to have one or the other?
Co-authored-by: Fran Dios <frankdiox@gmail.com>
Co-authored-by: Fran Dios <frankdiox@gmail.com>
Co-authored-by: Fran Dios <frankdiox@gmail.com>
WHY are these changes introduced?
Add-to-cart session in Live View is not working
Docs - https://github.com/Shopify/shopify-dev/pull/31897
WHAT is this pull request doing?
HOW to test your changes?
Result: You should see the
Active Cartcounter increase in theCustomer Behaviourcard.Note: You won't see the
Visitors right nowcounter increase if you are running in preview. This will only increase when running in production.Checklist