1- import { readFileSync , writeFileSync } from 'node:fs' ;
1+ import { writeFileSync } from 'node:fs' ;
22import { join } from 'node:path' ;
33import { cwd } from 'node:process' ;
44import type { Config } from '@react-router/dev/config' ;
5- import { vercelPreset } from '@vercel/react-router/vite' ;
6- import { normalizePath } from 'vite' ;
75
86// Ensure we always have a valid dirname, even in Vercel's environment
97const dirname = cwd ( ) ;
@@ -13,97 +11,20 @@ const config: Config = {
1311 // Server-side render by default, to enable SPA mode set this to `false`
1412 ssr : true ,
1513 buildDirectory : 'dist' ,
16- presets : [ vercelPreset ( ) ] ,
17- buildEnd : async ( { buildManifest : rrBuild } ) => {
18- const manifestPath = join (
19- dirname ,
20- '.vercel/react-router-build-result.json' ,
21- ) ;
22-
23- /* read file contents */
24- const newBuildResult : {
25- // biome-ignore lint/suspicious/noExplicitAny: Won't bother with type since vercel might change it
26- buildManifest ?: { serverBundles ?: any ; routes ?: any } ;
27- viteConfig ?: { ssr ?: { noExternal ?: string [ ] } } ;
28- reactRouterConfig ?: typeof rrBuild & {
29- appDirectory ?: string ;
30- buildDirectory ?: string ;
31- } ;
32- } = { } ;
33- try {
34- const fileContents = readFileSync ( manifestPath , 'utf-8' ) ;
35- newBuildResult . buildManifest = JSON . parse ( fileContents ) . buildManifest ;
36- newBuildResult . viteConfig = JSON . parse ( fileContents ) . viteConfig ;
37- } catch ( error ) {
38- console . error ( `Error reading manifest file: ${ error } ` ) ;
39- return ;
40- }
41-
42- /* For every item in buildmanifest.serverBundles, add config.runtime = "nodejs" */
43- if ( newBuildResult . buildManifest ?. serverBundles ) {
44- // Use Object.values to get an array of the serverBundles objects
45- for ( const bundle of Object . values (
46- newBuildResult . buildManifest . serverBundles ,
47- ) ) {
48- const typedBundle = bundle as { config ?: { runtime ?: string } } ;
49- typedBundle . config = typedBundle . config || { } ;
50- typedBundle . config . runtime = 'nodejs' ;
51- }
52- }
53-
54- /* For every item in buildmanifest.routes, add config.runtime = "nodejs" */
55- if ( newBuildResult . buildManifest ?. routes ) {
56- // Use Object.values to get an array of the routes objects
57- for ( const route of Object . values ( newBuildResult . buildManifest . routes ) ) {
58- const typedRoute = route as { config ?: { runtime ?: string } } ;
59- typedRoute . config = typedRoute . config || { } ;
60- ( route as { config : { runtime : string } } ) . config . runtime = 'nodejs' ;
61- }
62- }
63-
64- if ( ! rrBuild ) {
65- console . error (
66- 'No react-router build result found. Skipping Vercel config update.' ,
67- ) ;
68- return ;
69- }
70-
71- newBuildResult . reactRouterConfig = rrBuild || { } ;
72- newBuildResult . reactRouterConfig . appDirectory = normalizePath (
73- join ( dirname , 'app' ) ,
74- ) ;
75- newBuildResult . reactRouterConfig . buildDirectory = normalizePath (
76- join ( dirname , 'dist' ) ,
77- ) ;
78-
79- // write back to the file
14+ presets : [ ] ,
15+ buildEnd : async ( ) => {
16+ const robotsPath = join ( dirname , 'public' , 'robots.txt' ) ;
17+ const robotsContent =
18+ process . env . VERCEL_ENV === 'production'
19+ ? `User-agent: *\nAllow: /`
20+ : `User-agent: *\nDisallow: /` ;
21+
22+ console . log ( `Writing robots.txt to ${ robotsPath } ` ) ;
8023 try {
81- writeFileSync ( manifestPath , JSON . stringify ( newBuildResult , null , 2 ) ) ;
24+ writeFileSync ( robotsPath , robotsContent ) ;
8225 } catch ( error ) {
83- console . error ( `Error writing manifest file: ${ error } ` ) ;
84- }
85-
86- if ( process . env . VERCEL_ENV === 'production' ) {
87- const robotsPath = join ( dirname , 'public' , 'robots.txt' ) ;
88- const robotsContent = `User-agent: *\nAllow: /` ;
89-
90- console . log ( `Writing production robots.txt to ${ robotsPath } ` ) ;
91- try {
92- writeFileSync ( robotsPath , robotsContent ) ;
93- } catch ( error ) {
94- console . error ( `Error writing robots.txt file: ${ error } ` ) ;
95- throw new Error ( `Failed to write robots.txt file: ${ error } ` ) ;
96- }
97- } else {
98- const robotsPath = join ( dirname , 'public' , 'robots.txt' ) ;
99- const robotsContent = `User-agent: *\nDisallow: /` ;
100-
101- console . log ( `Writing preview robots.txt to ${ robotsPath } ` ) ;
102- try {
103- writeFileSync ( robotsPath , robotsContent ) ;
104- } catch ( error ) {
105- console . error ( `Error writing robots.txt file: ${ error } ` ) ;
106- }
26+ console . error ( `Error writing robots.txt file: ${ error } ` ) ;
27+ throw new Error ( `Failed to write robots.txt file: ${ error } ` ) ;
10728 }
10829 } ,
10930} ;
0 commit comments