mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-07-07 22:18:39 +00:00
Merge pull request #9960 from shadcn-ui/shadcn/update-track
feat: update handling of init urls
This commit is contained in:
5
.changeset/fluffy-poems-ask.md
Normal file
5
.changeset/fluffy-poems-ask.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"shadcn": patch
|
||||
---
|
||||
|
||||
update handling of init urls
|
||||
@@ -44,10 +44,12 @@ export async function GET(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
track("create_app", {
|
||||
...result.data,
|
||||
preset: presetCode,
|
||||
})
|
||||
if (searchParams.get("track") === "1") {
|
||||
track("create_app", {
|
||||
...result.data,
|
||||
preset: presetCode,
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json(parseResult.data)
|
||||
} catch (error) {
|
||||
|
||||
@@ -187,8 +187,11 @@ export const add = new Command()
|
||||
})
|
||||
|
||||
// Resolve registry:base config.
|
||||
const { registryBaseConfig, installStyleIndex } =
|
||||
await resolveRegistryBaseConfig(initUrl, options.cwd)
|
||||
const {
|
||||
registryBaseConfig,
|
||||
installStyleIndex,
|
||||
url: cleanInitUrl,
|
||||
} = await resolveRegistryBaseConfig(initUrl, options.cwd)
|
||||
|
||||
config = await runInit({
|
||||
cwd: options.cwd,
|
||||
@@ -201,7 +204,7 @@ export const add = new Command()
|
||||
cssVariables: true,
|
||||
rtl: false,
|
||||
installStyleIndex,
|
||||
components: [initUrl, ...(options.components ?? [])],
|
||||
components: [cleanInitUrl, ...(options.components ?? [])],
|
||||
registryBaseConfig,
|
||||
})
|
||||
initHasRun = true
|
||||
@@ -228,8 +231,11 @@ export const add = new Command()
|
||||
base: selectedBase,
|
||||
template,
|
||||
})
|
||||
const { registryBaseConfig, installStyleIndex } =
|
||||
await resolveRegistryBaseConfig(initUrl, options.cwd)
|
||||
const {
|
||||
registryBaseConfig,
|
||||
installStyleIndex,
|
||||
url: cleanInitUrl,
|
||||
} = await resolveRegistryBaseConfig(initUrl, options.cwd)
|
||||
|
||||
config = await runInit({
|
||||
cwd: options.cwd,
|
||||
@@ -242,7 +248,7 @@ export const add = new Command()
|
||||
cssVariables: true,
|
||||
rtl: false,
|
||||
installStyleIndex,
|
||||
components: [initUrl, ...(options.components ?? [])],
|
||||
components: [cleanInitUrl, ...(options.components ?? [])],
|
||||
registryBaseConfig,
|
||||
})
|
||||
initHasRun = true
|
||||
|
||||
@@ -384,6 +384,9 @@ export const init = new Command()
|
||||
} else if (options.rtl === false) {
|
||||
url.searchParams.delete("rtl")
|
||||
}
|
||||
if (url.pathname === "/init" && presetArg.startsWith(SHADCN_URL)) {
|
||||
url.searchParams.set("track", "1")
|
||||
}
|
||||
initUrl = url.toString()
|
||||
presetBase = url.searchParams.get("base") ?? undefined
|
||||
} else if (isPresetCode(presetArg)) {
|
||||
@@ -512,12 +515,18 @@ export const init = new Command()
|
||||
}
|
||||
|
||||
// Resolve registry:base config from the first component.
|
||||
const { registryBaseConfig, installStyleIndex } =
|
||||
await resolveRegistryBaseConfig(components[0], cwd, {
|
||||
registries: existingConfig?.registries as
|
||||
| z.infer<typeof registryConfigSchema>
|
||||
| undefined,
|
||||
})
|
||||
const {
|
||||
registryBaseConfig,
|
||||
installStyleIndex,
|
||||
url: cleanUrl,
|
||||
} = await resolveRegistryBaseConfig(components[0], cwd, {
|
||||
registries: existingConfig?.registries as
|
||||
| z.infer<typeof registryConfigSchema>
|
||||
| undefined,
|
||||
})
|
||||
|
||||
// Use the clean URL (track param stripped) for subsequent fetches.
|
||||
components[0] = cleanUrl
|
||||
|
||||
if (!installStyleIndex) {
|
||||
options.installStyleIndex = false
|
||||
|
||||
@@ -3,6 +3,7 @@ import { buildUrlAndHeadersForRegistryItem } from "@/src/registry/builder"
|
||||
import { configWithDefaults } from "@/src/registry/config"
|
||||
import { REGISTRY_URL, SHADCN_URL } from "@/src/registry/constants"
|
||||
import { type registryConfigSchema } from "@/src/registry/schema"
|
||||
import { isUrl } from "@/src/registry/utils"
|
||||
import { createConfig } from "@/src/utils/get-config"
|
||||
import { highlighter } from "@/src/utils/highlighter"
|
||||
import { logger } from "@/src/utils/logger"
|
||||
@@ -141,6 +142,9 @@ export function resolveInitUrl(
|
||||
params.set("template", options.template)
|
||||
}
|
||||
|
||||
// Signal the server to record this init run.
|
||||
params.set("track", "1")
|
||||
|
||||
return `${SHADCN_URL}/init?${params.toString()}`
|
||||
}
|
||||
|
||||
@@ -272,8 +276,25 @@ export async function resolveRegistryBaseConfig(
|
||||
const registryBaseConfig =
|
||||
item?.type === "registry:base" && item.config ? item.config : undefined
|
||||
|
||||
// Strip the track param so subsequent fetches don't re-trigger tracking.
|
||||
let cleanUrl = initUrl
|
||||
if (isShadcnInitUrl(initUrl)) {
|
||||
const url = new URL(initUrl)
|
||||
url.searchParams.delete("track")
|
||||
cleanUrl = url.toString()
|
||||
}
|
||||
|
||||
return {
|
||||
registryBaseConfig,
|
||||
installStyleIndex: item?.extends !== "none",
|
||||
url: cleanUrl,
|
||||
}
|
||||
}
|
||||
|
||||
function isShadcnInitUrl(url: string) {
|
||||
try {
|
||||
return new URL(url).pathname === "/init" && url.startsWith(SHADCN_URL)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user