PronounsPage/app/router.options.ts
Valentyne Stigloher 86c59d7d82 (nuxt) replace mechanism in router.options.ts for locale-specific noun pages with route.meta.translatedPaths
now possible because Nuxt scans definePageMeta for pages added by pages:extend
2025-08-17 18:13:59 +02:00

39 lines
1.5 KiB
TypeScript

import type { RouterOptions } from '@nuxt/schema';
import { loadConfig } from '~/src/data.ts';
const routerOptions: RouterOptions = {
routes: async (routes) => {
if (process.env.TEST) {
// workaround as there is no way to retrieve the original page routes
// which get overridden in the nuxt test environment by this router options
global.originalRoutes = routes;
}
// must use loadConfig() instead of useConfig() because routes are resolved
// before user plugins with enforce: pre are run
const config = await loadConfig();
return routes.flatMap((route) => {
if (route.meta?.translatedPaths) {
const translatedPaths = route.meta.translatedPaths(config);
if (translatedPaths.length === 0) {
return [];
}
if (Array.isArray(translatedPaths)) {
return [{ ...route, path: translatedPaths[0], alias: translatedPaths.slice(1) }];
}
return Object.entries(translatedPaths).map(([name, translatedRoute]) => {
return {
...route,
name,
path: translatedRoute.paths[0],
alias: translatedRoute.paths.slice(1),
meta: translatedRoute.meta,
};
});
}
return [route];
});
},
};
export default routerOptions;