Skip to content

Commit

Permalink
chore: better routes merge
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Oct 16, 2023
1 parent 0ae1001 commit c97bd01
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions packages/vercel/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import fs from 'fs/promises';
import {
getTransformedRoutes,
Header,
mergeRoutes,
normalizeRoutes,
Rewrite,
Route,
} from '@vercel/routing-utils';
import { ViteVercelRewrite } from './types';

Expand Down Expand Up @@ -54,23 +56,48 @@ export function getConfig(
) {
console.warn(
'It is discouraged to use `vercel.config.routes` to override routes. ' +
'Prefer using `vercel.rewrites` and `vercel.redirects`.',
'Prefer using `vercel.rewrites`, `vercel.redirects` and `vercel.appendRoutesToPhase`.',
);
}

const cleanRoutes = normalizeRoutes([
...(routes ?? []),
...(resolvedConfig.vercel?.config?.routes ?? []),
]);
let userRoutes: Route[] = [];
let buildRoutes: Route[] = [];

if (cleanRoutes.error) {
throw cleanRoutes.error;
if (resolvedConfig.vercel?.config?.routes) {
const norm = normalizeRoutes(resolvedConfig.vercel.config.routes);

if (norm.error) {
throw norm.error;
}

userRoutes = norm.routes ?? [];
}

if (resolvedConfig.vercel?.config?.routes) {
const norm = normalizeRoutes(routes);

if (norm.error) {
throw norm.error;
}

buildRoutes = norm.routes ?? [];
}

const cleanRoutes = mergeRoutes({
userRoutes,
builds: [
{
use: '@vercel/node',
entrypoint: 'index.js',
routes: buildRoutes,
},
],
});

return vercelOutputConfigSchema.parse({
version: 3,
...resolvedConfig.vercel?.config,
routes: cleanRoutes.routes,
routes: cleanRoutes,
overrides: {
...resolvedConfig.vercel?.config?.overrides,
...overrides,
Expand Down

0 comments on commit c97bd01

Please sign in to comment.