From a46249173edde66b03c19441144272baa8394fb4 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 21 Feb 2024 07:42:06 +0000 Subject: [PATCH] fix(i18n): move transformation outside the user schema (#10169) * fix(i18n): move transformation outside the user schema * fix condition * cleanup * ops * fix regression --- .changeset/loud-snakes-behave.md | 5 + packages/astro/src/core/app/index.ts | 6 +- packages/astro/src/core/app/types.ts | 4 +- packages/astro/src/core/build/generate.ts | 3 +- .../src/core/build/plugins/plugin-manifest.ts | 12 +- packages/astro/src/core/config/schema.ts | 65 +---- packages/astro/src/core/endpoint/index.ts | 2 +- packages/astro/src/core/render-context.ts | 8 +- packages/astro/src/core/render/result.ts | 6 +- .../astro/src/core/routing/manifest/create.ts | 8 +- packages/astro/src/i18n/index.ts | 16 +- packages/astro/src/i18n/middleware.ts | 6 +- packages/astro/src/i18n/utils.ts | 39 ++- packages/astro/src/virtual-modules/i18n.ts | 13 +- .../src/vite-plugin-astro-server/plugin.ts | 3 +- .../astro/test/units/i18n/astro_i18n.test.js | 224 +++++++++++------- 16 files changed, 229 insertions(+), 191 deletions(-) create mode 100644 .changeset/loud-snakes-behave.md diff --git a/.changeset/loud-snakes-behave.md b/.changeset/loud-snakes-behave.md new file mode 100644 index 000000000000..f9f077f6e0c3 --- /dev/null +++ b/.changeset/loud-snakes-behave.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Fixes an issue with the `i18n.routing` types, where an internal transformation was causing the generation of incorrect types for integrations. diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 094ac97b56fa..74bd4ecbb6f6 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -166,9 +166,9 @@ export class App { if ( this.#manifest.i18n && - (this.#manifest.i18n.routing === 'domains-prefix-always' || - this.#manifest.i18n.routing === 'domains-prefix-other-locales' || - this.#manifest.i18n.routing === 'domains-prefix-always-no-redirect') + (this.#manifest.i18n.strategy === 'domains-prefix-always' || + this.#manifest.i18n.strategy === 'domains-prefix-other-locales' || + this.#manifest.i18n.strategy === 'domains-prefix-always-no-redirect') ) { // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host let host = request.headers.get('X-Forwarded-Host'); diff --git a/packages/astro/src/core/app/types.ts b/packages/astro/src/core/app/types.ts index 17b7c0872b0f..740165710cb0 100644 --- a/packages/astro/src/core/app/types.ts +++ b/packages/astro/src/core/app/types.ts @@ -8,7 +8,7 @@ import type { SSRResult, } from '../../@types/astro.js'; import type { SinglePageBuiltModule } from '../build/types.js'; -import type { RoutingStrategies } from '../config/schema.js'; +import type { RoutingStrategies } from '../../i18n/utils.js'; export type ComponentPath = string; @@ -60,7 +60,7 @@ export type SSRManifest = { export type SSRManifestI18n = { fallback?: Record; - routing: RoutingStrategies; + strategy: RoutingStrategies; locales: Locales; defaultLocale: string; domainLookupTable: Record; diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 29a32a88b780..e27f71fccd18 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -60,6 +60,7 @@ import type { import { getTimeStat, shouldAppendForwardSlash } from './util.js'; import { NoPrerenderedRoutesWithDomains } from '../errors/errors-data.js'; import { RenderContext } from '../render-context.js'; +import { toRoutingStrategy } from '../../i18n/utils.js'; function createEntryURL(filePath: string, outFolder: URL) { return new URL('./' + filePath + `?time=${Date.now()}`, outFolder); @@ -582,7 +583,7 @@ function createBuildManifest( if (settings.config.i18n) { i18nManifest = { fallback: settings.config.i18n.fallback, - routing: settings.config.i18n.routing, + strategy: toRoutingStrategy(settings.config.i18n), defaultLocale: settings.config.i18n.defaultLocale, locales: settings.config.i18n.locales, domainLookupTable: {}, diff --git a/packages/astro/src/core/build/plugins/plugin-manifest.ts b/packages/astro/src/core/build/plugins/plugin-manifest.ts index 8b97651dbfbf..35c3632d02db 100644 --- a/packages/astro/src/core/build/plugins/plugin-manifest.ts +++ b/packages/astro/src/core/build/plugins/plugin-manifest.ts @@ -17,6 +17,7 @@ import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin.js'; import type { StaticBuildOptions } from '../types.js'; import { normalizeTheLocale } from '../../../i18n/index.js'; +import { toRoutingStrategy } from '../../../i18n/utils.js'; const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@'; const replaceExp = new RegExp(`['"]${manifestReplace}['"]`, 'g'); @@ -235,14 +236,7 @@ function buildManifest( * logic meant for i18n domain support, where we fill the lookup table */ const i18n = settings.config.i18n; - if ( - settings.config.experimental.i18nDomains && - i18n && - i18n.domains && - (i18n.routing === 'domains-prefix-always' || - i18n.routing === 'domains-prefix-other-locales' || - i18n.routing === 'domains-prefix-always-no-redirect') - ) { + if (settings.config.experimental.i18nDomains && i18n && i18n.domains) { for (const [locale, domainValue] of Object.entries(i18n.domains)) { domainLookupTable[domainValue] = normalizeTheLocale(locale); } @@ -257,7 +251,7 @@ function buildManifest( if (settings.config.i18n) { i18nManifest = { fallback: settings.config.i18n.fallback, - routing: settings.config.i18n.routing, + strategy: toRoutingStrategy(settings.config.i18n), locales: settings.config.i18n.locales, defaultLocale: settings.config.i18n.defaultLocale, domainLookupTable, diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index e840f93a91cd..f62e322c5bb8 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -66,14 +66,6 @@ const ASTRO_CONFIG_DEFAULTS = { }, } satisfies AstroUserConfig & { server: { open: boolean } }; -export type RoutingStrategies = - | 'pathname-prefix-always' - | 'pathname-prefix-other-locales' - | 'pathname-prefix-always-no-redirect' - | 'domains-prefix-always' - | 'domains-prefix-other-locales' - | 'domains-prefix-always-no-redirect'; - export const AstroConfigSchema = z.object({ root: z .string() @@ -363,37 +355,6 @@ export const AstroConfigSchema = z.object({ ), }) .optional() - .transform((i18n) => { - if (i18n) { - let { routing, domains } = i18n; - let strategy: RoutingStrategies; - const hasDomains = domains ? Object.keys(domains).length > 0 : false; - if (!hasDomains) { - if (routing.prefixDefaultLocale === true) { - if (routing.redirectToDefaultLocale) { - strategy = 'pathname-prefix-always'; - } else { - strategy = 'pathname-prefix-always-no-redirect'; - } - } else { - strategy = 'pathname-prefix-other-locales'; - } - } else { - if (routing.prefixDefaultLocale === true) { - if (routing.redirectToDefaultLocale) { - strategy = 'domains-prefix-always'; - } else { - strategy = 'domains-prefix-always-no-redirect'; - } - } else { - strategy = 'domains-prefix-other-locales'; - } - } - - return { ...i18n, routing: strategy }; - } - return undefined; - }) .superRefine((i18n, ctx) => { if (i18n) { const { defaultLocale, locales: _locales, fallback, domains, routing } = i18n; @@ -436,20 +397,15 @@ export const AstroConfigSchema = z.object({ } if (domains) { const entries = Object.entries(domains); - if (entries.length > 0) { - if ( - routing !== 'domains-prefix-other-locales' && - routing !== 'domains-prefix-always-no-redirect' && - routing !== 'domains-prefix-always' - ) { - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: `When specifying some domains, the property \`i18n.routingStrategy\` must be set to \`"domains"\`.`, - }); - } + const hasDomains = domains ? Object.keys(domains).length > 0 : false; + if (entries.length > 0 && !hasDomains) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `When specifying some domains, the property \`i18n.routingStrategy\` must be set to \`"domains"\`.`, + }); } - for (const [domainKey, domainValue] of Object.entries(domains)) { + for (const [domainKey, domainValue] of entries) { if (!locales.includes(domainKey)) { ctx.addIssue({ code: z.ZodIssueCode.custom, @@ -625,11 +581,8 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) { .superRefine((configuration, ctx) => { const { site, experimental, i18n, output } = configuration; if (experimental.i18nDomains) { - if ( - i18n?.routing === 'domains-prefix-other-locales' || - i18n?.routing === 'domains-prefix-always-no-redirect' || - i18n?.routing === 'domains-prefix-always' - ) { + const hasDomains = i18n?.domains ? Object.keys(i18n.domains).length > 0 : false; + if (hasDomains) { if (!site) { ctx.addIssue({ code: z.ZodIssueCode.custom, diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index 54b5dff4769d..d65b5e6c06f0 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -6,8 +6,8 @@ import { computeCurrentLocale, computePreferredLocale, computePreferredLocaleList, + type RoutingStrategies, } from '../../i18n/utils.js'; -import type { RoutingStrategies } from '../config/schema.js'; type CreateAPIContext = { request: Request; diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index 299c188aff3a..c0cc55c81d20 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -190,7 +190,7 @@ export class RenderContext { const { links, scripts, styles } = await pipeline.headElements(routeData); const componentMetadata = (await pipeline.componentMetadata(routeData)) ?? manifest.componentMetadata; - const { defaultLocale, locales, routing: routingStrategy } = i18n ?? {}; + const { defaultLocale, locales, strategy } = i18n ?? {}; const partial = Boolean(mod.partial); return createResult({ adapterName, @@ -210,7 +210,7 @@ export class RenderContext { resolve, request, route: routeData.route, - routingStrategy, + strategy, site, scripts, ssr: serverLike, @@ -238,9 +238,9 @@ export class RenderContext { preferredLocale: undefined, preferredLocaleList: undefined, }; - const { defaultLocale, locales, routing } = i18n; + const { defaultLocale, locales, strategy } = i18n; return (this.#i18nData = { - currentLocale: computeCurrentLocale(routeData.route, locales, routing, defaultLocale), + currentLocale: computeCurrentLocale(routeData.route, locales, strategy, defaultLocale), preferredLocale: computePreferredLocale(request, locales), preferredLocaleList: computePreferredLocaleList(request, locales), }); diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 6c1314f3c782..cc7fa219e9c2 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -17,8 +17,8 @@ import { computeCurrentLocale, computePreferredLocale, computePreferredLocaleList, + type RoutingStrategies, } from '../../i18n/utils.js'; -import type { RoutingStrategies } from '../config/schema.js'; import { clientAddressSymbol, responseSentSymbol } from '../constants.js'; export interface CreateResultArgs { @@ -53,7 +53,7 @@ export interface CreateResultArgs { locales: Locales | undefined; defaultLocale: string | undefined; route: string; - routingStrategy: RoutingStrategies | undefined; + strategy: RoutingStrategies | undefined; } function getFunctionExpression(slot: any) { @@ -234,7 +234,7 @@ export function createResult(args: CreateResultArgs): SSRResult { currentLocale = computeCurrentLocale( url.pathname, args.locales, - args.routingStrategy, + args.strategy, args.defaultLocale ); if (currentLocale) { diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index 93b3770b1d60..804cacc83fde 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -20,6 +20,7 @@ import { AstroError } from '../../errors/index.js'; import { removeLeadingForwardSlash, slash } from '../../path.js'; import { resolvePages } from '../../util.js'; import { getRouteGenerator } from './generator.js'; +import { toRoutingStrategy } from '../../../i18n/utils.js'; const require = createRequire(import.meta.url); interface Item { @@ -686,8 +687,9 @@ export function createRouteManifest( const i18n = settings.config.i18n; if (i18n) { + const strategy = toRoutingStrategy(i18n); // First we check if the user doesn't have an index page. - if (i18n.routing === 'pathname-prefix-always') { + if (strategy === 'pathname-prefix-always') { let index = routes.find((route) => route.route === '/'); if (!index) { let relativePath = path.relative( @@ -755,7 +757,7 @@ export function createRouteManifest( // Work done, now we start creating "fallback" routes based on the configuration - if (i18n.routing === 'pathname-prefix-always') { + if (strategy === 'pathname-prefix-always') { // we attempt to retrieve the index page of the default locale const defaultLocaleRoutes = routesByLocale.get(i18n.defaultLocale); if (defaultLocaleRoutes) { @@ -830,7 +832,7 @@ export function createRouteManifest( let route: string; if ( fallbackToLocale === i18n.defaultLocale && - i18n.routing === 'pathname-prefix-other-locales' + strategy === 'pathname-prefix-other-locales' ) { if (fallbackToRoute.pathname) { pathname = `/${fallbackFromLocale}${fallbackToRoute.pathname}`; diff --git a/packages/astro/src/i18n/index.ts b/packages/astro/src/i18n/index.ts index 4d66a56c3fc8..0b93f1d785b7 100644 --- a/packages/astro/src/i18n/index.ts +++ b/packages/astro/src/i18n/index.ts @@ -3,7 +3,7 @@ import type { AstroConfig, Locales } from '../@types/astro.js'; import { shouldAppendForwardSlash } from '../core/build/util.js'; import { MissingLocale } from '../core/errors/errors-data.js'; import { AstroError } from '../core/errors/index.js'; -import type { RoutingStrategies } from '../core/config/schema.js'; +import type { RoutingStrategies } from './utils.js'; type GetLocaleRelativeUrl = GetLocaleOptions & { locale: string; @@ -11,7 +11,7 @@ type GetLocaleRelativeUrl = GetLocaleOptions & { locales: Locales; trailingSlash: AstroConfig['trailingSlash']; format: AstroConfig['build']['format']; - routing?: RoutingStrategies; + strategy?: RoutingStrategies; defaultLocale: string; domains: Record | undefined; path?: string; @@ -45,7 +45,7 @@ export function getLocaleRelativeUrl({ path, prependWith, normalizeLocale = true, - routing = 'pathname-prefix-other-locales', + strategy = 'pathname-prefix-other-locales', defaultLocale, }: GetLocaleRelativeUrl) { const codeToUse = peekCodePathToUse(_locales, locale); @@ -58,10 +58,10 @@ export function getLocaleRelativeUrl({ const pathsToJoin = [base, prependWith]; const normalizedLocale = normalizeLocale ? normalizeTheLocale(codeToUse) : codeToUse; if ( - routing === 'pathname-prefix-always' || - routing === 'pathname-prefix-always-no-redirect' || - routing === 'domains-prefix-always' || - routing === 'domains-prefix-always-no-redirect' + strategy === 'pathname-prefix-always' || + strategy === 'pathname-prefix-always-no-redirect' || + strategy === 'domains-prefix-always' || + strategy === 'domains-prefix-always-no-redirect' ) { pathsToJoin.push(normalizedLocale); } else if (locale !== defaultLocale) { @@ -107,7 +107,7 @@ interface GetLocalesRelativeUrlList extends GetLocaleOptions { locales: Locales; trailingSlash: AstroConfig['trailingSlash']; format: AstroConfig['build']['format']; - routing?: RoutingStrategies; + strategy?: RoutingStrategies; defaultLocale: string; domains: Record | undefined; } diff --git a/packages/astro/src/i18n/middleware.ts b/packages/astro/src/i18n/middleware.ts index 815ac2e9c8a1..c3f85064b7d0 100644 --- a/packages/astro/src/i18n/middleware.ts +++ b/packages/astro/src/i18n/middleware.ts @@ -106,9 +106,9 @@ export function createI18nMiddleware( } const { url, currentLocale } = context; - const { locales, defaultLocale, fallback, routing } = i18n; + const { locales, defaultLocale, fallback, strategy } = i18n; - switch (i18n.routing) { + switch (i18n.strategy) { case 'domains-prefix-other-locales': { if (localeHasntDomain(i18n, currentLocale)) { const result = prefixOtherLocales(url, response); @@ -187,7 +187,7 @@ export function createI18nMiddleware( let newPathname: string; // If a locale falls back to the default locale, we want to **remove** the locale because // the default locale doesn't have a prefix - if (pathFallbackLocale === defaultLocale && routing === 'pathname-prefix-other-locales') { + if (pathFallbackLocale === defaultLocale && strategy === 'pathname-prefix-other-locales') { newPathname = url.pathname.replace(`/${urlLocale}`, ``); } else { newPathname = url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`); diff --git a/packages/astro/src/i18n/utils.ts b/packages/astro/src/i18n/utils.ts index 4cfec633b3a2..d3dddba1f8c5 100644 --- a/packages/astro/src/i18n/utils.ts +++ b/packages/astro/src/i18n/utils.ts @@ -1,6 +1,5 @@ -import type { Locales } from '../@types/astro.js'; +import type { AstroConfig, AstroUserConfig, Locales } from '../@types/astro.js'; import { normalizeTheLocale, toCodes } from './index.js'; -import type { RoutingStrategies } from '../core/config/schema.js'; type BrowserLocale = { locale: string; @@ -190,3 +189,39 @@ export function computeCurrentLocale( } return undefined; } + +export type RoutingStrategies = + | 'pathname-prefix-always' + | 'pathname-prefix-other-locales' + | 'pathname-prefix-always-no-redirect' + | 'domains-prefix-always' + | 'domains-prefix-other-locales' + | 'domains-prefix-always-no-redirect'; +export function toRoutingStrategy(i18n: NonNullable) { + let { routing, domains } = i18n; + let strategy: RoutingStrategies; + const hasDomains = domains ? Object.keys(domains).length > 0 : false; + if (!hasDomains) { + if (routing?.prefixDefaultLocale === true) { + if (routing.redirectToDefaultLocale) { + strategy = 'pathname-prefix-always'; + } else { + strategy = 'pathname-prefix-always-no-redirect'; + } + } else { + strategy = 'pathname-prefix-other-locales'; + } + } else { + if (routing?.prefixDefaultLocale === true) { + if (routing.redirectToDefaultLocale) { + strategy = 'domains-prefix-always'; + } else { + strategy = 'domains-prefix-always-no-redirect'; + } + } else { + strategy = 'domains-prefix-other-locales'; + } + } + + return strategy; +} diff --git a/packages/astro/src/virtual-modules/i18n.ts b/packages/astro/src/virtual-modules/i18n.ts index f71068667787..3358a880b23c 100644 --- a/packages/astro/src/virtual-modules/i18n.ts +++ b/packages/astro/src/virtual-modules/i18n.ts @@ -1,13 +1,16 @@ import * as I18nInternals from '../i18n/index.js'; import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js'; +import { toRoutingStrategy } from '../i18n/utils.js'; export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js'; const { trailingSlash, format, site, i18n, isBuild } = // @ts-expect-error __ASTRO_INTERNAL_I18N_CONFIG__ as I18nInternalConfig; -const { defaultLocale, locales, routing, domains } = i18n!; +const { defaultLocale, locales, domains } = i18n!; const base = import.meta.env.BASE_URL; +const routing = toRoutingStrategy(i18n!); + export type GetLocaleOptions = I18nInternals.GetLocaleOptions; /** @@ -40,7 +43,7 @@ export const getRelativeLocaleUrl = (locale: string, path?: string, options?: Ge format, defaultLocale, locales, - routing, + strategy: routing, domains, ...options, }); @@ -80,7 +83,7 @@ export const getAbsoluteLocaleUrl = (locale: string, path?: string, options?: Ge site, defaultLocale, locales, - routing, + strategy: routing, domains, isBuild, ...options, @@ -100,7 +103,7 @@ export const getRelativeLocaleUrlList = (path?: string, options?: GetLocaleOptio format, defaultLocale, locales, - routing, + strategy: routing, domains, ...options, }); @@ -120,7 +123,7 @@ export const getAbsoluteLocaleUrlList = (path?: string, options?: GetLocaleOptio format, defaultLocale, locales, - routing, + strategy: routing, domains, isBuild, ...options, diff --git a/packages/astro/src/vite-plugin-astro-server/plugin.ts b/packages/astro/src/vite-plugin-astro-server/plugin.ts index e149acad0a15..bea6d75ef0e4 100644 --- a/packages/astro/src/vite-plugin-astro-server/plugin.ts +++ b/packages/astro/src/vite-plugin-astro-server/plugin.ts @@ -16,6 +16,7 @@ import { AsyncLocalStorage } from 'node:async_hooks'; import { IncomingMessage } from 'node:http'; import { setRouteError } from './server-state.js'; import { recordServerError } from './error.js'; +import { toRoutingStrategy } from '../i18n/utils.js'; export interface AstroPluginOptions { settings: AstroSettings; @@ -117,7 +118,7 @@ export function createDevelopmentManifest(settings: AstroSettings): SSRManifest if (settings.config.i18n) { i18nManifest = { fallback: settings.config.i18n.fallback, - routing: settings.config.i18n.routing, + strategy: toRoutingStrategy(settings.config.i18n), defaultLocale: settings.config.i18n.defaultLocale, locales: settings.config.i18n.locales, domainLookupTable: {}, diff --git a/packages/astro/test/units/i18n/astro_i18n.test.js b/packages/astro/test/units/i18n/astro_i18n.test.js index d7702f7fd0d3..0e8fdb4b5c89 100644 --- a/packages/astro/test/units/i18n/astro_i18n.test.js +++ b/packages/astro/test/units/i18n/astro_i18n.test.js @@ -10,6 +10,7 @@ import * as assert from 'node:assert/strict'; import { validateConfig } from '../../../dist/core/config/config.js'; import { AstroError } from '#astro/core/errors/index'; import { MissingLocale } from '#astro/core/errors/errors-data'; +import { toRoutingStrategy } from '#astro/i18n/utils'; describe('getLocaleRelativeUrl', () => { it('should correctly return the URL with the base', () => { @@ -231,11 +232,9 @@ describe('getLocaleRelativeUrl', () => { */ const config = { base: '/blog', - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'en_US', 'en_AU'], - }, + i18n: { + defaultLocale: 'en', + locales: ['en', 'en_US', 'en_AU'], }, }; @@ -243,9 +242,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'en_US', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), '/blog/en-us/' ); @@ -254,10 +254,11 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'en_US', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', normalizeLocale: false, + strategy: toRoutingStrategy(config.i18n), }), '/blog/en_US/' ); @@ -266,9 +267,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'en_AU', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), '/blog/en-au/' ); @@ -281,11 +283,11 @@ describe('getLocaleRelativeUrl', () => { */ const config = { base: '/blog', - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'es', 'en_US', 'en_AU'], - routing: 'pathname-prefix-always', + i18n: { + defaultLocale: 'en', + locales: ['en', 'es', 'en_US', 'en_AU'], + routing: { + prefixDefaultLocale: true, }, }, }; @@ -297,7 +299,8 @@ describe('getLocaleRelativeUrl', () => { base: '/blog/', trailingSlash: 'always', format: 'directory', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), }), '/blog/en/' ); @@ -305,9 +308,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), '/blog/es/' ); @@ -317,9 +321,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), '/blog/en/' ); @@ -327,9 +332,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), '/blog/es/' ); @@ -342,11 +348,12 @@ describe('getLocaleRelativeUrl', () => { */ const config = { base: '/blog', - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'es', 'en_US', 'en_AU'], - routing: 'pathname-prefix-always-no-redirect', + i18n: { + defaultLocale: 'en', + locales: ['en', 'es', 'en_US', 'en_AU'], + routing: { + prefixDefaultLocale: true, + redirectToDefaultLocale: false, }, }, }; @@ -358,7 +365,8 @@ describe('getLocaleRelativeUrl', () => { base: '/blog/', trailingSlash: 'always', format: 'directory', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), }), '/blog/en/' ); @@ -366,9 +374,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), '/blog/es/' ); @@ -378,9 +387,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), '/blog/en/' ); @@ -388,9 +398,10 @@ describe('getLocaleRelativeUrl', () => { getLocaleRelativeUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), '/blog/es/' ); @@ -576,11 +587,11 @@ describe('getLocaleRelativeUrlList', () => { * @type {import("../../../dist/@types").AstroUserConfig} */ const config = { - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'en_US', 'es'], - routing: 'pathname-prefix-always', + i18n: { + defaultLocale: 'en', + locales: ['en', 'en_US', 'es'], + routing: { + prefixDefaultLocale: true, }, }, }; @@ -589,9 +600,10 @@ describe('getLocaleRelativeUrlList', () => { getLocaleRelativeUrlList({ locale: 'en', base: '/blog', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'never', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), ['/blog/en', '/blog/en-us', '/blog/es'] ); @@ -603,11 +615,12 @@ describe('getLocaleRelativeUrlList', () => { * @type {import("../../../dist/@types").AstroUserConfig} */ const config = { - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'en_US', 'es'], - routing: 'pathname-prefix-always-no-redirect', + i18n: { + defaultLocale: 'en', + locales: ['en', 'en_US', 'es'], + routing: { + prefixDefaultLocale: true, + redirectToDefaultLocale: false, }, }, }; @@ -616,9 +629,10 @@ describe('getLocaleRelativeUrlList', () => { getLocaleRelativeUrlList({ locale: 'en', base: '/blog', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'never', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), ['/blog/en', '/blog/en-us', '/blog/es'] ); @@ -795,14 +809,14 @@ describe('getLocaleAbsoluteUrl', () => { */ const config = { base: '/blog', - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'en_US', 'es'], - domains: { - es: 'https://es.example.com', - }, - routing: 'pathname-prefix-always', + i18n: { + defaultLocale: 'en', + locales: ['en', 'en_US', 'es'], + domains: { + es: 'https://es.example.com', + }, + routing: { + prefixDefaultLocale: true, }, }, }; @@ -815,7 +829,8 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'always', format: 'directory', site: 'https://example.com', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -824,10 +839,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -837,10 +853,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -848,10 +865,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -860,11 +878,12 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', site: 'https://example.com', isBuild: true, + strategy: toRoutingStrategy(config.i18n), }), 'https://es.example.com/blog/' ); @@ -874,12 +893,13 @@ describe('getLocaleAbsoluteUrl', () => { locale: 'es', base: '/blog/', prependWith: 'some-name', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'file', site: 'https://example.com', path: 'first-post', isBuild: true, + strategy: toRoutingStrategy(config.i18n), }), 'https://es.example.com/blog/some-name/first-post/' ); @@ -890,11 +910,11 @@ describe('getLocaleAbsoluteUrl', () => { * @type {import("../../../dist/@types").AstroUserConfig} */ const config = { - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'es'], - routing: 'pathname-prefix-always', + i18n: { + defaultLocale: 'en', + locales: ['en', 'es'], + routing: { + prefixDefaultLocale: true, }, }, }; @@ -903,10 +923,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'en', base: '/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/en/' ); @@ -914,10 +935,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/', - ...config.experimental.i18n, + ...config.i18n, trailingSlash: 'always', format: 'directory', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/es/' ); @@ -932,7 +954,9 @@ describe('getLocaleAbsoluteUrl', () => { i18n: { defaultLocale: 'en', locales: ['en', 'es'], - routing: 'pathname-prefix-always', + routing: { + prefixDefaultLocale: true, + }, }, }; // directory format @@ -944,6 +968,7 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'never', format: 'directory', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en' ); @@ -955,6 +980,7 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'always', format: 'directory', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -967,6 +993,7 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'ignore', format: 'directory', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -980,6 +1007,7 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'never', format: 'file', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en' ); @@ -991,6 +1019,7 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'always', format: 'file', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -1004,6 +1033,7 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'ignore', format: 'file', site: 'https://example.com', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en' ); @@ -1067,11 +1097,11 @@ describe('getLocaleAbsoluteUrl', () => { */ const config = { base: '/blog', - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'es', 'en_US', 'en_AU'], - routing: 'pathname-prefix-always', + i18n: { + defaultLocale: 'en', + locales: ['en', 'es', 'en_US', 'en_AU'], + routing: { + prefixDefaultLocale: true, }, }, }; @@ -1084,7 +1114,8 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'always', site: 'https://example.com', format: 'directory', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -1092,10 +1123,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, site: 'https://example.com', trailingSlash: 'always', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -1105,10 +1137,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, site: 'https://example.com', trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -1116,10 +1149,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, site: 'https://example.com', trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -1132,11 +1166,12 @@ describe('getLocaleAbsoluteUrl', () => { */ const config = { base: '/blog', - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'es', 'en_US', 'en_AU'], - routing: 'pathname-prefix-always-no-redirect', + i18n: { + defaultLocale: 'en', + locales: ['en', 'es', 'en_US', 'en_AU'], + routing: { + prefixDefaultLocale: true, + redirectToDefaultLocale: false, }, }, }; @@ -1149,7 +1184,8 @@ describe('getLocaleAbsoluteUrl', () => { trailingSlash: 'always', site: 'https://example.com', format: 'directory', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -1157,10 +1193,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, site: 'https://example.com', trailingSlash: 'always', format: 'directory', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -1170,10 +1207,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, site: 'https://example.com', trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/en/' ); @@ -1181,10 +1219,11 @@ describe('getLocaleAbsoluteUrl', () => { getLocaleAbsoluteUrl({ locale: 'es', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, site: 'https://example.com', trailingSlash: 'always', format: 'file', + strategy: toRoutingStrategy(config.i18n), }), 'https://example.com/blog/es/' ); @@ -1491,6 +1530,7 @@ describe('getLocaleAbsoluteUrlList', () => { path: 'download', ...config, ...config.i18n, + strategy: toRoutingStrategy(config.i18n), }), [ 'https://example.com/en/download/', @@ -1530,6 +1570,7 @@ describe('getLocaleAbsoluteUrlList', () => { path: 'download', ...config, ...config.i18n, + strategy: toRoutingStrategy(config.i18n), isBuild: true, }), [ @@ -1671,11 +1712,11 @@ describe('getLocaleAbsoluteUrlList', () => { * @type {import("../../../dist/@types").AstroUserConfig} */ const config = { - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'en_US', 'es'], - routing: 'pathname-prefix-always', + i18n: { + defaultLocale: 'en', + locales: ['en', 'en_US', 'es'], + routing: { + prefixDefaultLocale: true, }, }, }; @@ -1684,7 +1725,8 @@ describe('getLocaleAbsoluteUrlList', () => { getLocaleAbsoluteUrlList({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), trailingSlash: 'ignore', format: 'directory', site: 'https://example.com', @@ -1703,11 +1745,12 @@ describe('getLocaleAbsoluteUrlList', () => { * @type {import("../../../dist/@types").AstroUserConfig} */ const config = { - experimental: { - i18n: { - defaultLocale: 'en', - locales: ['en', 'en_US', 'es'], - routing: 'pathname-prefix-always-no-redirect', + i18n: { + defaultLocale: 'en', + locales: ['en', 'en_US', 'es'], + routing: { + prefixDefaultLocale: true, + redirectToDefaultLocale: false, }, }, }; @@ -1716,7 +1759,8 @@ describe('getLocaleAbsoluteUrlList', () => { getLocaleAbsoluteUrlList({ locale: 'en', base: '/blog/', - ...config.experimental.i18n, + ...config.i18n, + strategy: toRoutingStrategy(config.i18n), trailingSlash: 'ignore', format: 'directory', site: 'https://example.com',