From a2e645ac4d1d8165fbba0b52e5be9ddd2646401c Mon Sep 17 00:00:00 2001 From: Jade Devin Cabatlao Date: Wed, 4 Dec 2024 00:56:04 +0100 Subject: [PATCH] refactor(start): expose vinxi router middleware (#2893) Co-authored-by: Sean Cassiere <33615041+SeanCassiere@users.noreply.github.com> --- packages/start/src/config/index.ts | 15 +++++++++++---- packages/start/src/config/schema.ts | 3 +++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/start/src/config/index.ts b/packages/start/src/config/index.ts index 2d7cd57da9..21afad0b34 100644 --- a/packages/start/src/config/index.ts +++ b/packages/start/src/config/index.ts @@ -104,9 +104,16 @@ export function defineConfig( const tsrConfig = getConfig(tsr) + const publicDir = opts.routers?.public?.dir || './public' + + const publicBase = opts.routers?.public?.base || '/' const clientBase = opts.routers?.client?.base || '/_build' - const serverBase = opts.routers?.server?.base || '/_server' const apiBase = opts.tsr?.apiBase || '/api' + const serverBase = opts.routers?.server?.base || '/_server' + + const apiMiddleware = opts.routers?.api?.middleware || undefined + const serverMiddleware = opts.routers?.server?.middleware || undefined + const ssrMiddleware = opts.routers?.ssr?.middleware || undefined const clientEntry = opts.routers?.client?.entry || path.join(appDirectory, 'client.tsx') @@ -116,9 +123,6 @@ export function defineConfig( const apiEntryExists = existsSync(apiEntry) - const publicDir = opts.routers?.public?.dir || './public' - const publicBase = opts.routers?.public?.base || '/' - let vinxiApp = createApp({ server: { ...serverOptions, @@ -187,6 +191,7 @@ export function defineConfig( type: 'http', target: 'server', handler: ssrEntry, + middleware: ssrMiddleware, plugins: () => { const viteConfig = getUserViteConfig(opts.vite) const ssrViteConfig = getUserViteConfig(opts.routers?.ssr?.vite) @@ -232,6 +237,7 @@ export function defineConfig( type: 'http', target: 'server', base: serverBase, + middleware: serverMiddleware, // TODO: RSCS - enable this // worker: true, handler: importToProjectRelative('@tanstack/start/server-handler'), @@ -288,6 +294,7 @@ export function defineConfig( target: 'server', base: apiBase, handler: apiEntry, + middleware: apiMiddleware, routes: tanstackStartVinxiFileRouter({ tsrConfig, apiBase }), plugins: () => { const viteConfig = getUserViteConfig(opts.vite) diff --git a/packages/start/src/config/schema.ts b/packages/start/src/config/schema.ts index 26b23c17cf..d07704fe03 100644 --- a/packages/start/src/config/schema.ts +++ b/packages/start/src/config/schema.ts @@ -150,6 +150,7 @@ const routersSchema = z.object({ ssr: z .object({ entry: z.string().optional(), + middleware: z.string().optional(), vite: viteSchema.optional(), }) .optional(), @@ -163,12 +164,14 @@ const routersSchema = z.object({ server: z .object({ base: z.string().optional(), + middleware: z.string().optional(), vite: viteSchema.optional(), }) .optional(), api: z .object({ entry: z.string().optional(), + middleware: z.string().optional(), vite: viteSchema.optional(), }) .optional(),