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

Document or deprecate use of HTTP_SPREE_STORE header #1971

Closed
brchristian opened this issue May 25, 2017 · 11 comments
Closed

Document or deprecate use of HTTP_SPREE_STORE header #1971

brchristian opened this issue May 25, 2017 · 11 comments

Comments

@brchristian
Copy link
Contributor

brchristian commented May 25, 2017

I have noticed three(!) SQL calls related to Spree::Store on every single pageview and wondered what was going on...it seems excessive.

screen shot 2017-05-25 at 3 37 48 pm

The culprit is this line from current_store.rb:

def store_key
  @request.headers['HTTP_SPREE_STORE'] || @request.env['SERVER_NAME']
end

It seems that (since this commit from two years ago), the default method of checking the current store is by looking at the HTTP_SPREE_STORE response header.

However, looking far and wide I could find a total of zero documentation both in the Solidus codebase, and on the entire internet (!), about how to set or use the HTTP_SPREE_STORE header.

My feeling here is that we need to either (a) deprecate, or (b) document the use of this.

@brchristian
Copy link
Contributor Author

e.g., perhaps this can be extracted into https://github.com/solidusio/solidus_multi_domain?

@kennyadsl
Copy link
Member

@brchristian Hi there, which solidus version are you using? I think this is fixed with this commit.

When @request.headers['HTTP_SPREE_STORE'] or @request.env['SERVER_NAME'] are not set only the default store query (last one) should be done.

@brchristian
Copy link
Contributor Author

@kennyadsl I’m using the latest gem release, which is 2.2.1.

I note that out of the box, the default 2.2.1 store makes six Spree::Store find calls per pageview! (Three for the main layout, and three for the cart partial.)

My hunch is that only a small fraction of the Solidus user base have more than one Store. My personal opinion is that six SQL calls per pageview is a lot of DB chatter to support what is (IMO) a somewhat niche use-case.

What do you think about trying to move this functionality out into the solidus_multi_domain gem and support a single store by default in the vanilla Solidus gem?

@brchristian
Copy link
Contributor Author

@kennyadsl Also, the commit you referenced was reverted by 0abb4d0!

@kennyadsl
Copy link
Member

@brchristian I think the last commit you posted is not reverting the one I linked but it is just removing the deprecation previously set.

As far as I can see this is the code you are using:

      def store
        if store_key
          Spree::Store.current(store_key)
        else
          Spree::Store.default
        end
      end

      private

      def store_key
        @request.headers['HTTP_SPREE_STORE'] || @request.env['SERVER_NAME']
      end

So if store_key is nil (no @request.headers['HTTP_SPREE_STORE'] or @request.env['SERVER_NAME'] set) it should just make the default store query.

Anyway I'm going to do some more testing on a clean solidus app and see if I find those queries and they are coming from other places in the code. Will keep you posted!

@brchristian
Copy link
Contributor Author

@kennyadsl Oh, I think you are right about the deprecation. Sorry and thanks for the correction!

When I create a brand new blank Solidus app, @request.env['SERVER_NAME'] is "localhost" by default, and so the conditional for store_key is always true.

What’s more, I think that there is some redundancy because store_key can be either the HTTP_SPREE_STORE field or the SERVER_NAME. This forces us in https://github.com/solidusio/solidus/blob/master/core/app/models/spree/store.rb#L25 to check by code and by url, generating two queries.

Seems like we can clean this up, for sure!

@kennyadsl
Copy link
Member

@brchristian I think that moving that code out of core (in solidus_multi_domain as you suggested) is the best way to go. I've just opened a PR (#1993) with initial steps for having it done. Let's see what others think about it and I'll proceed with the rest of the needed PRs.

kennyadsl added a commit to nebulab/solidus_multi_domain that referenced this issue Jun 15, 2017
This class logic is extracted from Solidus core, which is going
to be simplified a lot to avoid multiple queries at each request
if solidus_multi_domain extension is not used (solidusio/solidus#1971).
@kennyadsl
Copy link
Member

kennyadsl commented Jun 15, 2017

Steps to fix this issue:

kennyadsl added a commit to nebulab/solidus_multi_domain that referenced this issue Jun 15, 2017
This class logic is extracted from Solidus core, which is going
to be simplified a lot to avoid multiple queries at each request
if solidus_multi_domain extension is not used (solidusio/solidus#1971).
kennyadsl added a commit to nebulab/solidus_multi_domain that referenced this issue Jun 16, 2017
This class logic is extracted from Solidus core, which is going
to be simplified a lot to avoid multiple queries at each request
if solidus_multi_domain extension is not used (solidusio/solidus#1971).
kennyadsl added a commit to nebulab/solidus that referenced this issue Jun 16, 2017
This commit avoids doing 2 extra queries for every request (solidusio#1971)
even when solidus store is not using solidus_multi_domain extension.

This logic has now been moved to the solidus_multi_domain extension
since it is needed only when multiple stores are expected
(solidusio-contrib/solidus_multi_domain#67).
@jhawthorn
Copy link
Contributor

I also would like to remove support for HTTP_SPREE_STORE gone. Since it requires extra configuration to use properly, it doesn't make sense to support it on all stores by default.

I've opened #2041 which is related to this. It supports selecting a store by domain name and falling back to default with a single query ⚡.

@aitbw
Copy link
Contributor

aitbw commented Feb 26, 2019

I think this one can be closed as per #2041, right @kennyadsl?

@kennyadsl
Copy link
Member

Yes, thanks @aitbw !

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

No branches or pull requests

4 participants