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

feat: add getStore function used in nuxt.config.js #300

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

fpdrozd
Copy link
Contributor

@fpdrozd fpdrozd commented Apr 4, 2023

Description

This PR adds a helper that fetches the Spree store configuration from the api that is then being passed to the nuxt.config.js. This is done to avoid static VSF configuration as much as possible.

There are a few crucial parts of the configuration that the api response is missing data for:

{
  locales: [
    {
      code: 'de',
      iso: 'de-DE',
      label: 'German',
      file: 'de.js'
    }
  ],
  currencies: {
    { code: 'USD', label: 'Dollar' },
  },
  country: 'US',
}

When it comes to loading this config dynamically on each page load, the available locales for i18n module have to be defined in nuxt.config.js (docs). The currencies and meta tags could be loaded dynamically from the app. But splitting this logic between nuxt.config and components seems a bit messy to me, besides it won't solve the problem of having to restart the vsf server.

We could put all of the store config initialisation into the nuxt.config file (as it is now) and set up a redeployment with webhooks every time the store configuration in Spree changes. Technically only rerunning yarn start would be enough but that is up to the hosting platform.

Related Issue

Motivation and Context

How Has This Been Tested?

visually

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Comment on lines 18 to 19
const store = await getStore({
backendUrl: serverConfig.backendUrl
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for not replacing nuxt.config.js with .ts extension? We're lacking type safety in many places here, eg. const store: any.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean type inference works here anyway because of how typescript is configured in this project. But yes we should change that here and in all the other places where files are defined as .js. I'll make a separate PR for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


dotenv.config({ path: `./../../.env.${process.env.NODE_ENV}` });

const serverConfig = {
port: process.env.PORT || 3000,
host: process.env.HOST || '0.0.0.0',
baseUrl: process.env.BASE_URL || 'http://localhost:3000'
baseUrl: process.env.BASE_URL || 'http://localhost:3000',
backendUrl: process.env.BACKEND_URL
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider adding base to the variable name (backendBaseUrl) to emphasize its potential structure. Or even storeBaseUrl since backend is not that self-explainatory in my opinion.

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 that might be confusing since technically we also have the Nuxt backend. I would opt for storeBaseUrl. Changing the env variable name would be a breaking change though (BACKEND_URL) and it might confuse developers who are already using the integration.

@rafalcymerys What do you think?

packages/theme/getStore.ts Outdated Show resolved Hide resolved
packages/theme/getStore.ts Outdated Show resolved Hide resolved
packages/theme/getStore.ts Outdated Show resolved Hide resolved
packages/theme/getStore.ts Outdated Show resolved Hide resolved
supportedLocales: string
}

const apiRoute = '/api/v2/storefront/store';
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm still thinking about this tight coupling of the API route. getStore returns Store based on the given StoreConfig. This gives us enough flexibility to provide our custom base-url of the store-api, however, not enough to have custom route, which could be limiting in some edge cases.

I'd rather see passing the API route as one of the StoreConfig parameters. We could make it optional with /api/v2/storefront/store as a default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Placing that apiRoute in the nuxt.config would be also easier to find if someone wanted to modify it. On the other hand, I don't think this endpoint will change since it's specific to how Spree is constructed internally.

I'll move it to nuxt.config and we can rethink it later.

packages/theme/getStore.ts Outdated Show resolved Hide resolved
packages/theme/nuxt.config.js Show resolved Hide resolved
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.

None yet

2 participants