Skip to content

Commit 7c0d006

Browse files
authored
Merge pull request #14 from Tenemo/dev
GitHub data refactor/cleanup + JSON-DL fixes
2 parents 8685ac6 + 30eb263 commit 7c0d006

5 files changed

Lines changed: 39 additions & 35 deletions

File tree

src/routes/contact.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const meta: MetaFunction = () => {
104104
rel: 'canonical',
105105
href: 'https://piech.dev/contact/',
106106
},
107-
{ 'script:ld+json': JSON.stringify(graph) },
107+
{ 'script:ld+json': graph },
108108
];
109109
};
110110

src/routes/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export const meta: MetaFunction = () => {
194194
content: 'Portrait photo of Piotr Piech.',
195195
},
196196
{ tagName: 'link', rel: 'canonical', href: 'https://piech.dev/' },
197-
{ 'script:ld+json': JSON.stringify(graph) },
197+
{ 'script:ld+json': graph },
198198
];
199199
};
200200

src/routes/project-item.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export const meta: MetaFunction = (args) => {
155155
rel: 'canonical',
156156
href: `https://piech.dev/projects/${repo}`,
157157
},
158-
{ 'script:ld+json': JSON.stringify(graph) },
158+
{ 'script:ld+json': graph },
159159
];
160160
};
161161

src/routes/projects.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const meta: MetaFunction = () => {
139139
rel: 'canonical',
140140
href: 'https://piech.dev/projects/',
141141
},
142-
{ 'script:ld+json': JSON.stringify(graph) },
142+
{ 'script:ld+json': graph },
143143
];
144144
};
145145

src/utils/fetchGithubData.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -113,43 +113,47 @@ async function getPackageJsonLicenseFromMaster(
113113
repo: string,
114114
): Promise<string | undefined> {
115115
// Spec: license from package.json on master HEAD commit, if present.
116-
// We intentionally only check the 'master' branch as requested,
117-
// and do not fallback to 'main' here.
118-
try {
119-
const raw = await fetchText(
120-
`https://raw.githubusercontent.com/${owner}/${repo}/master/package.json`,
121-
);
116+
// Fallback: if 'master' isn't available, try 'main'.
117+
const candidateBranches = ['master', 'main'] as const;
118+
119+
for (const branch of candidateBranches) {
122120
try {
123-
const pkg = JSON.parse(raw) as unknown;
124-
if (
125-
pkg &&
126-
typeof pkg === 'object' &&
127-
'license' in (pkg as Record<string, unknown>)
128-
) {
129-
const lic = (pkg as Record<string, unknown>).license;
130-
if (typeof lic === 'string') return lic;
121+
const raw = await fetchText(
122+
`https://raw.githubusercontent.com/${owner}/${repo}/${branch}/package.json`,
123+
);
124+
try {
125+
const pkg = JSON.parse(raw) as unknown;
131126
if (
132-
lic &&
133-
typeof lic === 'object' &&
134-
'type' in (lic as Record<string, unknown>) &&
135-
typeof (lic as Record<string, unknown>).type === 'string'
136-
)
137-
return (lic as Record<string, unknown>).type as string;
138-
}
139-
// Support legacy `licenses` array
140-
if (pkg && typeof pkg === 'object') {
141-
const anyPkg = pkg as Record<string, unknown>;
142-
const licensesRaw = anyPkg.licenses;
143-
if (Array.isArray(licensesRaw) && licensesRaw.length > 0) {
144-
const first = licensesRaw[0] as { type?: unknown };
145-
if (typeof first.type === 'string') return first.type;
127+
pkg &&
128+
typeof pkg === 'object' &&
129+
'license' in (pkg as Record<string, unknown>)
130+
) {
131+
const lic = (pkg as Record<string, unknown>).license;
132+
if (typeof lic === 'string') return lic;
133+
if (
134+
lic &&
135+
typeof lic === 'object' &&
136+
'type' in (lic as Record<string, unknown>)
137+
) {
138+
const typeVal = (lic as Record<string, unknown>).type;
139+
if (typeof typeVal === 'string') return typeVal;
140+
}
141+
}
142+
// Support legacy `licenses` array
143+
if (pkg && typeof pkg === 'object') {
144+
const anyPkg = pkg as Record<string, unknown>;
145+
const licensesRaw = anyPkg.licenses;
146+
if (Array.isArray(licensesRaw) && licensesRaw.length > 0) {
147+
const first = licensesRaw[0] as { type?: unknown };
148+
if (typeof first.type === 'string') return first.type;
149+
}
146150
}
151+
} catch {
152+
// ignore JSON parsing errors and try next branch
147153
}
148154
} catch {
149-
// ignore JSON parsing errors and fall through
155+
// package.json not found or inaccessible on this branch; try next
150156
}
151-
} catch {
152-
// master/package.json not found or inaccessible
153157
}
154158
return undefined;
155159
}

0 commit comments

Comments
 (0)