Skip to content

Commit de03c57

Browse files
SimeonGriggsbjoerge
authored andcommitted
fix(cli) use Next.js specific defaults for new projects (#7465)
1 parent ea3a79a commit de03c57

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

packages/@sanity/cli/src/actions/init-project/initProject.ts

+36-3
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ export default async function initSanity(
240240
throw new Error('`--reconfigure` is deprecated - manual configuration is now required')
241241
}
242242

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
244248
if (!envFilename.startsWith('.env')) {
245249
throw new Error(`Env filename must start with .env`)
246250
}
@@ -433,10 +437,39 @@ export default async function initSanity(
433437
const appendEnv = unattended ? true : await promptForAppendEnv(prompt, envFilename)
434438

435439
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',
438448
})
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+
}
439471
}
472+
440473
const {chosen} = await getPackageManagerChoice(workDir, {interactive: false})
441474
trace.log({step: 'selectPackageManager', selectedOption: chosen})
442475
const packages = ['@sanity/vision@3', 'sanity@3', '@sanity/image-url@1', 'styled-components@6']

0 commit comments

Comments
 (0)