@@ -240,7 +240,11 @@ export default async function initSanity(
240
240
throw new Error ( '`--reconfigure` is deprecated - manual configuration is now required' )
241
241
}
242
242
243
- const envFilename = typeof env === 'string' ? env : '.env'
243
+ let envFilenameDefault = '.env'
244
+ if ( detectedFramework && detectedFramework . slug === 'nextjs' ) {
245
+ envFilenameDefault = '.env.local'
246
+ }
247
+ const envFilename = typeof env === 'string' ? env : envFilenameDefault
244
248
if ( ! envFilename . startsWith ( '.env' ) ) {
245
249
throw new Error ( `Env filename must start with .env` )
246
250
}
@@ -433,10 +437,39 @@ export default async function initSanity(
433
437
const appendEnv = unattended ? true : await promptForAppendEnv ( prompt , envFilename )
434
438
435
439
if ( appendEnv ) {
436
- await createOrAppendEnvVars ( envFilename , detectedFramework , {
437
- log : true ,
440
+ await createOrAppendEnvVars ( envFilename , detectedFramework , { log : true } )
441
+ }
442
+
443
+ if ( embeddedStudio ) {
444
+ const nextjsLocalDevOrigin = 'http://localhost:3000'
445
+ const existingCorsOrigins = await apiClient ( { api : { projectId} } ) . request ( {
446
+ method : 'GET' ,
447
+ uri : '/cors' ,
438
448
} )
449
+ const hasExistingCorsOrigin = existingCorsOrigins . some (
450
+ ( item : { origin : string } ) => item . origin === nextjsLocalDevOrigin ,
451
+ )
452
+ if ( ! hasExistingCorsOrigin ) {
453
+ await apiClient ( { api : { projectId} } )
454
+ . request ( {
455
+ method : 'POST' ,
456
+ url : '/cors' ,
457
+ body : { origin : nextjsLocalDevOrigin , allowCredentials : true } ,
458
+ maxRedirects : 0 ,
459
+ } )
460
+ . then ( ( res ) => {
461
+ print (
462
+ res . id
463
+ ? `Added ${ nextjsLocalDevOrigin } to CORS origins`
464
+ : `Failed to add ${ nextjsLocalDevOrigin } to CORS origins` ,
465
+ )
466
+ } )
467
+ . catch ( ( error ) => {
468
+ print ( `Failed to add ${ nextjsLocalDevOrigin } to CORS origins` , error )
469
+ } )
470
+ }
439
471
}
472
+
440
473
const { chosen} = await getPackageManagerChoice ( workDir , { interactive : false } )
441
474
trace . log ( { step : 'selectPackageManager' , selectedOption : chosen } )
442
475
const packages = [ '@sanity/vision@3' , 'sanity@3' , '@sanity/image-url@1' , 'styled-components@6' ]
0 commit comments