Improve tree shaking and dynamic loading, fix translations in production build. Moved i18n settings to the application's side.
This commit is contained in:
@@ -11,7 +11,8 @@
|
||||
"prettier": "@kit/prettier-config",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./components": "./src/components/index.ts"
|
||||
"./components": "./src/components/index.ts",
|
||||
"./checkout": "./src/components/embedded-checkout.tsx"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@hookform/resolvers": "3.3.4",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
export * from './plan-picker';
|
||||
export * from './current-subscription-card';
|
||||
export * from './current-lifetime-order-card';
|
||||
export * from './embedded-checkout';
|
||||
export * from './billing-session-status';
|
||||
export * from './billing-portal-card';
|
||||
export * from './pricing-table';
|
||||
|
||||
@@ -24,11 +24,11 @@
|
||||
"zod": "^3.22.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/cms": "^0.1.0",
|
||||
"@kit/cms": "workspace:^",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "^0.1.0",
|
||||
"@kit/ui": "workspace:^",
|
||||
"@types/node": "^20.12.7",
|
||||
"zod": "^3.22.4"
|
||||
},
|
||||
|
||||
@@ -18,11 +18,11 @@
|
||||
"@kit/ui": "workspace:^"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/cms": "^0.1.0",
|
||||
"@kit/cms": "workspace:^",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "^0.1.0",
|
||||
"@kit/ui": "workspace:^",
|
||||
"@types/node": "^20.12.7",
|
||||
"wp-types": "^3.65.0"
|
||||
},
|
||||
|
||||
@@ -20,14 +20,14 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/billing": "workspace:^",
|
||||
"@kit/billing-gateway": "^0.1.0",
|
||||
"@kit/billing-gateway": "workspace:^",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/shared": "workspace:^",
|
||||
"@kit/stripe": "workspace:^",
|
||||
"@kit/supabase": "workspace:^",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/team-accounts": "^0.1.0",
|
||||
"@kit/team-accounts": "workspace:^",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "workspace:^",
|
||||
"@supabase/supabase-js": "^2.42.3",
|
||||
|
||||
@@ -19,16 +19,16 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@hookform/resolvers": "^3.3.4",
|
||||
"@kit/billing-gateway": "^0.1.0",
|
||||
"@kit/email-templates": "^0.1.0",
|
||||
"@kit/billing-gateway": "workspace:^",
|
||||
"@kit/email-templates": "workspace:^",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/mailers": "^0.1.0",
|
||||
"@kit/mailers": "workspace:^",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/shared": "^0.1.0",
|
||||
"@kit/supabase": "^0.1.0",
|
||||
"@kit/shared": "workspace:^",
|
||||
"@kit/supabase": "workspace:^",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "^0.1.0",
|
||||
"@kit/ui": "workspace:^",
|
||||
"@radix-ui/react-icons": "^1.3.0",
|
||||
"@supabase/supabase-js": "^2.42.3",
|
||||
"@tanstack/react-query": "5.29.0",
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
"devDependencies": {
|
||||
"@hookform/resolvers": "^3.3.4",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/next": "^0.1.0",
|
||||
"@kit/next": "workspace:^",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/supabase": "workspace:^",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
@@ -37,7 +37,7 @@
|
||||
"@makerkit/data-loader-supabase-nextjs": "^1.0.0",
|
||||
"@supabase/supabase-js": "^2.42.3",
|
||||
"@tanstack/react-query": "5.29.0",
|
||||
"@tanstack/react-table": "^8.15.3",
|
||||
"@tanstack/react-table": "^8.16.0",
|
||||
"@types/react": "^18.2.77",
|
||||
"lucide-react": "^0.367.0",
|
||||
"next": "14.2.0",
|
||||
|
||||
@@ -21,11 +21,11 @@
|
||||
"@hookform/resolvers": "^3.3.4",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/shared": "^0.1.0",
|
||||
"@kit/supabase": "^0.1.0",
|
||||
"@kit/shared": "workspace:^",
|
||||
"@kit/supabase": "workspace:^",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "^0.1.0",
|
||||
"@kit/ui": "workspace:^",
|
||||
"@marsidev/react-turnstile": "^0.5.4",
|
||||
"@radix-ui/react-icons": "^1.3.0",
|
||||
"@supabase/supabase-js": "^2.42.3",
|
||||
|
||||
@@ -17,20 +17,20 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@hookform/resolvers": "^3.3.4",
|
||||
"@kit/accounts": "^0.1.0",
|
||||
"@kit/accounts": "workspace:^",
|
||||
"@kit/billing-gateway": "workspace:*",
|
||||
"@kit/email-templates": "^0.1.0",
|
||||
"@kit/email-templates": "workspace:^",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/mailers": "^0.1.0",
|
||||
"@kit/mailers": "workspace:^",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/shared": "^0.1.0",
|
||||
"@kit/supabase": "^0.1.0",
|
||||
"@kit/shared": "workspace:^",
|
||||
"@kit/supabase": "workspace:^",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "^0.1.0",
|
||||
"@kit/ui": "workspace:^",
|
||||
"@supabase/supabase-js": "^2.42.3",
|
||||
"@tanstack/react-query": "5.29.0",
|
||||
"@tanstack/react-table": "^8.15.3",
|
||||
"@tanstack/react-table": "^8.16.0",
|
||||
"@types/react": "^18.2.77",
|
||||
"@types/react-dom": "^18.2.25",
|
||||
"class-variance-authority": "^0.7.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import type { i18n } from 'i18next';
|
||||
import type { InitOptions, i18n } from 'i18next';
|
||||
|
||||
let client: i18n;
|
||||
|
||||
@@ -10,28 +10,28 @@ type Resolver = (
|
||||
) => Promise<Record<string, string>>;
|
||||
|
||||
export function I18nProvider({
|
||||
lang,
|
||||
settings,
|
||||
children,
|
||||
resolver,
|
||||
}: React.PropsWithChildren<{
|
||||
lang: string;
|
||||
settings: InitOptions;
|
||||
resolver: Resolver;
|
||||
}>) {
|
||||
if (!client) {
|
||||
throw withI18nClient(lang, resolver);
|
||||
throw withI18nClient(settings, resolver);
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
async function withI18nClient(lang: string, resolver: Resolver) {
|
||||
async function withI18nClient(settings: InitOptions, resolver: Resolver) {
|
||||
if (typeof window !== 'undefined') {
|
||||
const { initializeI18nClient } = await import('./i18n.client');
|
||||
|
||||
client = await initializeI18nClient(lang, resolver);
|
||||
client = await initializeI18nClient(settings, resolver);
|
||||
} else {
|
||||
const { initializeServerI18n } = await import('./i18n.server');
|
||||
|
||||
client = await initializeServerI18n(lang, resolver);
|
||||
client = await initializeServerI18n(settings, resolver);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,23 @@
|
||||
import i18next, { i18n } from 'i18next';
|
||||
import i18next, { type InitOptions, i18n } from 'i18next';
|
||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||
import resourcesToBackend from 'i18next-resources-to-backend';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
|
||||
import { I18N_COOKIE_NAME, getI18nSettings } from './i18n.settings';
|
||||
|
||||
/**
|
||||
* Initialize the i18n instance on the client.
|
||||
* @param settings - the i18n settings
|
||||
* @param resolver - a function that resolves the i18n resources
|
||||
*/
|
||||
export function initializeI18nClient(
|
||||
lng: string | undefined,
|
||||
i18nResolver: (lang: string, namespace: string) => Promise<object>,
|
||||
settings: InitOptions,
|
||||
resolver: (lang: string, namespace: string) => Promise<object>,
|
||||
): Promise<i18n> {
|
||||
const settings = getI18nSettings(lng);
|
||||
|
||||
if (i18next.isInitialized) {
|
||||
return Promise.resolve(i18next);
|
||||
}
|
||||
|
||||
return new Promise<i18n>((resolve, reject) => {
|
||||
void i18next
|
||||
.use(initReactI18next)
|
||||
.use(
|
||||
resourcesToBackend(async (language, namespace, callback) => {
|
||||
const data = await i18nResolver(language, namespace);
|
||||
const data = await resolver(language, namespace);
|
||||
|
||||
return callback(null, data);
|
||||
}),
|
||||
@@ -32,7 +29,7 @@ export function initializeI18nClient(
|
||||
detection: {
|
||||
order: ['htmlTag', 'cookie', 'navigator'],
|
||||
caches: ['cookie'],
|
||||
lookupCookie: I18N_COOKIE_NAME,
|
||||
lookupCookie: 'lang',
|
||||
},
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
|
||||
@@ -1,35 +1,25 @@
|
||||
import { createInstance } from 'i18next';
|
||||
import { type InitOptions, createInstance } from 'i18next';
|
||||
import resourcesToBackend from 'i18next-resources-to-backend';
|
||||
import { initReactI18next } from 'react-i18next/initReactI18next';
|
||||
|
||||
import { I18N_COOKIE_NAME, getI18nSettings, languages } from './i18n.settings';
|
||||
|
||||
export function getLanguageCookie<
|
||||
Cookies extends {
|
||||
get: (name: string) => { value: string } | undefined;
|
||||
},
|
||||
>(cookies: Cookies) {
|
||||
return cookies.get(I18N_COOKIE_NAME)?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the i18n instance on the server.
|
||||
* This is useful for RSC and SSR.
|
||||
* @param settings - the i18n settings
|
||||
* @param resolver - a function that resolves the i18n resources
|
||||
*/
|
||||
export async function initializeServerI18n(
|
||||
lang: string | undefined,
|
||||
i18nResolver: (language: string, namespace: string) => Promise<object>,
|
||||
settings: InitOptions,
|
||||
resolver: (language: string, namespace: string) => Promise<object>,
|
||||
) {
|
||||
const i18nInstance = createInstance();
|
||||
|
||||
if (i18nInstance.isInitialized) {
|
||||
return i18nInstance;
|
||||
}
|
||||
|
||||
const settings = getI18nSettings(lang);
|
||||
|
||||
await i18nInstance
|
||||
.use(initReactI18next)
|
||||
.use(
|
||||
resourcesToBackend(async (language, namespace, callback) => {
|
||||
try {
|
||||
const data = await i18nResolver(language, namespace);
|
||||
const data = await resolver(language, namespace);
|
||||
|
||||
return callback(null, data);
|
||||
} catch (error) {
|
||||
@@ -49,7 +39,7 @@ export async function initializeServerI18n(
|
||||
|
||||
export function parseAcceptLanguageHeader(
|
||||
languageHeaderValue: string | null | undefined,
|
||||
acceptedLanguages = languages,
|
||||
acceptedLanguages: string[],
|
||||
): string[] {
|
||||
// Return an empty array if the header value is not provided
|
||||
if (!languageHeaderValue) return [];
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
import { InitOptions } from 'i18next';
|
||||
|
||||
const fallbackLng = 'en';
|
||||
|
||||
export const languages: string[] = [fallbackLng];
|
||||
|
||||
export const I18N_COOKIE_NAME = 'lang';
|
||||
|
||||
/**
|
||||
* The default array of Internationalization (i18n) namespaces.
|
||||
* These namespaces are commonly used in the application for translation purposes.
|
||||
*
|
||||
* Add your own namespaces here
|
||||
**/
|
||||
export const defaultI18nNamespaces = [
|
||||
'common',
|
||||
'auth',
|
||||
'account',
|
||||
'teams',
|
||||
'billing',
|
||||
'marketing',
|
||||
];
|
||||
|
||||
export function getI18nSettings(
|
||||
language: string | undefined,
|
||||
ns: string | string[] = defaultI18nNamespaces,
|
||||
): InitOptions {
|
||||
let lng = language ?? fallbackLng;
|
||||
|
||||
if (!languages.includes(lng)) {
|
||||
console.warn(
|
||||
`Language "${lng}" is not supported. Falling back to "${fallbackLng}"`,
|
||||
);
|
||||
|
||||
lng = fallbackLng;
|
||||
}
|
||||
|
||||
return {
|
||||
supportedLngs: languages,
|
||||
fallbackLng,
|
||||
lng,
|
||||
fallbackNS: defaultI18nNamespaces,
|
||||
defaultNS: defaultI18nNamespaces,
|
||||
ns,
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"name": "@kit/monitoring",
|
||||
"private": true,
|
||||
"sideEffects": false,
|
||||
"version": "0.1.0",
|
||||
"scripts": {
|
||||
"clean": "git clean -xdf .turbo node_modules",
|
||||
|
||||
@@ -21,10 +21,10 @@
|
||||
"zod": "^3.22.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@kit/auth": "^0.1.0",
|
||||
"@kit/auth": "workspace:^",
|
||||
"@kit/eslint-config": "workspace:*",
|
||||
"@kit/prettier-config": "workspace:*",
|
||||
"@kit/supabase": "^0.1.0",
|
||||
"@kit/supabase": "workspace:^",
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@supabase/supabase-js": "^2.42.3",
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
"@kit/tailwind-config": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@radix-ui/react-icons": "^1.3.0",
|
||||
"@tanstack/react-table": "^8.15.3",
|
||||
"@tanstack/react-table": "^8.16.0",
|
||||
"@types/react": "^18.2.77",
|
||||
"@types/react-dom": "^18.2.25",
|
||||
"class-variance-authority": "^0.7.0",
|
||||
|
||||
Reference in New Issue
Block a user