feat(apps): bootstrap, env, i18n, providers, error pages and session proxy
- env validated with the env lib (NEXT_PUBLIC_API_ORIGIN, API_INTERNAL_ORIGIN, site URL, support config); .env.example per app - client bootstrap: API base under /v1, one shared session refresh, error mapping; server bootstrap: cookie forwarding for server renders - i18n instance for the server, bound to react-i18next on the client - root layout, AppProviders, global-error, not-found and a page boundary in every route group; instrumentation reports uncaught server errors - proxy.ts refreshes an expired session before rendering Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a483fc0767
commit
c251633246
|
|
@ -0,0 +1,13 @@
|
|||
# Copy to .env.local. Public variables are inlined into the browser bundle: never put a secret in one.
|
||||
# The API's public origin; routes live under /v1, uploads under /files.
|
||||
NEXT_PUBLIC_API_ORIGIN=http://localhost:3000
|
||||
NEXT_PUBLIC_SITE_URL=http://localhost:3002
|
||||
NEXT_PUBLIC_BALE_HANDLE=lumina_support
|
||||
NEXT_PUBLIC_TELEGRAM_HANDLE=lumina_support
|
||||
NEXT_PUBLIC_SUPPORT_HOURS=شنبه تا پنجشنبه، ۹ تا ۱۸
|
||||
|
||||
# Server only: the API origin from inside the deployment network.
|
||||
API_INTERNAL_ORIGIN=http://localhost:3000
|
||||
|
||||
# Only for steps that run without the real env (lint, type-check, image build).
|
||||
SKIP_ENV_VALIDATION=false
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
'use client';
|
||||
|
||||
// global-error replaces the root layout: no providers, so it brings the bootstrap, styles and i18n.
|
||||
import '../bootstrap/bootstrap.client';
|
||||
import '../styles/app.css';
|
||||
|
||||
import { Element } from '@lumina/island/primitives/element';
|
||||
import { ErrorFallback } from '@lumina/market/app/error-boundaries';
|
||||
|
||||
import { i18n } from '../i18n/i18n.client';
|
||||
import { yekanBakh } from '../styles';
|
||||
|
||||
type GlobalErrorProps = {
|
||||
error: Error & { digest?: string };
|
||||
};
|
||||
|
||||
/** The app boundary (EB-W): the last resort when nothing below caught the error. */
|
||||
export default function GlobalError({ error }: GlobalErrorProps) {
|
||||
return (
|
||||
<Element
|
||||
tag="html"
|
||||
lang={i18n.language}
|
||||
dir={i18n.dir()}
|
||||
className={yekanBakh.variable}
|
||||
>
|
||||
<Element tag="body">
|
||||
<Element tag="meta" name="robots" content="noindex" />
|
||||
<ErrorFallback
|
||||
error={error}
|
||||
size="app"
|
||||
onRetry={() => window.location.reload()}
|
||||
/>
|
||||
</Element>
|
||||
</Element>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,16 +1,31 @@
|
|||
// First: fills the server module graph before anything renders.
|
||||
import '../bootstrap/bootstrap.server';
|
||||
import '../styles/app.css';
|
||||
|
||||
import { Element } from '@lumina/island/primitives/element';
|
||||
import type { Metadata } from 'next';
|
||||
import type { ReactNode } from 'react';
|
||||
|
||||
import { rootMetadata } from '../configs';
|
||||
import { i18n } from '../i18n';
|
||||
import { yekanBakh, yekanBakhLatin } from '../styles';
|
||||
import '../styles/app.css';
|
||||
import { AppProviders } from './organisms/app-providers';
|
||||
|
||||
export function generateMetadata(): Metadata {
|
||||
return rootMetadata();
|
||||
}
|
||||
|
||||
export default function RootLayout({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<html
|
||||
lang="fa"
|
||||
dir="rtl"
|
||||
<Element
|
||||
tag="html"
|
||||
lang={i18n.language}
|
||||
dir={i18n.dir()}
|
||||
className={`${yekanBakh.variable} ${yekanBakhLatin.variable}`}
|
||||
>
|
||||
<body>{children}</body>
|
||||
</html>
|
||||
<Element tag="body">
|
||||
<AppProviders>{children}</AppProviders>
|
||||
</Element>
|
||||
</Element>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
import { StatusEmptyState } from '@lumina/market/app/error-boundaries';
|
||||
import type { Metadata } from 'next';
|
||||
|
||||
export const metadata: Metadata = { robots: { index: false } };
|
||||
|
||||
/** A missing channel, post or page: calm, with the way home. */
|
||||
export default function NotFound() {
|
||||
return <StatusEmptyState variant="not-found" size="page" />;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
'use client';
|
||||
|
||||
// First: fills the client module graph before any client component renders.
|
||||
import '../../../bootstrap/bootstrap.client';
|
||||
|
||||
import { AppShell } from '@lumina/market/app/app-shell';
|
||||
|
||||
import { getQueryClient } from '../../../configs/react-query';
|
||||
import { i18n } from '../../../i18n/i18n.client';
|
||||
import type { AppProvidersProps } from './types';
|
||||
|
||||
/** The app's root client component: runs the client bootstrap, then the platform's shell. */
|
||||
export function AppProviders({ children }: AppProvidersProps) {
|
||||
return (
|
||||
<AppShell queryClient={getQueryClient()} i18n={i18n}>
|
||||
{children}
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { AppProviders } from './AppProviders';
|
||||
export type { AppProvidersProps } from './types';
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import type { ReactNode } from 'react';
|
||||
|
||||
export type AppProvidersProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
'use client';
|
||||
|
||||
import { Element } from '@lumina/island/primitives/element';
|
||||
import { ErrorFallback } from '@lumina/market/app/error-boundaries';
|
||||
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
|
||||
|
||||
import type { PageErrorProps } from './types';
|
||||
|
||||
/**
|
||||
* The page-size boundary (EB-S) every route group's error.tsx renders, inside the shell, so a
|
||||
* failed page keeps the bars usable. Try again resets the failed queries first, or they would
|
||||
* throw again at once. Never indexed.
|
||||
*/
|
||||
export function PageError({ error, retry }: PageErrorProps) {
|
||||
const queryErrors = useQueryErrorResetBoundary();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Element tag="meta" name="robots" content="noindex" />
|
||||
<ErrorFallback
|
||||
error={error}
|
||||
size="page"
|
||||
onRetry={() => {
|
||||
queryErrors.reset();
|
||||
retry();
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { PageError } from './PageError';
|
||||
export type { PageErrorProps } from './types';
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
export type PageErrorProps = {
|
||||
error: Error & { digest?: string };
|
||||
/** Next.js re-fetches and re-renders the segment. */
|
||||
retry: () => void;
|
||||
};
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import { configureErrorHandler } from '@lumina/libs/error-handler';
|
||||
import { eventEmitter } from '@lumina/libs/event-emitter';
|
||||
import { http } from '@lumina/libs/http';
|
||||
import { mapApiErrorBody } from '@lumina/market/app/http';
|
||||
import { SESSION_REFRESH_ENDPOINT } from '@lumina/market/app/session';
|
||||
import { setEnv as setMarketEnv } from '@lumina/market/env';
|
||||
import { AUTH_EVENTS } from '@lumina/market/features/auth';
|
||||
|
||||
import { API_VERSION_PATH } from '../constants';
|
||||
import { clientEnv } from '../env';
|
||||
import { ERROR_HANDLER_CONFIG } from './error-reporting';
|
||||
|
||||
// Fills the client module graph with public values only, before client components render (during
|
||||
// SSR and in the browser). Idempotent: safe to run again.
|
||||
setMarketEnv({
|
||||
apiOrigin: clientEnv.NEXT_PUBLIC_API_ORIGIN,
|
||||
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
|
||||
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
||||
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
|
||||
});
|
||||
// The session lives in HttpOnly cookies the browser sends itself. A 401 runs one refresh for the
|
||||
// whole app; when that fails the session is over and the shell signs the user out.
|
||||
http.configure({
|
||||
baseUrl: `${clientEnv.NEXT_PUBLIC_API_ORIGIN}${API_VERSION_PATH}`,
|
||||
refresh: {
|
||||
endpoint: SESSION_REFRESH_ENDPOINT,
|
||||
onFailure: () =>
|
||||
eventEmitter.emit(AUTH_EVENTS.logout, { reason: 'session-ended' }),
|
||||
},
|
||||
mapErrorBody: mapApiErrorBody,
|
||||
});
|
||||
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
import 'server-only';
|
||||
|
||||
import { configureErrorHandler } from '@lumina/libs/error-handler';
|
||||
import { http } from '@lumina/libs/http';
|
||||
import { configureServerHttp } from '@lumina/libs/server';
|
||||
import { mapApiErrorBody } from '@lumina/market/app/http';
|
||||
import { setEnv as setMarketEnv } from '@lumina/market/env';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import { API_VERSION_PATH } from '../constants';
|
||||
import { clientEnv } from '../env';
|
||||
import { serverEnv } from '../env/server';
|
||||
import { ERROR_HANDLER_CONFIG } from './error-reporting';
|
||||
|
||||
// Fills the server module graph once, before rendering. Idempotent: safe to run again.
|
||||
setMarketEnv({
|
||||
apiOrigin: clientEnv.NEXT_PUBLIC_API_ORIGIN,
|
||||
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
|
||||
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
||||
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
|
||||
});
|
||||
// Each server request forwards its own user's session cookies; nothing per user is kept in module
|
||||
// state. The server never refreshes: proxy.ts already did, before rendering.
|
||||
configureServerHttp({
|
||||
baseUrl: `${serverEnv.API_INTERNAL_ORIGIN}${API_VERSION_PATH}`,
|
||||
getCookieHeader: async () => (await cookies()).toString(),
|
||||
});
|
||||
http.configure({ mapErrorBody: mapApiErrorBody });
|
||||
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import type { ErrorHandlerConfig } from '@lumina/libs/error-handler';
|
||||
|
||||
/**
|
||||
* The error handler's settings for both sides. `report` takes the error-tracking integration's
|
||||
* adapter once it exists; until then 5xx and logic errors are classified but not sent anywhere.
|
||||
*/
|
||||
export const ERROR_HANDLER_CONFIG: ErrorHandlerConfig = {};
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { getQueryClient } from './react-query';
|
||||
export { rootMetadata } from './seo';
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
import { handleError } from '@lumina/libs/error-handler';
|
||||
import {
|
||||
defaultShouldDehydrateQuery,
|
||||
environmentManager,
|
||||
QueryCache,
|
||||
QueryClient,
|
||||
} from '@tanstack/react-query';
|
||||
|
||||
function makeQueryClient(): QueryClient {
|
||||
return new QueryClient({
|
||||
queryCache: new QueryCache({
|
||||
// The one place a failed refetch stops; a failed first load goes to its boundary instead.
|
||||
onError: (error, query) => {
|
||||
if (query.state.data !== undefined) {
|
||||
handleError(error, { where: 'refetch' });
|
||||
}
|
||||
},
|
||||
}),
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
// Hydrated data isn't refetched on mount.
|
||||
staleTime: 60_000,
|
||||
// The HTTP lib already retried idempotent requests; the user retries from the fallback.
|
||||
retry: false,
|
||||
// Offline fails at once and shows the network fallback, instead of an endless skeleton.
|
||||
networkMode: 'offlineFirst',
|
||||
// A first load that fails throws to its boundary; a failed refetch keeps the data.
|
||||
throwOnError: (_error, query) => query.state.data === undefined,
|
||||
},
|
||||
mutations: { networkMode: 'offlineFirst' },
|
||||
dehydrate: {
|
||||
// Pending queries stream to the browser.
|
||||
shouldDehydrateQuery: (query) =>
|
||||
defaultShouldDehydrateQuery(query) ||
|
||||
query.state.status === 'pending',
|
||||
// Next.js reports the real error on the server; the browser gets its digest.
|
||||
shouldRedactErrors: () => false,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let browserQueryClient: QueryClient | undefined;
|
||||
|
||||
/** A new client for every server request; one shared client in the browser. */
|
||||
export function getQueryClient(): QueryClient {
|
||||
if (environmentManager.isServer()) return makeQueryClient();
|
||||
browserQueryClient ??= makeQueryClient();
|
||||
return browserQueryClient;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import type { Metadata } from 'next';
|
||||
|
||||
import { clientEnv } from '../env';
|
||||
import { i18n } from '../i18n';
|
||||
|
||||
/** The root metadata: the brand as the title template, and absolute URLs from the site address. */
|
||||
export function rootMetadata(): Metadata {
|
||||
const name = i18n.t('app.name');
|
||||
return {
|
||||
metadataBase: new URL(clientEnv.NEXT_PUBLIC_SITE_URL),
|
||||
title: { default: name, template: `%s · ${name}` },
|
||||
description: i18n.t('app.description'),
|
||||
applicationName: name,
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
/** Every gateway route except `/files/*` sits under this prefix. */
|
||||
export const API_VERSION_PATH = '/v1';
|
||||
|
|
@ -0,0 +1 @@
|
|||
export { API_VERSION_PATH } from './api';
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import { createClientEnv } from '@lumina/libs/env';
|
||||
import * as v from 'valibot';
|
||||
|
||||
const required = v.pipe(v.string(), v.nonEmpty());
|
||||
const url = v.pipe(v.string(), v.url());
|
||||
|
||||
/** Public variables, inlined into the browser bundle. Next.js inlines only literal reads. */
|
||||
export const clientEnv = createClientEnv({
|
||||
prefix: 'NEXT_PUBLIC_',
|
||||
schema: {
|
||||
NEXT_PUBLIC_API_ORIGIN: url,
|
||||
NEXT_PUBLIC_SITE_URL: url,
|
||||
NEXT_PUBLIC_BALE_HANDLE: required,
|
||||
NEXT_PUBLIC_TELEGRAM_HANDLE: required,
|
||||
NEXT_PUBLIC_SUPPORT_HOURS: required,
|
||||
},
|
||||
runtimeEnv: {
|
||||
NEXT_PUBLIC_API_ORIGIN: process.env.NEXT_PUBLIC_API_ORIGIN,
|
||||
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
|
||||
NEXT_PUBLIC_BALE_HANDLE: process.env.NEXT_PUBLIC_BALE_HANDLE,
|
||||
NEXT_PUBLIC_TELEGRAM_HANDLE: process.env.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
||||
NEXT_PUBLIC_SUPPORT_HOURS: process.env.NEXT_PUBLIC_SUPPORT_HOURS,
|
||||
},
|
||||
});
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
import 'server-only';
|
||||
|
||||
import { createServerEnv } from '@lumina/libs/env/server';
|
||||
import * as v from 'valibot';
|
||||
|
||||
/** Server variables; only server code imports them, through `env/server`. */
|
||||
export const serverEnv = createServerEnv({
|
||||
schema: {
|
||||
API_INTERNAL_ORIGIN: v.pipe(v.string(), v.url()),
|
||||
},
|
||||
runtimeEnv: process.env,
|
||||
skipValidation: process.env.SKIP_ENV_VALIDATION === 'true',
|
||||
});
|
||||
|
|
@ -0,0 +1 @@
|
|||
export { clientEnv } from './env.client';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import 'server-only';
|
||||
|
||||
export { serverEnv } from './env.server';
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import { setI18n } from 'react-i18next';
|
||||
|
||||
import { i18n } from './i18n';
|
||||
|
||||
// useTranslation works even without a provider (global-error renders outside the shell).
|
||||
setI18n(i18n);
|
||||
|
||||
export { i18n };
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import {
|
||||
DEFAULT_LOCALE,
|
||||
DEFAULT_NAMESPACE,
|
||||
NAMESPACES,
|
||||
resources,
|
||||
} from '@lumina/market/shared/locales';
|
||||
import { createInstance } from 'i18next';
|
||||
import type { i18n as I18n } from 'i18next';
|
||||
|
||||
/**
|
||||
* The app's i18n instance. One Persian locale with resources bundled in, so it initialises
|
||||
* synchronously and holds nothing per request. Server components read it directly; client code
|
||||
* imports it through `i18n.client`, which binds it to react-i18next (React context can't load in
|
||||
* a server component).
|
||||
*/
|
||||
export const i18n: I18n = createInstance();
|
||||
|
||||
void i18n.init({
|
||||
lng: DEFAULT_LOCALE,
|
||||
fallbackLng: DEFAULT_LOCALE,
|
||||
resources,
|
||||
ns: NAMESPACES,
|
||||
defaultNS: DEFAULT_NAMESPACE,
|
||||
initAsync: false,
|
||||
// React escapes already.
|
||||
interpolation: { escapeValue: false },
|
||||
});
|
||||
|
|
@ -0,0 +1 @@
|
|||
export { i18n } from './i18n';
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import { configureErrorHandler, handleError } from '@lumina/libs/error-handler';
|
||||
import type { Instrumentation } from 'next';
|
||||
|
||||
import { ERROR_HANDLER_CONFIG } from './bootstrap/error-reporting';
|
||||
|
||||
export function register(): void {
|
||||
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
||||
}
|
||||
|
||||
/** Reports what no server code caught, once, with the route it happened on. */
|
||||
export const onRequestError: Instrumentation.onRequestError = (
|
||||
error,
|
||||
_request,
|
||||
context,
|
||||
) => {
|
||||
handleError(error, { where: 'server', name: context.routePath });
|
||||
};
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import { refreshSessionInProxy } from '@lumina/market/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import { API_VERSION_PATH } from './constants';
|
||||
import { serverEnv } from './env/server';
|
||||
|
||||
/** Refreshes an expired session before the page renders (see refreshSessionInProxy). */
|
||||
export function proxy(request: NextRequest) {
|
||||
return refreshSessionInProxy(request, {
|
||||
apiUrl: `${serverEnv.API_INTERNAL_ORIGIN}${API_VERSION_PATH}`,
|
||||
});
|
||||
}
|
||||
|
||||
export const config = {
|
||||
// Pages and their data requests only: not build assets, images or files with an extension.
|
||||
matcher: ['/((?!_next/static|_next/image|favicon.ico|.*\\.[a-zA-Z0-9]+$).*)'],
|
||||
};
|
||||
|
|
@ -7,7 +7,7 @@ import localFont from 'next/font/local';
|
|||
export const yekanBakh = localFont({
|
||||
src: '../../../../packages/island/src/fonts/yekan-bakh-fa-num-vf.woff2',
|
||||
variable: '--font-yekan-bakh',
|
||||
weight: '100 950',
|
||||
weight: '100 1000',
|
||||
display: 'swap',
|
||||
adjustFontFallback: 'Arial',
|
||||
});
|
||||
|
|
@ -16,7 +16,7 @@ export const yekanBakh = localFont({
|
|||
export const yekanBakhLatin = localFont({
|
||||
src: '../../../../packages/island/src/fonts/yekan-bakh-vf.woff2',
|
||||
variable: '--font-yekan-bakh-latin',
|
||||
weight: '100 950',
|
||||
weight: '100 1000',
|
||||
display: 'swap',
|
||||
preload: false,
|
||||
adjustFontFallback: 'Arial',
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
# Copy to .env.local. Public variables are inlined into the browser bundle: never put a secret in one.
|
||||
# The API's public origin; routes live under /v1, uploads under /files.
|
||||
NEXT_PUBLIC_API_ORIGIN=http://localhost:3000
|
||||
NEXT_PUBLIC_SITE_URL=http://localhost:3001
|
||||
NEXT_PUBLIC_BALE_HANDLE=lumina_support
|
||||
NEXT_PUBLIC_TELEGRAM_HANDLE=lumina_support
|
||||
NEXT_PUBLIC_SUPPORT_HOURS=شنبه تا پنجشنبه، ۹ تا ۱۸
|
||||
|
||||
# Server only: the API origin from inside the deployment network.
|
||||
API_INTERNAL_ORIGIN=http://localhost:3000
|
||||
|
||||
# Only for steps that run without the real env (lint, type-check, image build).
|
||||
SKIP_ENV_VALIDATION=false
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<!-- BEGIN:nextjs-agent-rules -->
|
||||
|
||||
# This is NOT the Next.js you know
|
||||
|
||||
This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in `node_modules/next/dist/docs/` (resolved from this file's directory; in monorepos the `next` package may not be visible from the repo root) before writing any code. Heed deprecation notices.
|
||||
|
||||
This block is written and re-added by `next dev` — verify at `node_modules/next/dist/server/lib/generate-agent-files.js`. Removing it from a diff only re-creates the uncommitted change; committing it with your work keeps the tree clean.
|
||||
|
||||
<!-- END:nextjs-agent-rules -->
|
||||
|
|
@ -0,0 +1 @@
|
|||
@AGENTS.md
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
'use client';
|
||||
|
||||
export { PageError as default } from '../organisms/page-error';
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
'use client';
|
||||
|
||||
// global-error replaces the root layout: no providers, so it brings the bootstrap, styles and i18n.
|
||||
import '../bootstrap/bootstrap.client';
|
||||
import '../styles/app.css';
|
||||
|
||||
import { Element } from '@lumina/island/primitives/element';
|
||||
import { ErrorFallback } from '@lumina/market/app/error-boundaries';
|
||||
|
||||
import { i18n } from '../i18n/i18n.client';
|
||||
import { yekanBakh } from '../styles';
|
||||
|
||||
type GlobalErrorProps = {
|
||||
error: Error & { digest?: string };
|
||||
};
|
||||
|
||||
/** The app boundary (EB-W): the last resort when nothing below caught the error. */
|
||||
export default function GlobalError({ error }: GlobalErrorProps) {
|
||||
return (
|
||||
<Element
|
||||
tag="html"
|
||||
lang={i18n.language}
|
||||
dir={i18n.dir()}
|
||||
className={yekanBakh.variable}
|
||||
>
|
||||
<Element tag="body">
|
||||
<Element tag="meta" name="robots" content="noindex" />
|
||||
<ErrorFallback
|
||||
error={error}
|
||||
size="app"
|
||||
onRetry={() => window.location.reload()}
|
||||
/>
|
||||
</Element>
|
||||
</Element>
|
||||
);
|
||||
}
|
||||
|
|
@ -1,16 +1,31 @@
|
|||
// First: fills the server module graph before anything renders.
|
||||
import '../bootstrap/bootstrap.server';
|
||||
import '../styles/app.css';
|
||||
|
||||
import { Element } from '@lumina/island/primitives/element';
|
||||
import type { Metadata } from 'next';
|
||||
import type { ReactNode } from 'react';
|
||||
|
||||
import { rootMetadata } from '../configs';
|
||||
import { i18n } from '../i18n';
|
||||
import { yekanBakh, yekanBakhLatin } from '../styles';
|
||||
import '../styles/app.css';
|
||||
import { AppProviders } from './organisms/app-providers';
|
||||
|
||||
export function generateMetadata(): Metadata {
|
||||
return rootMetadata();
|
||||
}
|
||||
|
||||
export default function RootLayout({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<html
|
||||
lang="fa"
|
||||
dir="rtl"
|
||||
<Element
|
||||
tag="html"
|
||||
lang={i18n.language}
|
||||
dir={i18n.dir()}
|
||||
className={`${yekanBakh.variable} ${yekanBakhLatin.variable}`}
|
||||
>
|
||||
<body>{children}</body>
|
||||
</html>
|
||||
<Element tag="body">
|
||||
<AppProviders>{children}</AppProviders>
|
||||
</Element>
|
||||
</Element>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
import { StatusEmptyState } from '@lumina/market/app/error-boundaries';
|
||||
import type { Metadata } from 'next';
|
||||
|
||||
export const metadata: Metadata = { robots: { index: false } };
|
||||
|
||||
/** A missing channel, post or page: calm, with the way home. */
|
||||
export default function NotFound() {
|
||||
return <StatusEmptyState variant="not-found" size="page" />;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
'use client';
|
||||
|
||||
// First: fills the client module graph before any client component renders.
|
||||
import '../../../bootstrap/bootstrap.client';
|
||||
|
||||
import { AppShell } from '@lumina/market/app/app-shell';
|
||||
|
||||
import { getQueryClient } from '../../../configs/react-query';
|
||||
import { i18n } from '../../../i18n/i18n.client';
|
||||
import type { AppProvidersProps } from './types';
|
||||
|
||||
/** The app's root client component: runs the client bootstrap, then the platform's shell. */
|
||||
export function AppProviders({ children }: AppProvidersProps) {
|
||||
return (
|
||||
<AppShell queryClient={getQueryClient()} i18n={i18n}>
|
||||
{children}
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { AppProviders } from './AppProviders';
|
||||
export type { AppProvidersProps } from './types';
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import type { ReactNode } from 'react';
|
||||
|
||||
export type AppProvidersProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
'use client';
|
||||
|
||||
import { Element } from '@lumina/island/primitives/element';
|
||||
import { ErrorFallback } from '@lumina/market/app/error-boundaries';
|
||||
import { useQueryErrorResetBoundary } from '@tanstack/react-query';
|
||||
|
||||
import type { PageErrorProps } from './types';
|
||||
|
||||
/**
|
||||
* The page-size boundary (EB-S) every route group's error.tsx renders, inside the shell, so a
|
||||
* failed page keeps the bars usable. Try again resets the failed queries first, or they would
|
||||
* throw again at once. Never indexed.
|
||||
*/
|
||||
export function PageError({ error, retry }: PageErrorProps) {
|
||||
const queryErrors = useQueryErrorResetBoundary();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Element tag="meta" name="robots" content="noindex" />
|
||||
<ErrorFallback
|
||||
error={error}
|
||||
size="page"
|
||||
onRetry={() => {
|
||||
queryErrors.reset();
|
||||
retry();
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { PageError } from './PageError';
|
||||
export type { PageErrorProps } from './types';
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
export type PageErrorProps = {
|
||||
error: Error & { digest?: string };
|
||||
/** Next.js re-fetches and re-renders the segment. */
|
||||
retry: () => void;
|
||||
};
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
import { configureErrorHandler } from '@lumina/libs/error-handler';
|
||||
import { eventEmitter } from '@lumina/libs/event-emitter';
|
||||
import { http } from '@lumina/libs/http';
|
||||
import { mapApiErrorBody } from '@lumina/market/app/http';
|
||||
import { SESSION_REFRESH_ENDPOINT } from '@lumina/market/app/session';
|
||||
import { setEnv as setMarketEnv } from '@lumina/market/env';
|
||||
import { AUTH_EVENTS } from '@lumina/market/features/auth';
|
||||
|
||||
import { API_VERSION_PATH } from '../constants';
|
||||
import { clientEnv } from '../env';
|
||||
import { ERROR_HANDLER_CONFIG } from './error-reporting';
|
||||
|
||||
// Fills the client module graph with public values only, before client components render (during
|
||||
// SSR and in the browser). Idempotent: safe to run again.
|
||||
setMarketEnv({
|
||||
apiOrigin: clientEnv.NEXT_PUBLIC_API_ORIGIN,
|
||||
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
|
||||
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
||||
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
|
||||
});
|
||||
// The session lives in HttpOnly cookies the browser sends itself. A 401 runs one refresh for the
|
||||
// whole app; when that fails the session is over and the shell signs the user out.
|
||||
http.configure({
|
||||
baseUrl: `${clientEnv.NEXT_PUBLIC_API_ORIGIN}${API_VERSION_PATH}`,
|
||||
refresh: {
|
||||
endpoint: SESSION_REFRESH_ENDPOINT,
|
||||
onFailure: () =>
|
||||
eventEmitter.emit(AUTH_EVENTS.logout, { reason: 'session-ended' }),
|
||||
},
|
||||
mapErrorBody: mapApiErrorBody,
|
||||
});
|
||||
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
import 'server-only';
|
||||
|
||||
import { configureErrorHandler } from '@lumina/libs/error-handler';
|
||||
import { http } from '@lumina/libs/http';
|
||||
import { configureServerHttp } from '@lumina/libs/server';
|
||||
import { mapApiErrorBody } from '@lumina/market/app/http';
|
||||
import { setEnv as setMarketEnv } from '@lumina/market/env';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
import { API_VERSION_PATH } from '../constants';
|
||||
import { clientEnv } from '../env';
|
||||
import { serverEnv } from '../env/server';
|
||||
import { ERROR_HANDLER_CONFIG } from './error-reporting';
|
||||
|
||||
// Fills the server module graph once, before rendering. Idempotent: safe to run again.
|
||||
setMarketEnv({
|
||||
apiOrigin: clientEnv.NEXT_PUBLIC_API_ORIGIN,
|
||||
baleHandle: clientEnv.NEXT_PUBLIC_BALE_HANDLE,
|
||||
telegramHandle: clientEnv.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
||||
supportHours: clientEnv.NEXT_PUBLIC_SUPPORT_HOURS,
|
||||
});
|
||||
// Each server request forwards its own user's session cookies; nothing per user is kept in module
|
||||
// state. The server never refreshes: proxy.ts already did, before rendering.
|
||||
configureServerHttp({
|
||||
baseUrl: `${serverEnv.API_INTERNAL_ORIGIN}${API_VERSION_PATH}`,
|
||||
getCookieHeader: async () => (await cookies()).toString(),
|
||||
});
|
||||
http.configure({ mapErrorBody: mapApiErrorBody });
|
||||
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import type { ErrorHandlerConfig } from '@lumina/libs/error-handler';
|
||||
|
||||
/**
|
||||
* The error handler's settings for both sides. `report` takes the error-tracking integration's
|
||||
* adapter once it exists; until then 5xx and logic errors are classified but not sent anywhere.
|
||||
*/
|
||||
export const ERROR_HANDLER_CONFIG: ErrorHandlerConfig = {};
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
export { getQueryClient } from './react-query';
|
||||
export { rootMetadata } from './seo';
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
import { handleError } from '@lumina/libs/error-handler';
|
||||
import {
|
||||
defaultShouldDehydrateQuery,
|
||||
environmentManager,
|
||||
QueryCache,
|
||||
QueryClient,
|
||||
} from '@tanstack/react-query';
|
||||
|
||||
function makeQueryClient(): QueryClient {
|
||||
return new QueryClient({
|
||||
queryCache: new QueryCache({
|
||||
// The one place a failed refetch stops; a failed first load goes to its boundary instead.
|
||||
onError: (error, query) => {
|
||||
if (query.state.data !== undefined) {
|
||||
handleError(error, { where: 'refetch' });
|
||||
}
|
||||
},
|
||||
}),
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
// Hydrated data isn't refetched on mount.
|
||||
staleTime: 60_000,
|
||||
// The HTTP lib already retried idempotent requests; the user retries from the fallback.
|
||||
retry: false,
|
||||
// Offline fails at once and shows the network fallback, instead of an endless skeleton.
|
||||
networkMode: 'offlineFirst',
|
||||
// A first load that fails throws to its boundary; a failed refetch keeps the data.
|
||||
throwOnError: (_error, query) => query.state.data === undefined,
|
||||
},
|
||||
mutations: { networkMode: 'offlineFirst' },
|
||||
dehydrate: {
|
||||
// Pending queries stream to the browser.
|
||||
shouldDehydrateQuery: (query) =>
|
||||
defaultShouldDehydrateQuery(query) ||
|
||||
query.state.status === 'pending',
|
||||
// Next.js reports the real error on the server; the browser gets its digest.
|
||||
shouldRedactErrors: () => false,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let browserQueryClient: QueryClient | undefined;
|
||||
|
||||
/** A new client for every server request; one shared client in the browser. */
|
||||
export function getQueryClient(): QueryClient {
|
||||
if (environmentManager.isServer()) return makeQueryClient();
|
||||
browserQueryClient ??= makeQueryClient();
|
||||
return browserQueryClient;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import type { Metadata } from 'next';
|
||||
|
||||
import { clientEnv } from '../env';
|
||||
import { i18n } from '../i18n';
|
||||
|
||||
/** The root metadata: the brand as the title template, and absolute URLs from the site address. */
|
||||
export function rootMetadata(): Metadata {
|
||||
const name = i18n.t('app.name');
|
||||
return {
|
||||
metadataBase: new URL(clientEnv.NEXT_PUBLIC_SITE_URL),
|
||||
title: { default: name, template: `%s · ${name}` },
|
||||
description: i18n.t('app.description'),
|
||||
applicationName: name,
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
/** Every gateway route except `/files/*` sits under this prefix. */
|
||||
export const API_VERSION_PATH = '/v1';
|
||||
|
|
@ -0,0 +1 @@
|
|||
export { API_VERSION_PATH } from './api';
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
import { createClientEnv } from '@lumina/libs/env';
|
||||
import * as v from 'valibot';
|
||||
|
||||
const required = v.pipe(v.string(), v.nonEmpty());
|
||||
const url = v.pipe(v.string(), v.url());
|
||||
|
||||
/** Public variables, inlined into the browser bundle. Next.js inlines only literal reads. */
|
||||
export const clientEnv = createClientEnv({
|
||||
prefix: 'NEXT_PUBLIC_',
|
||||
schema: {
|
||||
NEXT_PUBLIC_API_ORIGIN: url,
|
||||
NEXT_PUBLIC_SITE_URL: url,
|
||||
NEXT_PUBLIC_BALE_HANDLE: required,
|
||||
NEXT_PUBLIC_TELEGRAM_HANDLE: required,
|
||||
NEXT_PUBLIC_SUPPORT_HOURS: required,
|
||||
},
|
||||
runtimeEnv: {
|
||||
NEXT_PUBLIC_API_ORIGIN: process.env.NEXT_PUBLIC_API_ORIGIN,
|
||||
NEXT_PUBLIC_SITE_URL: process.env.NEXT_PUBLIC_SITE_URL,
|
||||
NEXT_PUBLIC_BALE_HANDLE: process.env.NEXT_PUBLIC_BALE_HANDLE,
|
||||
NEXT_PUBLIC_TELEGRAM_HANDLE: process.env.NEXT_PUBLIC_TELEGRAM_HANDLE,
|
||||
NEXT_PUBLIC_SUPPORT_HOURS: process.env.NEXT_PUBLIC_SUPPORT_HOURS,
|
||||
},
|
||||
});
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
import 'server-only';
|
||||
|
||||
import { createServerEnv } from '@lumina/libs/env/server';
|
||||
import * as v from 'valibot';
|
||||
|
||||
/** Server variables; only server code imports them, through `env/server`. */
|
||||
export const serverEnv = createServerEnv({
|
||||
schema: {
|
||||
API_INTERNAL_ORIGIN: v.pipe(v.string(), v.url()),
|
||||
},
|
||||
runtimeEnv: process.env,
|
||||
skipValidation: process.env.SKIP_ENV_VALIDATION === 'true',
|
||||
});
|
||||
|
|
@ -0,0 +1 @@
|
|||
export { clientEnv } from './env.client';
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import 'server-only';
|
||||
|
||||
export { serverEnv } from './env.server';
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import { setI18n } from 'react-i18next';
|
||||
|
||||
import { i18n } from './i18n';
|
||||
|
||||
// useTranslation works even without a provider (global-error renders outside the shell).
|
||||
setI18n(i18n);
|
||||
|
||||
export { i18n };
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import {
|
||||
DEFAULT_LOCALE,
|
||||
DEFAULT_NAMESPACE,
|
||||
NAMESPACES,
|
||||
resources,
|
||||
} from '@lumina/market/shared/locales';
|
||||
import { createInstance } from 'i18next';
|
||||
import type { i18n as I18n } from 'i18next';
|
||||
|
||||
/**
|
||||
* The app's i18n instance. One Persian locale with resources bundled in, so it initialises
|
||||
* synchronously and holds nothing per request. Server components read it directly; client code
|
||||
* imports it through `i18n.client`, which binds it to react-i18next (React context can't load in
|
||||
* a server component).
|
||||
*/
|
||||
export const i18n: I18n = createInstance();
|
||||
|
||||
void i18n.init({
|
||||
lng: DEFAULT_LOCALE,
|
||||
fallbackLng: DEFAULT_LOCALE,
|
||||
resources,
|
||||
ns: NAMESPACES,
|
||||
defaultNS: DEFAULT_NAMESPACE,
|
||||
initAsync: false,
|
||||
// React escapes already.
|
||||
interpolation: { escapeValue: false },
|
||||
});
|
||||
|
|
@ -0,0 +1 @@
|
|||
export { i18n } from './i18n';
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import { configureErrorHandler, handleError } from '@lumina/libs/error-handler';
|
||||
import type { Instrumentation } from 'next';
|
||||
|
||||
import { ERROR_HANDLER_CONFIG } from './bootstrap/error-reporting';
|
||||
|
||||
export function register(): void {
|
||||
configureErrorHandler(ERROR_HANDLER_CONFIG);
|
||||
}
|
||||
|
||||
/** Reports what no server code caught, once, with the route it happened on. */
|
||||
export const onRequestError: Instrumentation.onRequestError = (
|
||||
error,
|
||||
_request,
|
||||
context,
|
||||
) => {
|
||||
handleError(error, { where: 'server', name: context.routePath });
|
||||
};
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import { refreshSessionInProxy } from '@lumina/market/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import { API_VERSION_PATH } from './constants';
|
||||
import { serverEnv } from './env/server';
|
||||
|
||||
/** Refreshes an expired session before the page renders (see refreshSessionInProxy). */
|
||||
export function proxy(request: NextRequest) {
|
||||
return refreshSessionInProxy(request, {
|
||||
apiUrl: `${serverEnv.API_INTERNAL_ORIGIN}${API_VERSION_PATH}`,
|
||||
});
|
||||
}
|
||||
|
||||
export const config = {
|
||||
// Pages and their data requests only: not build assets, images or files with an extension.
|
||||
matcher: ['/((?!_next/static|_next/image|favicon.ico|.*\\.[a-zA-Z0-9]+$).*)'],
|
||||
};
|
||||
|
|
@ -7,7 +7,7 @@ import localFont from 'next/font/local';
|
|||
export const yekanBakh = localFont({
|
||||
src: '../../../../packages/island/src/fonts/yekan-bakh-fa-num-vf.woff2',
|
||||
variable: '--font-yekan-bakh',
|
||||
weight: '100 950',
|
||||
weight: '100 1000',
|
||||
display: 'swap',
|
||||
adjustFontFallback: 'Arial',
|
||||
});
|
||||
|
|
@ -16,7 +16,7 @@ export const yekanBakh = localFont({
|
|||
export const yekanBakhLatin = localFont({
|
||||
src: '../../../../packages/island/src/fonts/yekan-bakh-vf.woff2',
|
||||
variable: '--font-yekan-bakh-latin',
|
||||
weight: '100 950',
|
||||
weight: '100 1000',
|
||||
display: 'swap',
|
||||
preload: false,
|
||||
adjustFontFallback: 'Arial',
|
||||
|
|
|
|||
Loading…
Reference in New Issue