@djangocfg/nextjs 2.1.110 → 2.1.111

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.
Files changed (60) hide show
  1. package/README.md +176 -7
  2. package/dist/config/index.d.mts +16 -1
  3. package/dist/config/index.mjs +83 -14
  4. package/dist/config/index.mjs.map +1 -1
  5. package/dist/i18n/client.d.mts +123 -0
  6. package/dist/i18n/client.mjs +104 -0
  7. package/dist/i18n/client.mjs.map +1 -0
  8. package/dist/i18n/components.d.mts +22 -0
  9. package/dist/i18n/components.mjs +133 -0
  10. package/dist/i18n/components.mjs.map +1 -0
  11. package/dist/i18n/index.d.mts +17 -0
  12. package/dist/i18n/index.mjs +269 -0
  13. package/dist/i18n/index.mjs.map +1 -0
  14. package/dist/i18n/navigation.d.mts +1095 -0
  15. package/dist/i18n/navigation.mjs +45 -0
  16. package/dist/i18n/navigation.mjs.map +1 -0
  17. package/dist/i18n/plugin.d.mts +41 -0
  18. package/dist/i18n/plugin.mjs +17 -0
  19. package/dist/i18n/plugin.mjs.map +1 -0
  20. package/dist/i18n/provider.d.mts +18 -0
  21. package/dist/i18n/provider.mjs +54 -0
  22. package/dist/i18n/provider.mjs.map +1 -0
  23. package/dist/i18n/proxy.d.mts +40 -0
  24. package/dist/i18n/proxy.mjs +42 -0
  25. package/dist/i18n/proxy.mjs.map +1 -0
  26. package/dist/i18n/request.d.mts +42 -0
  27. package/dist/i18n/request.mjs +63 -0
  28. package/dist/i18n/request.mjs.map +1 -0
  29. package/dist/i18n/routing.d.mts +79 -0
  30. package/dist/i18n/routing.mjs +33 -0
  31. package/dist/i18n/routing.mjs.map +1 -0
  32. package/dist/i18n/server.d.mts +90 -0
  33. package/dist/i18n/server.mjs +79 -0
  34. package/dist/i18n/server.mjs.map +1 -0
  35. package/dist/index.d.mts +3 -1
  36. package/dist/index.mjs +176 -30
  37. package/dist/index.mjs.map +1 -1
  38. package/dist/sitemap/index.d.mts +22 -3
  39. package/dist/sitemap/index.mjs +92 -15
  40. package/dist/sitemap/index.mjs.map +1 -1
  41. package/dist/types-Cy349X20.d.mts +60 -0
  42. package/package.json +54 -4
  43. package/src/config/constants.ts +1 -0
  44. package/src/config/createNextConfig.ts +39 -17
  45. package/src/i18n/client.ts +221 -0
  46. package/src/i18n/components/LocaleSwitcher.tsx +124 -0
  47. package/src/i18n/components/index.ts +7 -0
  48. package/src/i18n/index.ts +149 -0
  49. package/src/i18n/navigation.ts +90 -0
  50. package/src/i18n/plugin.ts +66 -0
  51. package/src/i18n/provider.tsx +91 -0
  52. package/src/i18n/proxy.ts +81 -0
  53. package/src/i18n/request.ts +141 -0
  54. package/src/i18n/routing.ts +84 -0
  55. package/src/i18n/server.ts +175 -0
  56. package/src/i18n/types.ts +88 -0
  57. package/src/sitemap/generator.ts +84 -9
  58. package/src/sitemap/index.ts +1 -1
  59. package/src/sitemap/route.ts +71 -8
  60. package/src/sitemap/types.ts +9 -0
@@ -2,32 +2,107 @@
2
2
  * Sitemap Generator
3
3
  *
4
4
  * Generates XML sitemap from configuration
5
+ * Supports i18n with hreflang tags for multilingual sites
5
6
  */
6
7
 
7
8
  import type { SitemapUrl } from '../types';
9
+ import type { SitemapI18nOptions } from './types';
10
+
11
+ export interface GenerateSitemapXmlOptions {
12
+ urls: SitemapUrl[];
13
+ i18n?: SitemapI18nOptions;
14
+ siteUrl: string;
15
+ }
8
16
 
9
17
  /**
10
18
  * Generate XML sitemap string from URLs
19
+ * Supports i18n with hreflang alternate links
11
20
  */
12
- export function generateSitemapXml(urls: SitemapUrl[]): string {
13
- return `<?xml version="1.0" encoding="UTF-8"?>
14
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
21
+ export function generateSitemapXml(
22
+ urlsOrOptions: SitemapUrl[] | GenerateSitemapXmlOptions
23
+ ): string {
24
+ // Support both old signature (just urls) and new signature (options object)
25
+ const isOptionsObject = !Array.isArray(urlsOrOptions);
26
+ const urls = isOptionsObject ? urlsOrOptions.urls : urlsOrOptions;
27
+ const i18n = isOptionsObject ? urlsOrOptions.i18n : undefined;
28
+ const siteUrl = isOptionsObject ? urlsOrOptions.siteUrl : '';
29
+
30
+ // Add xhtml namespace if i18n is enabled
31
+ const namespaces = i18n
32
+ ? `xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
33
+ xmlns:xhtml="http://www.w3.org/1999/xhtml"
34
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
35
+ xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
36
+ http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"`
37
+ : `xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
15
38
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16
39
  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
17
- http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
40
+ http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"`;
41
+
42
+ return `<?xml version="1.0" encoding="UTF-8"?>
43
+ <urlset ${namespaces}>
18
44
  ${urls
19
- .map(
20
- ({ loc, lastmod, changefreq, priority }) => ` <url>
21
- <loc>${escapeXml(loc)}</loc>
45
+ .map(({ loc, lastmod, changefreq, priority }) => {
46
+ const hreflangLinks = i18n
47
+ ? generateHreflangLinks(loc, i18n, siteUrl)
48
+ : '';
49
+
50
+ return ` <url>
51
+ <loc>${escapeXml(loc)}</loc>${hreflangLinks}
22
52
  ${lastmod ? `<lastmod>${formatDate(lastmod)}</lastmod>` : ''}
23
53
  ${changefreq ? `<changefreq>${changefreq}</changefreq>` : ''}
24
54
  ${priority !== undefined ? `<priority>${priority.toFixed(1)}</priority>` : ''}
25
- </url>`
26
- )
55
+ </url>`;
56
+ })
27
57
  .join('\n')}
28
58
  </urlset>`;
29
59
  }
30
60
 
61
+ /**
62
+ * Generate hreflang links for a URL
63
+ */
64
+ function generateHreflangLinks(
65
+ loc: string,
66
+ i18n: SitemapI18nOptions,
67
+ siteUrl: string
68
+ ): string {
69
+ const { locales, defaultLocale } = i18n;
70
+
71
+ // Extract the path without locale prefix from the URL
72
+ // e.g., https://example.com/en/page -> /page
73
+ const baseSiteUrl = siteUrl.endsWith('/') ? siteUrl.slice(0, -1) : siteUrl;
74
+ let path = loc.replace(baseSiteUrl, '');
75
+
76
+ // Remove locale prefix if present
77
+ for (const locale of locales) {
78
+ const localePrefix = `/${locale}`;
79
+ if (path === localePrefix || path.startsWith(`${localePrefix}/`)) {
80
+ path = path.slice(localePrefix.length) || '/';
81
+ break;
82
+ }
83
+ }
84
+
85
+ const links: string[] = [];
86
+
87
+ // Add hreflang for each locale
88
+ for (const locale of locales) {
89
+ const localePath = path === '/' ? `/${locale}` : `/${locale}${path}`;
90
+ const fullUrl = `${baseSiteUrl}${localePath}`;
91
+ links.push(
92
+ ` <xhtml:link rel="alternate" hreflang="${locale}" href="${escapeXml(fullUrl)}"/>`
93
+ );
94
+ }
95
+
96
+ // Add x-default pointing to default locale
97
+ const defaultPath = path === '/' ? `/${defaultLocale}` : `/${defaultLocale}${path}`;
98
+ const defaultUrl = `${baseSiteUrl}${defaultPath}`;
99
+ links.push(
100
+ ` <xhtml:link rel="alternate" hreflang="x-default" href="${escapeXml(defaultUrl)}"/>`
101
+ );
102
+
103
+ return '\n' + links.join('\n');
104
+ }
105
+
31
106
  /**
32
107
  * Format date for sitemap (ISO 8601)
33
108
  */
@@ -4,5 +4,5 @@
4
4
 
5
5
  export { createSitemapHandler } from './route';
6
6
  export { generateSitemapXml, normalizeUrl } from './generator';
7
- export type { SitemapGeneratorOptions, SitemapRoute } from './types';
7
+ export type { SitemapGeneratorOptions, SitemapRoute, SitemapI18nOptions } from './types';
8
8
 
@@ -13,7 +13,6 @@
13
13
  * { loc: '/about', changefreq: 'monthly', priority: 0.8 },
14
14
  * ],
15
15
  * dynamicPages: async () => {
16
- * // Fetch dynamic pages from API
17
16
  * const posts = await fetchPosts();
18
17
  * return posts.map(post => ({
19
18
  * loc: `/posts/${post.slug}`,
@@ -22,6 +21,11 @@
22
21
  * priority: 0.7,
23
22
  * }));
24
23
  * },
24
+ * // i18n support with hreflang
25
+ * i18n: {
26
+ * locales: ['en', 'ru', 'ko'],
27
+ * defaultLocale: 'en',
28
+ * },
25
29
  * });
26
30
  * ```
27
31
  */
@@ -39,6 +43,7 @@ export function createSitemapHandler(options: SitemapGeneratorOptions) {
39
43
  staticPages = [],
40
44
  dynamicPages = [],
41
45
  cacheControl = 'public, s-maxage=86400, stale-while-revalidate',
46
+ i18n,
42
47
  } = options;
43
48
 
44
49
  return async function GET() {
@@ -54,14 +59,23 @@ export function createSitemapHandler(options: SitemapGeneratorOptions) {
54
59
  }
55
60
  }
56
61
 
57
- // Normalize all URLs
58
- const normalizedUrls = urls.map((url) => ({
59
- ...url,
60
- loc: normalizeUrl(url.loc, siteUrl),
61
- }));
62
+ // Expand URLs for each locale if i18n is enabled
63
+ let expandedUrls: SitemapUrl[];
64
+ if (i18n) {
65
+ expandedUrls = expandUrlsForLocales(urls, i18n.locales, siteUrl);
66
+ } else {
67
+ expandedUrls = urls.map((url) => ({
68
+ ...url,
69
+ loc: normalizeUrl(url.loc, siteUrl),
70
+ }));
71
+ }
62
72
 
63
- // Generate XML
64
- const sitemap = generateSitemapXml(normalizedUrls);
73
+ // Generate XML with i18n support
74
+ const sitemap = generateSitemapXml({
75
+ urls: expandedUrls,
76
+ i18n,
77
+ siteUrl,
78
+ });
65
79
 
66
80
  // Return response
67
81
  return new NextResponse(sitemap, {
@@ -74,3 +88,52 @@ export function createSitemapHandler(options: SitemapGeneratorOptions) {
74
88
  };
75
89
  }
76
90
 
91
+ /**
92
+ * Expand URLs to include all locale variants
93
+ * Input: [{ loc: '/page' }] with locales ['en', 'ru']
94
+ * Output: [{ loc: 'https://example.com/en/page' }, { loc: 'https://example.com/ru/page' }]
95
+ */
96
+ function expandUrlsForLocales(
97
+ urls: SitemapUrl[],
98
+ locales: string[],
99
+ siteUrl: string
100
+ ): SitemapUrl[] {
101
+ const baseSiteUrl = siteUrl.endsWith('/') ? siteUrl.slice(0, -1) : siteUrl;
102
+ const expandedUrls: SitemapUrl[] = [];
103
+
104
+ for (const url of urls) {
105
+ // Normalize the path (remove leading slash if present for consistency)
106
+ let path = url.loc;
107
+ if (path.startsWith(baseSiteUrl)) {
108
+ path = path.replace(baseSiteUrl, '');
109
+ }
110
+ if (!path.startsWith('/')) {
111
+ path = '/' + path;
112
+ }
113
+
114
+ // Check if the path already has a locale prefix
115
+ const hasLocalePrefix = locales.some(
116
+ (locale) => path === `/${locale}` || path.startsWith(`/${locale}/`)
117
+ );
118
+
119
+ if (hasLocalePrefix) {
120
+ // URL already has locale prefix, just normalize it
121
+ expandedUrls.push({
122
+ ...url,
123
+ loc: normalizeUrl(path, siteUrl),
124
+ });
125
+ } else {
126
+ // Create a URL for each locale
127
+ for (const locale of locales) {
128
+ const localePath = path === '/' ? `/${locale}` : `/${locale}${path}`;
129
+ expandedUrls.push({
130
+ ...url,
131
+ loc: `${baseSiteUrl}${localePath}`,
132
+ });
133
+ }
134
+ }
135
+ }
136
+
137
+ return expandedUrls;
138
+ }
139
+
@@ -4,11 +4,20 @@
4
4
 
5
5
  import type { ChangeFreq, SitemapUrl } from '../types';
6
6
 
7
+ export interface SitemapI18nOptions {
8
+ /** Supported locales (e.g., ['en', 'ru', 'ko']) */
9
+ locales: string[];
10
+ /** Default locale for x-default hreflang */
11
+ defaultLocale: string;
12
+ }
13
+
7
14
  export interface SitemapGeneratorOptions {
8
15
  siteUrl: string;
9
16
  staticPages?: SitemapUrl[];
10
17
  dynamicPages?: (() => Promise<SitemapUrl[]>) | SitemapUrl[];
11
18
  cacheControl?: string;
19
+ /** i18n configuration for hreflang support */
20
+ i18n?: SitemapI18nOptions;
12
21
  }
13
22
 
14
23
  export interface SitemapRoute {