Skip to content

feat: validation of locale for a given hostname - #3156

Merged
lariciamota merged 5 commits into
feat/multilanguagefrom
feat/domains-locale-strategy
Dec 24, 2025
Merged

lariciamota merged 5 commits into
feat/multilanguagefrom
feat/domains-locale-strategy

Conversation

@lariciamota

@lariciamota lariciamota commented Dec 15, 2025 •

Copy link
Copy Markdown
Contributor

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:

  • Subdomain bindings: A hostname dedicated to a locale (e.g., canada.brandless.fast.store → fr-CA)
  • Path-based bindings: A hostname that serves multiple locales via paths (e.g., 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, as fr-CA is only bound to canada.brandless.fast.store
  • canada.brandless.fast.store/en-US/products → should be 404, as en-US is not bound to this hostname

How 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/*)

  • When: Page uses getServerSideProps
  • Where: Server-side before rendering
  • How: withLocaleValidationSSR HOC wraps getServerSideProps
  • Result: Returns notFound: true → Next.js serves 404 page
  • Benefit: User never sees the invalid page, better SEO

SSG Pages (e.g., PLPs, PDPs)

  • When: Page uses getStaticProps (pre-generated at build time)
  • Where: Client-side after hydration
  • How: useEffect in _app.tsx validates after mount
  • Result: Redirects to /404 with original path in query string
  • Trade-off: Page loads before redirect, but SSG pages remain fast

ps.: 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

  1. Localhost bypass: Allows all locales on localhost and 127.0.0.1 for development tests
  2. Binding check: Verifies if the locale has a binding for the hostname
    • Checks all bindings for the given locale
    • Matches if binding.url hostname equals the request hostname

How to test it?

Configure your etc/hosts like explained in the previous PR, then test navigating through the store and changing domains/paths.
In the discovery.config.default, put enableFaststoreMyAccount as true to test the /pvt/account pages.

Example Scenarios (based on mock)

Valid Combinations ✅

URL Type Result
brandless.fast.store:3000/pt-BR/apparel Path-based ✅ Valid
brandless.fast.store.com.br:3000/pt-BR Subdomain ✅ Valid
canada.brandless.fast.store:3000/fr-CA Subdomain ✅ Valid
brandless.fast.store:3000/en-US/pvt/account Path-based (SSR) ✅ Valid

Invalid Combinations ❌

URL Why Invalid Result
brandless.fast.store:3000/fr-CA fr-CA only bound to canada subdomain ❌ 404 (Client)
canada.brandless.fast.store:3000/en-US/apparel en-US not bound to canada subdomain ❌ 404 (Client)
brandless.fast.store.com:3000/pt-BR/apparel pt-BR not bound to .com ❌ 404 (Client)
canada.brandless.fast.store:3000/pt-BR/pvt/account pt-BR not bound to canada ❌ 404 (SSR)

References

@lariciamota lariciamota self-assigned this Dec 15, 2025
@lariciamota lariciamota added the enhancement New feature or request label Dec 15, 2025
@codesandbox-ci

codesandbox-ci Bot commented Dec 15, 2025 •

Copy link
Copy Markdown

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.

@lariciamota
lariciamota force-pushed the feat/domains-locale-strategy branch from 673b34e to 039a939 Compare December 15, 2025 22:54
@lariciamota
lariciamota marked this pull request as ready for review December 15, 2025 23:56
@lariciamota
lariciamota requested a review from a team as a code owner December 15, 2025 23:56
@lariciamota
lariciamota requested review from lemagnetic and ommeirelles and removed request for a team December 15, 2025 23:56

@eduardoformiga eduardoformiga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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) ?

Comment thread packages/core/src/pages/_app.tsx Resolved
@lariciamota

Copy link
Copy Markdown
Contributor Author

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 getStaticProps :/ That's why for the pages that use getStaticProps we'll rely on redirecting from client side, unfortunately

@eduardoformiga eduardoformiga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good job!

@hellofanny hellofanny left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice!!! 🥳 🚀

I think with the multilanguage feature flag, we could check if no enabled we could skip this validation right?

image

@lariciamota

Copy link
Copy Markdown
Contributor Author

I think with the multilanguage feature flag, we could check if no enabled we could skip this validation right?

Yes, good point! I've added to the description of the feature flag task so we remember that.

@lariciamota
lariciamota merged commit e99f2de into feat/multilanguage Dec 24, 2025
@lariciamota
lariciamota deleted the feat/domains-locale-strategy branch December 24, 2025 13:28
@lariciamota
lariciamota restored the feat/domains-locale-strategy branch January 12, 2026 13:59
@lariciamota
lariciamota deleted the feat/domains-locale-strategy branch January 12, 2026 13:59
lemagnetic added a commit that referenced this pull request Mar 5, 2026
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants