Skip to content
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

Pass request to createStorefrontClient #1444

Closed
wants to merge 9 commits into from

Conversation

frandiox
Copy link
Contributor

@frandiox frandiox commented Oct 23, 2023

Let's discuss if this is a good API or not (original public discussion). The problem is the "cross runtime request" issue.

I've added tests here for a the storefront client as well (the tests we had before were in an e2e that we removed long ago...), so we should merge at least that (will revert other things if we don't want it).

@frandiox frandiox requested a review from a team October 23, 2023 10:27
@github-actions
Copy link
Contributor

We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset.
If the changes are user-facing and should cause a version bump, run npm run changeset add to track your changes and include them in the next release CHANGELOG.
If you are making simple updates to examples or documentation, you do not need to add a changeset.

Comment on lines +364 to +365
[STOREFRONT_ACCESS_TOKEN_HEADER]:
defaultHeaders[STOREFRONT_ACCESS_TOKEN_HEADER],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wizardlyhel This one is only the public token. The private one lives in a different variable and is not being added to the cache key. Is this correct?

Copy link
Contributor

@wizardlyhel wizardlyhel Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. The access token could be the private token if it exists

const getHeaders = clientOptions.privateStorefrontToken
? getPrivateTokenHeaders
: getPublicTokenHeaders;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait nvm .. those 2 function generates different headers

getPrivateTokenHeaders returns contentType, buyerIp, privateStorefrontToken
getPublicTokenHeaders returns contentType, publicStorefrontToken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's different header. Should I fix that here and also add the private one?

Comment on lines +13 to +21
export function getClientIp(request: CrossRuntimeRequest) {
return (
request.headers?.get?.('oxygen-buyer-ip') ??
request.headers?.get?.('cf-connecting-ip') ??
request.headers?.['x-forwarded-for']?.split(',')[0] ??
request.socket?.remoteAddress ??
null
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK to check for Node here and CFW here? We are leaving Deno and Bun out, but currently those don't provide the IP in the request object.

Should we just care about Oxygen instead? In that case I think we should add an optional parameter to overwrite this behavior for non-Oxygen environments, right (like the current storefrontHeaders.buyerIp).

Copy link
Contributor

@wizardlyhel wizardlyhel Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to just care about what Hydrogen cares about but developers must be able to override this ip value since it is the only way to avoid being blocked by Shopify ABS if they were to use other hosting options. Example: AWS hosting

Comment on lines +26 to +29
if (!requestId) {
// Store it in the headers object for later access
request.headers['request-id'] = requestId = generateUUID();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This piece here is going to generate an ID and store it in the request headers object so that it can be used in the next call (e.g., when calling createWithCache and later createStorefrontClient, they should access the same request-id if the environment doesn't provide it).

@@ -26,7 +25,7 @@ export default {
throw new Error('SESSION_SECRET environment variable is not set');
}

const waitUntil = (p: Promise<any>) => executionContext.waitUntil(p);
const waitUntil = executionContext.waitUntil.bind(executionContext);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I'm changing these lines to use bind because that's what we have in our skeleton template. It's not as easy to understand as arrow functions but it keeps the type correct in JS projects, which I think is useful?

@wizardlyhel
Copy link
Contributor

Hmm .. passing the request object to withCache makes sense, since we are just pulling minimal header data defined with Hydrogen. If these headers are wrong, it has no significant impact.

However, createStorefrontClient needs more cross env concerns since it would be huge impact if a header is missing (notably the buyerIp header)

@frandiox
Copy link
Contributor Author

However, createStorefrontClient needs more cross env concerns since it would be huge impact if a header is missing (notably the buyerIp header)

Yeah I also have the same feeling. Just wanted to show how this looks like but not really sure about it.

Let's see what the rest of the team thinks. @blittle @juanpprieto

@blittle
Copy link
Contributor

blittle commented Oct 24, 2023

@frandiox @wizardlyhel I agree, as long as we are unopinionated about the deployment platform, then I don't think it's worth making this change.

@frandiox
Copy link
Contributor Author

The tests are extracted in #1449

@frandiox frandiox closed this Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants