Repository navigation
feat: validation of locale for a given hostname - #3156
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
673b34e to
039a939
Compare
eduardoformiga
left a comment
There was a problem hiding this comment.
Nice! I tested the scenarios with the hostnames from the PR description, and it worked as described.
We won't have the hostname from the SDK or the platform in the future so that we can move the function to getStaticProps (SSG) instead of Client Side (_app.tsx) ?
The problem is that we don't have access to the current hostname inside |
Yes, good point! I've added to the description of the feature flag task so we remember that. |
## What's the purpose of this pull request? Removing hardcoded labels: - #3099 - #3102 - #3107 - #3104 - #3103 - #3125 - #3131 - #3116 - #3142 - #3192 Setting Up locales - #3141 - #3143 - #3147 - #3156 - #3162 - #3145 - #3164 - #3169 - #3171 - #3176 - #3180 - #3169 custom path - #3185 - #3162 - #3156 - #3170 Components - #3093 - #3175 Fetching content - #3165 Others - #3176 - #3171 - #3181 - #3189 - #3205 ## How to test locally? - Run `pnpm I` (install all the new dependencies and needed packages) - If you want to try localization feature, go to`/discovery.config.default.js`: Enable localization: ``` localization: { enabled: true } ``` - Run `node packages/cli/bin/run.js generate-i18n ./packages/core` to fetch the bindings If the contentSource is not set to content platform yet, you will see a message to update to it. > contentSource: { > type: 'CP', > },⚠️ Note: Ensure the url is set to localhost for local testing. These locale bindings are generated in `discovery.config` after running the generate-i18n command. ```diff localization: { enabled: true, defaultLocale: 'en-US', regions: {...}, locales: { 'pt-BR': { ... bindings: [ { currencyCode: 'BRL', - url: 'https://brandless.fast.store/pt-BR', + url: 'http://localhost:3000/pt-BR', salesChannel: '2', isDefault: true, locales: [ { code: 'pt-BR', isDefault: true, }, ], }, ], }, 'it-IT': { ... bindings: [ { currencyCode: 'EUR', - url: 'https://brandless.fast.store/it-IT', + url: 'http://localhost:3000/it-IT', salesChannel: '3', isDefault: true, locales: [ { code: 'it-IT', isDefault: true, }, ], }, { currencyCode: 'EUR', - url: 'https://brandless.fast.store/europe/it', + url: 'http://localhost:3000/europe/it', salesChannel: '3', isDefault: false, locales: [ { code: 'it-IT', isDefault: true, }, ], }, ], }, 'en-US': { code: 'en-US', name: 'English', languageCode: 'en', languageName: 'English', script: 'Latn', textDirection: 'ltr', regionCode: 'US', bindings: [ { currencyCode: 'USD', - url: 'https://brandless.fast.store', + url: 'http://localhost:3000, salesChannel: '1', isDefault: true, locales: [ { code: 'en-US', isDefault: true, }, ], }, ], }, ... } ``` - Go to /packages/core and run `npmn dev` --------- Co-authored-by: Larícia Mota <[email protected]> Co-authored-by: Leandro Rodrigues <[email protected]> Co-authored-by: Otavio Moreira Meirelles <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

What's the purpose of this pull request?
Implements a locale/hostname validation system that ensures URLs match the correct locale configuration based on the hostname. The feature prevents invalid locale/hostname combinations and returns appropriate 404 responses when configurations don't match.
We want to support multiple locales and they can be configured with different URL structures:
canada.brandless.fast.store→fr-CA)brandless.fast.store/en-US,brandless.fast.store/it-IT)Without validation, users could access invalid combinations like:
brandless.fast.store/fr-CA/products→ should be 404, asfr-CAis only bound tocanada.brandless.fast.storecanada.brandless.fast.store/en-US/products→ should be 404, asen-USis not bound to this hostnameHow it works?
SSR & Client-Side Validation
This implementation handles both Server-Side Rendered (SSR) and Static Site Generated (SSG) pages:
SSR Pages (e.g.,
/pvt/account/*)getServerSidePropswithLocaleValidationSSRHOC wrapsgetServerSidePropsnotFound: true→ Next.js serves 404 pageSSG Pages (e.g., PLPs, PDPs)
getStaticProps(pre-generated at build time)useEffectin_app.tsxvalidates after mount/404with original path in query stringps.: The same solution from SSR doesn't work on SSG because we don't have access to hostname on
getStaticProps.ps2.: Middleware would work for this use case, but we decided to not use it to be cost efficient.
Validation logic
localhostand127.0.0.1for development testsbinding.urlhostname equals the request hostnameHow to test it?
Configure your
etc/hostslike explained in the previous PR, then test navigating through the store and changing domains/paths.In the discovery.config.default, put
enableFaststoreMyAccountastrueto test the/pvt/accountpages.Example Scenarios (based on mock)
Valid Combinations ✅
brandless.fast.store:3000/pt-BR/apparelbrandless.fast.store.com.br:3000/pt-BRcanada.brandless.fast.store:3000/fr-CAbrandless.fast.store:3000/en-US/pvt/accountInvalid Combinations ❌
brandless.fast.store:3000/fr-CAcanada.brandless.fast.store:3000/en-US/apparelbrandless.fast.store.com:3000/pt-BR/apparelcanada.brandless.fast.store:3000/pt-BR/pvt/accountReferences