feat: Add a conditional filter hook before generating the session token #913
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this implement?
Sometimes, we need to avoid generating a session for certain services. I am using the App Router in Next.js version 14, and I send requests on the server side. I don’t want a WooCommerce session to be created unnecessarily for each request, as the request is on the server and does not have access to the session in local storage (it can access cookies, but I have run multiple requests initially before the cookies were set). Therefore, I would like to add a header to my server-side requests that indicates this API request came from the server and does not require the creation of a WooCommerce session.
Does this close any currently open issues?
Based on issue #908, my front-end is hosted at example.com while WordPress is hosted at
api.example.com
. As a result, the browser sends anOPTIONS
request to checkCORS
. This filter hook can help resolve the issues. In the filter hook of my custom plugin, I can check if the request is anOPTIONS
request and prevent the creation of asession token
.Example