@djangocfg/nextjs 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@djangocfg/nextjs",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Next.js utilities and components: sitemap, health, OG images, legal pages, error pages",
5
5
  "keywords": [
6
6
  "nextjs",
@@ -109,14 +109,14 @@
109
109
  "next": "^15.4.4",
110
110
  "react": "^19.1.0",
111
111
  "react-dom": "^19.1.0",
112
- "@djangocfg/ui": "^1.4.32",
113
- "@djangocfg/layouts": "^2.0.2",
114
- "@djangocfg/api": "^1.4.32",
112
+ "@djangocfg/ui": "^1.4.33",
113
+ "@djangocfg/layouts": "^2.0.3",
114
+ "@djangocfg/api": "^1.4.33",
115
115
  "lucide-react": "^0.469.0"
116
116
  },
117
117
  "devDependencies": {
118
- "@djangocfg/typescript-config": "^1.4.32",
119
- "@djangocfg/layouts": "^2.0.2",
118
+ "@djangocfg/typescript-config": "^1.4.33",
119
+ "@djangocfg/layouts": "^2.0.3",
120
120
  "@types/node": "^24.7.2",
121
121
  "@types/react": "19.2.2",
122
122
  "@types/react-dom": "19.2.1",
@@ -1,8 +1,8 @@
1
1
  'use client';
2
2
 
3
3
  import { useEffect } from 'react';
4
- import { useRouter } from 'next/navigation';
5
4
  import { useAuth } from '@djangocfg/layouts';
5
+ import { useRouter } from '@djangocfg/ui/hooks';
6
6
  import { Loader2 } from 'lucide-react';
7
7
 
8
8
  export interface HomePageProps {
@@ -23,6 +23,7 @@
23
23
 
24
24
  import type { NextConfig } from 'next';
25
25
  import type { Configuration as WebpackConfig } from 'webpack';
26
+ import { deepMerge } from './deepMerge';
26
27
 
27
28
  // ─────────────────────────────────────────────────────────────────────────
28
29
  // Standard Environment Variables
@@ -36,10 +37,8 @@ const isDev = process.env.NODE_ENV === 'development';
36
37
  // ─────────────────────────────────────────────────────────────────────────
37
38
 
38
39
  export interface BaseNextConfigOptions {
39
- /** Base path for static builds (default: '/cfg/admin') */
40
- basePath?: string;
41
- /** Static build path - used when NEXT_PUBLIC_STATIC_BUILD=true (overrides basePath for static builds) */
42
- staticBuildPath?: string;
40
+ /** Static build path (used when NEXT_PUBLIC_STATIC_BUILD=true) */
41
+ isDefaultCfgAdmin?: boolean;
43
42
  /** Additional transpile packages (merged with defaults) */
44
43
  transpilePackages?: string[];
45
44
  /** Additional optimize package imports (merged with defaults) */
@@ -47,7 +46,7 @@ export interface BaseNextConfigOptions {
47
46
  /** Custom webpack configuration function (called after base webpack logic) */
48
47
  webpack?: (
49
48
  config: WebpackConfig,
50
- options: { isServer: boolean; dev: boolean; [key: string]: any }
49
+ options: { isServer: boolean; dev: boolean;[key: string]: any }
51
50
  ) => WebpackConfig | void;
52
51
  /** Custom experimental options (merged with defaults) */
53
52
  experimental?: NextConfig['experimental'];
@@ -57,38 +56,6 @@ export interface BaseNextConfigOptions {
57
56
  [key: string]: any;
58
57
  }
59
58
 
60
- // ─────────────────────────────────────────────────────────────────────────
61
- // Deep Merge Helper (simple implementation)
62
- // ─────────────────────────────────────────────────────────────────────────
63
-
64
- function deepMerge<T extends Record<string, any>>(target: T, source: Partial<T>): T {
65
- const output = { ...target };
66
-
67
- for (const key in source) {
68
- if (source[key] === undefined) continue;
69
-
70
- // Arrays: replace (don't merge arrays)
71
- if (Array.isArray(source[key])) {
72
- output[key] = source[key] as any;
73
- }
74
- // Objects: deep merge
75
- else if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
76
- const targetValue = output[key];
77
- if (targetValue && typeof targetValue === 'object' && !Array.isArray(targetValue)) {
78
- output[key] = deepMerge(targetValue, source[key] as any);
79
- } else {
80
- output[key] = source[key] as any;
81
- }
82
- }
83
- // Primitives: replace
84
- else {
85
- output[key] = source[key] as any;
86
- }
87
- }
88
-
89
- return output;
90
- }
91
-
92
59
  // ─────────────────────────────────────────────────────────────────────────
93
60
  // Base Configuration Factory
94
61
  // ─────────────────────────────────────────────────────────────────────────
@@ -102,10 +69,12 @@ function deepMerge<T extends Record<string, any>>(target: T, source: Partial<T>)
102
69
  export function createBaseNextConfig(
103
70
  options: BaseNextConfigOptions = {}
104
71
  ): NextConfig {
105
- // Determine basePath: staticBuildPath takes precedence for static builds, then basePath, then default
106
- const basePath = isStaticBuild
107
- ? (options.staticBuildPath || options.basePath)
108
- : (options.basePath || '');
72
+
73
+ // If static build not provided, use default for nextjs-admin development
74
+ const staticBuildPath = options.isDefaultCfgAdmin ? '/cfg/admin' : '/cfg/nextjs-admin';
75
+ const basePath = isStaticBuild ? staticBuildPath : '';
76
+ const apiUrl = isStaticBuild ? '' : process.env.NEXT_PUBLIC_API_URL || '';
77
+ const siteUrl = isStaticBuild ? '' : process.env.NEXT_PUBLIC_SITE_URL || '';
109
78
 
110
79
  // Base configuration
111
80
  const baseConfig: NextConfig = {
@@ -116,8 +85,8 @@ export function createBaseNextConfig(
116
85
  ...(isStaticBuild && {
117
86
  output: 'export' as const,
118
87
  distDir: 'out',
119
- basePath,
120
- assetPrefix: basePath,
88
+ basePath: basePath,
89
+ // assetPrefix: basePath,
121
90
  // Fix for Next.js 15.5.4: prevent 500.html generation issue
122
91
  //
123
92
  // PROBLEM: In App Router, error.tsx is a client component ('use client')
@@ -139,7 +108,8 @@ export function createBaseNextConfig(
139
108
  // Environment variables
140
109
  env: {
141
110
  NEXT_PUBLIC_BASE_PATH: basePath,
142
- NEXT_PUBLIC_API_URL: isStaticBuild ? '' : process.env.NEXT_PUBLIC_API_URL,
111
+ NEXT_PUBLIC_API_URL: apiUrl,
112
+ NEXT_PUBLIC_SITE_URL: siteUrl,
143
113
  ...options.env,
144
114
  },
145
115
 
@@ -180,7 +150,7 @@ export function createBaseNextConfig(
180
150
  },
181
151
 
182
152
  // Webpack configuration
183
- webpack: (config: WebpackConfig, webpackOptions: { isServer: boolean; dev: boolean; [key: string]: any }) => {
153
+ webpack: (config: WebpackConfig, webpackOptions: { isServer: boolean; dev: boolean;[key: string]: any }) => {
184
154
  const { isServer, dev } = webpackOptions;
185
155
 
186
156
  // Dev mode optimizations
@@ -209,7 +179,7 @@ export function createBaseNextConfig(
209
179
  try {
210
180
  // Dynamic import to avoid bundling compression-webpack-plugin
211
181
  const CompressionPlugin = require('compression-webpack-plugin');
212
-
182
+
213
183
  // Gzip compression
214
184
  config.plugins.push(
215
185
  new CompressionPlugin({
@@ -255,8 +225,6 @@ export function createBaseNextConfig(
255
225
 
256
226
  // Cleanup: Remove our custom options that are not part of NextConfig
257
227
  // These are internal to BaseNextConfigOptions and should not be in the final config
258
- delete (finalConfig as any).basePath;
259
- delete (finalConfig as any).staticBuildPath;
260
228
  delete (finalConfig as any).optimizePackageImports;
261
229
  // Note: We don't delete transpilePackages, experimental, env, webpack
262
230
  // as they are valid NextConfig keys and may have been overridden by user
@@ -0,0 +1,33 @@
1
+
2
+ // ─────────────────────────────────────────────────────────────────────────
3
+ // Deep Merge Helper (simple implementation)
4
+ // ─────────────────────────────────────────────────────────────────────────
5
+
6
+ export function deepMerge<T extends Record<string, any>>(target: T, source: Partial<T>): T {
7
+ const output = { ...target };
8
+
9
+ for (const key in source) {
10
+ if (source[key] === undefined) continue;
11
+
12
+ // Arrays: replace (don't merge arrays)
13
+ if (Array.isArray(source[key])) {
14
+ output[key] = source[key] as any;
15
+ }
16
+ // Objects: deep merge
17
+ else if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
18
+ const targetValue = output[key];
19
+ if (targetValue && typeof targetValue === 'object' && !Array.isArray(targetValue)) {
20
+ output[key] = deepMerge(targetValue, source[key] as any);
21
+ } else {
22
+ output[key] = source[key] as any;
23
+ }
24
+ }
25
+ // Primitives: replace
26
+ else {
27
+ output[key] = source[key] as any;
28
+ }
29
+ }
30
+
31
+ return output;
32
+ }
33
+
@@ -6,4 +6,3 @@
6
6
 
7
7
  export * from './types';
8
8
  export * from './utils';
9
-
@@ -10,40 +10,24 @@ import type { RouteDefinition, RouteMetadata, MenuItem } from './types';
10
10
  // Route Definition Helper
11
11
  // ─────────────────────────────────────────────────────────────────────────
12
12
 
13
- export interface RouteConfig {
14
- /** Base path for static builds (optional) */
15
- basePath?: string;
16
- /** Whether this is a static build (optional, defaults to false) */
17
- isStaticBuild?: boolean;
18
- }
19
-
20
13
  /**
21
- * Manage base path for routes
22
- */
23
- function manageBasePath(path: string, config?: RouteConfig): string {
24
- if (!config?.isStaticBuild || !config?.basePath) {
25
- return path;
26
- }
27
- return `${config.basePath}${path}`;
28
- }
29
-
30
- /**
31
- * Define a route with automatic basePath injection
14
+ * Define a route with metadata
32
15
  *
33
- * NOTE: For static builds, Next.js automatically prepends basePath to <Link> hrefs,
34
- * so we don't add it here. For dev mode, we need to add it manually.
16
+ * IMPORTANT: Next.js automatically handles basePath for <Link> components when
17
+ * basePath is set in next.config.ts. We should NOT add basePath manually here,
18
+ * as it would cause double-prefixing in static builds.
19
+ *
20
+ * @param path - Route path (e.g., '/dashboard', '/admin/users')
21
+ * @param metadata - Route metadata (label, icon, etc.)
35
22
  */
36
23
  export function defineRoute(
37
24
  path: string,
38
25
  metadata: RouteMetadata,
39
- config?: RouteConfig
40
26
  ): RouteDefinition {
41
- // In static builds, Next.js handles basePath automatically
42
- // In dev mode, we need to add it manually for consistency
43
- const fullPath = manageBasePath(path, config);
44
-
27
+ // Always return path as-is. Next.js will handle basePath automatically
28
+ // for <Link> components when basePath is set in next.config.ts
45
29
  return {
46
- path: fullPath,
30
+ path,
47
31
  metadata,
48
32
  };
49
33
  }
@@ -58,11 +42,11 @@ export function defineRoute(
58
42
  export function getUnauthenticatedRedirect(
59
43
  path: string,
60
44
  authPath: string = '/auth',
61
- config?: RouteConfig
62
45
  ): string | null {
63
46
  if (path.startsWith('/private') || path.startsWith('/admin')) {
64
- // In static builds, Next.js handles basePath automatically
65
- return manageBasePath(authPath, config);
47
+ // Return path as-is. Next.js will handle basePath automatically for router.push()
48
+ // when basePath is set in next.config.ts
49
+ return authPath;
66
50
  }
67
51
  return null;
68
52
  }
@@ -72,10 +56,10 @@ export function getUnauthenticatedRedirect(
72
56
  */
73
57
  export function redirectToAuth(
74
58
  authPath: string = '/auth',
75
- config?: RouteConfig
76
59
  ): string {
77
- // In static builds, Next.js handles basePath automatically
78
- return manageBasePath(authPath, config);
60
+ // Return path as-is. Next.js will handle basePath automatically for router.push()
61
+ // when basePath is set in next.config.ts
62
+ return authPath;
79
63
  }
80
64
 
81
65
  // ─────────────────────────────────────────────────────────────────────────
@@ -93,13 +93,8 @@ function getSiteUrl(): string {
93
93
  return process.env.NEXT_PUBLIC_SITE_URL;
94
94
  }
95
95
 
96
- // Fallback to VERCEL_URL if available
97
- if (typeof process !== 'undefined' && process.env.VERCEL_URL) {
98
- return `https://${process.env.VERCEL_URL}`;
99
- }
100
-
101
96
  // Development fallback
102
- return 'http://localhost:3000';
97
+ return '';
103
98
  }
104
99
 
105
100
  export function generateOgImageMetadata(
@@ -161,8 +156,8 @@ export function generateOgImageMetadata(
161
156
  : [metadata.twitter.images]
162
157
  : [];
163
158
 
164
- // Merge with existing metadata
165
- return {
159
+ // Build final metadata object
160
+ const finalMetadata: Metadata = {
166
161
  ...metadata,
167
162
  openGraph: {
168
163
  ...metadata.openGraph,
@@ -188,6 +183,23 @@ export function generateOgImageMetadata(
188
183
  ],
189
184
  },
190
185
  };
186
+
187
+ // Automatically add metadataBase if siteUrl is an absolute URL
188
+ // metadataBase requires absolute URL - skip if siteUrl is relative path (like /cfg/admin)
189
+ // Only add if not already set in input metadata
190
+ if (!finalMetadata.metadataBase && siteUrl) {
191
+ // Check if siteUrl is an absolute URL (starts with http:// or https://)
192
+ if (siteUrl.startsWith('http://') || siteUrl.startsWith('https://')) {
193
+ try {
194
+ finalMetadata.metadataBase = new URL(siteUrl);
195
+ } catch (e) {
196
+ // If URL construction fails, skip metadataBase
197
+ // This shouldn't happen if we check for http/https, but just in case
198
+ }
199
+ }
200
+ }
201
+
202
+ return finalMetadata;
191
203
  }
192
204
 
193
205
  /**