Skip to content

Commit 6f35eb6

Browse files
committed
fix: don't hardcode npmx.dev & docs.npmx.dev
Extract a composable that returns URLs for this app itself (and docs, which lives in this codebase), so that URLs are always internally consistent within an environment, e.g. in dev links go to dev.
1 parent 8ee3608 commit 6f35eb6

13 files changed

Lines changed: 51 additions & 23 deletions

File tree

app/app.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ useHead({
4444
})
4545
4646
if (import.meta.server) {
47-
setJsonLd(createWebSiteSchema())
47+
const { siteUrl } = useAppUrls()
48+
setJsonLd(createWebSiteSchema(siteUrl))
4849
}
4950
5051
onKeyDown(

app/components/AppFooter.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup lang="ts">
2+
const { docsUrl } = useAppUrls()
23
const route = useRoute()
34
const isHome = computed(() => route.name === 'index')
45
@@ -92,7 +93,7 @@ const showModal = () => modalRef.value?.showModal?.()
9293
</li>
9394
</ul>
9495
</Modal>
95-
<LinkBase to="https://docs.npmx.dev">
96+
<LinkBase :to="docsUrl">
9697
{{ $t('footer.docs') }}
9798
</LinkBase>
9899
<LinkBase to="https://repo.npmx.dev">

app/components/AppHeader.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ withDefaults(
1212
},
1313
)
1414
15+
const { docsUrl } = useAppUrls()
1516
const { isConnected, npmUser } = useConnector()
1617
1718
const desktopLinks = computed<NavigationConfig>(() => [
@@ -85,7 +86,7 @@ const mobileLinks = computed<NavigationConfigWithGroups>(() => [
8586
{
8687
name: 'Docs',
8788
label: $t('footer.docs'),
88-
href: 'https://docs.npmx.dev',
89+
href: docsUrl,
8990
target: '_blank',
9091
type: 'link',
9192
external: true,

app/components/Package/SkillsModal.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ function toggleSkill(dirName: string) {
2727
type InstallMethod = 'skills-npm' | 'skills-cli'
2828
const selectedMethod = ref<InstallMethod>('skills-npm')
2929
30-
const baseUrl = computed(() =>
31-
typeof window !== 'undefined' ? window.location.origin : 'https://npmx.dev',
32-
)
30+
const { siteUrl } = useAppUrls()
31+
const baseUrl = computed(() => (typeof window !== 'undefined' ? window.location.origin : siteUrl))
3332
3433
const installCommand = computed(() => {
3534
if (!props.skills.length) return null

app/composables/useAppUrls.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NPMX_DOCS_SITE_PROD } from '#shared/utils/constants'
2+
3+
export function useAppUrls() {
4+
const { env, siteUrl } = useAppConfig()
5+
return {
6+
siteUrl,
7+
// TODO(serhalp): Handle preview environment. The docs site is a separate deployment, so we'd
8+
// need to infer its preview URL from the site preview URL somehow...?
9+
docsUrl: env === 'dev' ? 'http://localhost:3001' : NPMX_DOCS_SITE_PROD,
10+
}
11+
}

app/composables/useJsonLd.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ export function setJsonLd(schema: WithContext<Thing> | WithContext<Thing>[]): vo
1818
/**
1919
* Create WebSite schema with search action
2020
*/
21-
export function createWebSiteSchema(options?: {
22-
name?: string
23-
description?: string
24-
}): WithContext<WebSite> {
25-
const siteUrl = 'https://npmx.dev'
21+
export function createWebSiteSchema(
22+
siteUrl: string,
23+
options?: {
24+
name?: string
25+
description?: string
26+
},
27+
): WithContext<WebSite> {
2628
return {
2729
'@context': 'https://schema.org',
2830
'@type': 'WebSite',

app/pages/org/[org].vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ function handleClearFilter(chip: FilterChip) {
129129
const activeTab = shallowRef<'members' | 'teams'>('members')
130130
131131
// Canonical URL for this org page
132-
const canonicalUrl = computed(() => `https://npmx.dev/@${orgName.value}`)
132+
const { siteUrl } = useAppUrls()
133+
const canonicalUrl = computed(() => `${siteUrl}/@${orgName.value}`)
133134
134135
useHead({
135136
link: [{ rel: 'canonical', href: canonicalUrl }],

app/pages/package-code/[[org]]/[packageName]/v/[version]/[...filePath].vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ function copyPermalinkUrl() {
249249
}
250250
251251
// Canonical URL for this code page
252-
const canonicalUrl = computed(() => `https://npmx.dev${getCodeUrl(route.params)}`)
252+
const { siteUrl } = useAppUrls()
253+
const canonicalUrl = computed(() => `${siteUrl}${getCodeUrl(route.params)}`)
253254
254255
// Toggle markdown view mode
255256
const markdownViewModes = [

app/pages/package/[[org]]/[name].vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,9 @@ const createPackageInfo = computed(() => {
471471
})
472472
473473
// Canonical URL for this package page
474+
const { siteUrl } = useAppUrls()
474475
const canonicalUrl = computed(() => {
475-
const base = `https://npmx.dev/package/${packageName.value}`
476+
const base = `${siteUrl}/package/${packageName.value}`
476477
return requestedVersion.value ? `${base}/v/${requestedVersion.value}` : base
477478
})
478479

config/env.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import Git from 'simple-git'
44
import * as process from 'node:process'
5+
import { NPMX_SITE_PROD } from '../shared/utils/constants'
56

67
export { version } from '../package.json'
78

@@ -87,6 +88,11 @@ export const getProductionUrl = () =>
8788
: undefined
8889
: undefined
8990

91+
export const getSiteUrl = (isDevelopment: boolean) => {
92+
if (isDevelopment) return 'http://localhost:3000'
93+
return getPreviewUrl() || getProductionUrl() || NPMX_SITE_PROD
94+
}
95+
9096
const git = Git()
9197
export async function getGitInfo() {
9298
let branch
@@ -140,12 +146,14 @@ export async function getEnv(isDevelopment: boolean) {
140146
: 'release'
141147
const previewUrl = getPreviewUrl()
142148
const productionUrl = getProductionUrl()
149+
const siteUrl = getSiteUrl(isDevelopment)
143150
return {
144151
commit,
145152
shortCommit,
146153
branch,
147154
env,
148155
previewUrl,
149156
productionUrl,
157+
siteUrl,
150158
} as const
151159
}

0 commit comments

Comments
 (0)