@djangocfg/nextjs 2.1.412 → 2.1.413
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/README.md +30 -36
- package/dist/config/index.mjs +1 -1
- package/dist/config/index.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +110 -137
- package/dist/index.mjs.map +1 -1
- package/dist/sitemap/index.d.mts +126 -56
- package/dist/sitemap/index.mjs +107 -134
- package/dist/sitemap/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/sitemap/README.md +315 -0
- package/src/sitemap/fetch.ts +66 -0
- package/src/sitemap/ids.ts +21 -0
- package/src/sitemap/index.ts +22 -4
- package/src/sitemap/robots.ts +34 -0
- package/src/sitemap/sitemap.ts +94 -0
- package/src/sitemap/types.ts +67 -17
- package/src/sitemap/generator.ts +0 -144
- package/src/sitemap/route.ts +0 -146
package/src/sitemap/generator.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sitemap Generator
|
|
3
|
-
*
|
|
4
|
-
* Generates XML sitemap from configuration
|
|
5
|
-
* Supports i18n with hreflang tags for multilingual sites
|
|
6
|
-
*/
|
|
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
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Generate XML sitemap string from URLs
|
|
19
|
-
* Supports i18n with hreflang alternate links
|
|
20
|
-
*/
|
|
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"
|
|
38
|
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
39
|
-
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
|
|
40
|
-
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"`;
|
|
41
|
-
|
|
42
|
-
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
43
|
-
<urlset ${namespaces}>
|
|
44
|
-
${urls
|
|
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}
|
|
52
|
-
${lastmod ? `<lastmod>${formatDate(lastmod)}</lastmod>` : ''}
|
|
53
|
-
${changefreq ? `<changefreq>${changefreq}</changefreq>` : ''}
|
|
54
|
-
${priority !== undefined ? `<priority>${priority.toFixed(1)}</priority>` : ''}
|
|
55
|
-
</url>`;
|
|
56
|
-
})
|
|
57
|
-
.join('\n')}
|
|
58
|
-
</urlset>`;
|
|
59
|
-
}
|
|
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
|
-
// Default locale gets no prefix (localePrefix: 'as-needed')
|
|
89
|
-
for (const locale of locales) {
|
|
90
|
-
const localePath =
|
|
91
|
-
locale === defaultLocale
|
|
92
|
-
? path
|
|
93
|
-
: path === '/'
|
|
94
|
-
? `/${locale}`
|
|
95
|
-
: `/${locale}${path}`;
|
|
96
|
-
const fullUrl = `${baseSiteUrl}${localePath}`;
|
|
97
|
-
links.push(
|
|
98
|
-
` <xhtml:link rel="alternate" hreflang="${locale}" href="${escapeXml(fullUrl)}"/>`
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Add x-default pointing to default locale (no prefix)
|
|
103
|
-
const defaultUrl = `${baseSiteUrl}${path}`;
|
|
104
|
-
links.push(
|
|
105
|
-
` <xhtml:link rel="alternate" hreflang="x-default" href="${escapeXml(defaultUrl)}"/>`
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
return '\n' + links.join('\n');
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Format date for sitemap (ISO 8601)
|
|
113
|
-
*/
|
|
114
|
-
function formatDate(date: string | Date): string {
|
|
115
|
-
if (typeof date === 'string') {
|
|
116
|
-
return date;
|
|
117
|
-
}
|
|
118
|
-
return date.toISOString().split('T')[0];
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Escape XML special characters
|
|
123
|
-
*/
|
|
124
|
-
function escapeXml(unsafe: string): string {
|
|
125
|
-
return unsafe
|
|
126
|
-
.replace(/&/g, '&')
|
|
127
|
-
.replace(/</g, '<')
|
|
128
|
-
.replace(/>/g, '>')
|
|
129
|
-
.replace(/"/g, '"')
|
|
130
|
-
.replace(/'/g, ''');
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Normalize URL (ensure absolute)
|
|
135
|
-
*/
|
|
136
|
-
export function normalizeUrl(url: string, siteUrl: string): string {
|
|
137
|
-
if (url.startsWith('http://') || url.startsWith('https://')) {
|
|
138
|
-
return url;
|
|
139
|
-
}
|
|
140
|
-
const baseUrl = siteUrl.endsWith('/') ? siteUrl.slice(0, -1) : siteUrl;
|
|
141
|
-
const path = url.startsWith('/') ? url : `/${url}`;
|
|
142
|
-
return `${baseUrl}${path}`;
|
|
143
|
-
}
|
|
144
|
-
|
package/src/sitemap/route.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sitemap Route Handler for Next.js App Router
|
|
3
|
-
*
|
|
4
|
-
* Usage:
|
|
5
|
-
* ```tsx
|
|
6
|
-
* // app/sitemap.ts
|
|
7
|
-
* import { createSitemapHandler } from '@djangocfg/nextjs/sitemap';
|
|
8
|
-
*
|
|
9
|
-
* export default createSitemapHandler({
|
|
10
|
-
* siteUrl: 'https://example.com',
|
|
11
|
-
* staticPages: [
|
|
12
|
-
* { loc: '/', changefreq: 'daily', priority: 1.0 },
|
|
13
|
-
* { loc: '/about', changefreq: 'monthly', priority: 0.8 },
|
|
14
|
-
* ],
|
|
15
|
-
* dynamicPages: async () => {
|
|
16
|
-
* const posts = await fetchPosts();
|
|
17
|
-
* return posts.map(post => ({
|
|
18
|
-
* loc: `/posts/${post.slug}`,
|
|
19
|
-
* lastmod: post.updatedAt,
|
|
20
|
-
* changefreq: 'weekly',
|
|
21
|
-
* priority: 0.7,
|
|
22
|
-
* }));
|
|
23
|
-
* },
|
|
24
|
-
* // i18n support with hreflang
|
|
25
|
-
* i18n: {
|
|
26
|
-
* locales: ['en', 'ru', 'ko'],
|
|
27
|
-
* defaultLocale: 'en',
|
|
28
|
-
* },
|
|
29
|
-
* });
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import { NextResponse } from 'next/server';
|
|
34
|
-
|
|
35
|
-
import { generateSitemapXml, normalizeUrl } from './generator';
|
|
36
|
-
|
|
37
|
-
import type { SitemapGeneratorOptions } from './types';
|
|
38
|
-
import type { SitemapUrl } from '../types';
|
|
39
|
-
|
|
40
|
-
export function createSitemapHandler(options: SitemapGeneratorOptions) {
|
|
41
|
-
const {
|
|
42
|
-
siteUrl,
|
|
43
|
-
staticPages = [],
|
|
44
|
-
dynamicPages = [],
|
|
45
|
-
cacheControl = 'public, s-maxage=86400, stale-while-revalidate',
|
|
46
|
-
i18n,
|
|
47
|
-
} = options;
|
|
48
|
-
|
|
49
|
-
return async function GET() {
|
|
50
|
-
const urls: SitemapUrl[] = [...staticPages];
|
|
51
|
-
|
|
52
|
-
// Add dynamic pages
|
|
53
|
-
if (dynamicPages) {
|
|
54
|
-
if (typeof dynamicPages === 'function') {
|
|
55
|
-
const dynamicUrls = await dynamicPages();
|
|
56
|
-
urls.push(...dynamicUrls);
|
|
57
|
-
} else {
|
|
58
|
-
urls.push(...dynamicPages);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
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, i18n.defaultLocale);
|
|
66
|
-
} else {
|
|
67
|
-
expandedUrls = urls.map((url) => ({
|
|
68
|
-
...url,
|
|
69
|
-
loc: normalizeUrl(url.loc, siteUrl),
|
|
70
|
-
}));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Generate XML with i18n support
|
|
74
|
-
const sitemap = generateSitemapXml({
|
|
75
|
-
urls: expandedUrls,
|
|
76
|
-
i18n,
|
|
77
|
-
siteUrl,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
// Return response
|
|
81
|
-
return new NextResponse(sitemap, {
|
|
82
|
-
status: 200,
|
|
83
|
-
headers: {
|
|
84
|
-
'Content-Type': 'application/xml',
|
|
85
|
-
'Cache-Control': cacheControl,
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
}
|
|
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
|
-
defaultLocale?: string
|
|
101
|
-
): SitemapUrl[] {
|
|
102
|
-
const baseSiteUrl = siteUrl.endsWith('/') ? siteUrl.slice(0, -1) : siteUrl;
|
|
103
|
-
const expandedUrls: SitemapUrl[] = [];
|
|
104
|
-
|
|
105
|
-
for (const url of urls) {
|
|
106
|
-
// Normalize the path (remove leading slash if present for consistency)
|
|
107
|
-
let path = url.loc;
|
|
108
|
-
if (path.startsWith(baseSiteUrl)) {
|
|
109
|
-
path = path.replace(baseSiteUrl, '');
|
|
110
|
-
}
|
|
111
|
-
if (!path.startsWith('/')) {
|
|
112
|
-
path = '/' + path;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Check if the path already has a locale prefix
|
|
116
|
-
const hasLocalePrefix = locales.some(
|
|
117
|
-
(locale) => path === `/${locale}` || path.startsWith(`/${locale}/`)
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
if (hasLocalePrefix) {
|
|
121
|
-
// URL already has locale prefix, just normalize it
|
|
122
|
-
expandedUrls.push({
|
|
123
|
-
...url,
|
|
124
|
-
loc: normalizeUrl(path, siteUrl),
|
|
125
|
-
});
|
|
126
|
-
} else {
|
|
127
|
-
// Create a URL for each locale
|
|
128
|
-
// Default locale gets no prefix (localePrefix: 'as-needed')
|
|
129
|
-
for (const locale of locales) {
|
|
130
|
-
const localePath =
|
|
131
|
-
locale === defaultLocale
|
|
132
|
-
? path
|
|
133
|
-
: path === '/'
|
|
134
|
-
? `/${locale}`
|
|
135
|
-
: `/${locale}${path}`;
|
|
136
|
-
expandedUrls.push({
|
|
137
|
-
...url,
|
|
138
|
-
loc: `${baseSiteUrl}${localePath}`,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return expandedUrls;
|
|
145
|
-
}
|
|
146
|
-
|