2023-12-12 14:46:53 +01:00
|
|
|
import { writeFileSync } from 'fs';
|
2023-12-12 11:28:20 +01:00
|
|
|
import { redirects } from './../assets/data/old-hub-redirects';
|
|
|
|
import { localesConst } from './../assets/data/locales';
|
|
|
|
import type { LocaleCodes } from './../assets/data/locales';
|
2024-02-09 13:15:12 +01:00
|
|
|
import type { PartialRecord } from './../types/others';
|
2023-12-12 11:28:20 +01:00
|
|
|
import type { NuxtConfig } from 'nuxt/schema';
|
2023-12-12 14:46:53 +01:00
|
|
|
import { joinURL, cleanDoubleSlashes } from 'ufo';
|
2023-12-12 11:28:20 +01:00
|
|
|
|
2023-12-12 14:46:53 +01:00
|
|
|
type VercelRouteSource = {
|
|
|
|
src: string;
|
|
|
|
dest?: string;
|
|
|
|
headers?: Record<string, string>;
|
|
|
|
methods?: string[];
|
|
|
|
continue?: boolean;
|
|
|
|
caseSensitive?: boolean;
|
|
|
|
check?: boolean;
|
|
|
|
status?: number;
|
|
|
|
has?: Array<unknown>;
|
|
|
|
missing?: Array<unknown>;
|
|
|
|
locale?: {
|
|
|
|
redirect?: Record<string, string>;
|
|
|
|
cookie?: string;
|
|
|
|
};
|
|
|
|
middlewareRawSrc?: string[];
|
|
|
|
middlewarePath?: string;
|
|
|
|
};
|
|
|
|
|
2023-12-23 17:00:23 +01:00
|
|
|
|
|
|
|
export function getOldHubRedirects(mode: 'nitro'): NuxtConfig['routeRules']
|
|
|
|
export function getOldHubRedirects(mode: 'vercel'): VercelRouteSource[]
|
|
|
|
export function getOldHubRedirects(mode: 'nitro' | 'vercel' = 'nitro'): NuxtConfig['routeRules'] | VercelRouteSource[] {
|
2023-12-12 11:28:20 +01:00
|
|
|
|
|
|
|
// 旧Hub時代の各言語のプレフィックス
|
2024-02-09 13:15:12 +01:00
|
|
|
const hubLocales: PartialRecord<LocaleCodes, string> = {
|
2023-12-12 11:28:20 +01:00
|
|
|
ja: '/',
|
|
|
|
en: '/en',
|
|
|
|
id: '/id',
|
|
|
|
ko: '/ko',
|
|
|
|
it: '/it',
|
|
|
|
pl: '/pl',
|
|
|
|
fr: '/fr',
|
|
|
|
cn: '/zh-CN',
|
|
|
|
tw: '/zh-TW',
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-12-23 17:00:23 +01:00
|
|
|
if (mode === 'vercel') {
|
|
|
|
const out: VercelRouteSource[] = [];
|
2023-12-12 11:28:20 +01:00
|
|
|
|
2023-12-23 17:00:23 +01:00
|
|
|
redirects.forEach((route) => {
|
|
|
|
if (route[0].startsWith('/ns')) return;
|
2023-12-12 11:28:20 +01:00
|
|
|
|
2023-12-23 17:00:23 +01:00
|
|
|
let destination = route[1];
|
|
|
|
|
|
|
|
if (!new RegExp(`^/(${localesConst.map((e) => e.code).join('|')})/`, 'g').test(destination)) {
|
|
|
|
destination = joinURL(`$1/`, destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
out.push({
|
|
|
|
src: joinURL(`(${Object.values(hubLocales).map((v) => v === '/' ? '' : v).join('|')})/`, route[0]).replace(/(?<!\\)\./g, '\\.'),
|
|
|
|
headers: {
|
|
|
|
'Location': destination,
|
|
|
|
},
|
|
|
|
status: 308,
|
|
|
|
});
|
|
|
|
});
|
2023-12-12 11:28:20 +01:00
|
|
|
|
2023-12-12 14:46:53 +01:00
|
|
|
out.push({
|
2023-12-23 17:00:23 +01:00
|
|
|
src: '/zh-CN/(.*)',
|
2023-12-12 14:46:53 +01:00
|
|
|
headers: {
|
2023-12-23 17:00:23 +01:00
|
|
|
'Location': '/cn/$1',
|
2023-12-12 14:46:53 +01:00
|
|
|
},
|
2023-12-23 17:00:23 +01:00
|
|
|
status: 307,
|
|
|
|
}, {
|
|
|
|
src: '/zh-TW/(.*)',
|
|
|
|
headers: {
|
|
|
|
'Location': '/tw/$1',
|
|
|
|
},
|
|
|
|
status: 307,
|
2023-12-12 11:28:20 +01:00
|
|
|
});
|
|
|
|
|
2023-12-23 17:00:23 +01:00
|
|
|
return out;
|
|
|
|
} else {
|
|
|
|
const out: NuxtConfig['routeRules'] = {};
|
2023-12-12 14:46:53 +01:00
|
|
|
|
2023-12-23 17:00:23 +01:00
|
|
|
localesConst.forEach((locale) => {
|
|
|
|
redirects.forEach((route) => {
|
|
|
|
if (route[0].startsWith('/ns')) return;
|
|
|
|
|
|
|
|
let destination = route[1];
|
|
|
|
|
|
|
|
if (route[0].endsWith('.html') && !new RegExp(`^/(${localesConst.map((e) => e.code).join('|')})/`, 'g').test(destination)) {
|
|
|
|
destination = joinURL(`/${locale.code}`, destination);
|
|
|
|
}
|
|
|
|
|
2024-02-09 13:15:12 +01:00
|
|
|
out[joinURL(hubLocales[locale.code] ?? '/', route[0])] = {
|
2023-12-23 17:00:23 +01:00
|
|
|
redirect: {
|
|
|
|
to: destination,
|
|
|
|
statusCode: 301,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...out,
|
|
|
|
/* See: https://github.com/unjs/nitro/pull/1976
|
|
|
|
'/zh-CN/**': { redirect: '/cn/**' },
|
|
|
|
'/zh-TW/**': { redirect: '/tw/**' },
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 11:28:20 +01:00
|
|
|
}
|