Skip to content

Commit 3cba960

Browse files
authored
Merge pull request #162 from increments/fix-assets-api
Fix issue that appearance of preview differs between Qiita and Qiita CLI
2 parents 7856794 + 3e8f8d0 commit 3cba960

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/qiita-api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,8 @@ export class QiitaApi {
269269
body: data,
270270
});
271271
}
272+
273+
async getAssetUrls() {
274+
return await this.get<{ [key: string]: string }>("/api/qiita-cli/assets");
275+
}
272276
}

src/server/api/assets.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,50 @@
11
import type Express from "express";
22
import { Router } from "express";
3+
import { getQiitaApiInstance } from "../../lib/get-qiita-api-instance";
34

45
const redirectToArticleCss = async (
56
req: Express.Request,
67
res: Express.Response,
78
) => {
8-
const url =
9-
process.env.QIITA_ASSETS_ARTICLE_CSS ||
10-
(await resolveAssetsUrl("public/article.css"));
9+
const url = await resolveAssetsUrl("article_css_url");
1110
res.redirect(url);
1211
};
1312

1413
const redirectToEmbedInitJs = async (
1514
req: Express.Request,
1615
res: Express.Response,
1716
) => {
18-
const url =
19-
process.env.QIITA_ASSETS_EMBED_INIT_JS ||
20-
(await resolveAssetsUrl("public/v3-embed-init.js"));
17+
const url = await resolveAssetsUrl("v3_embed_init_js_url");
2118
res.redirect(url);
2219
};
2320

2421
const redirectToFavicon = async (
2522
req: Express.Request,
2623
res: Express.Response,
2724
) => {
28-
const url =
29-
process.env.QIITA_ASSETS_FAVICON ||
30-
(await resolveAssetsUrl("favicons/public/production.ico"));
25+
const url = await resolveAssetsUrl("favicon_url");
3126
res.redirect(url);
3227
};
3328

34-
const resolveAssetsUrl = async (key: string) => {
35-
const latest_manifest_url =
36-
"https://qiita.com/assets/.latest_client_manifest_name_production";
29+
const resolveAssetsUrl = (() => {
30+
let cachedAssetUrls: { [key: string]: string } | null = null;
3731

38-
const cdnAssetsUrl = "https://cdn.qiita.com/assets";
32+
return async (key: string) => {
33+
if (cachedAssetUrls === null) {
34+
const qiitaApi = await getQiitaApiInstance();
35+
const assetUrls = await qiitaApi.getAssetUrls();
3936

40-
const clientManifestFileName = await (
41-
await fetch(latest_manifest_url)
42-
).text();
43-
const json = await (
44-
await fetch(`${cdnAssetsUrl}/${clientManifestFileName}`)
45-
).json();
37+
cachedAssetUrls = assetUrls;
38+
}
4639

47-
const filename = json[key];
48-
if (filename === undefined) {
49-
throw new Error(`Asset not found: ${key}`);
50-
}
40+
const url = cachedAssetUrls[key];
41+
if (!url) {
42+
throw new Error(`Asset not found: ${key}`);
43+
}
5144

52-
return `${cdnAssetsUrl}/${filename}`;
53-
};
45+
return url;
46+
};
47+
})();
5448

5549
export const AssetsRouter = Router()
5650
.get("/article.css", redirectToArticleCss)

0 commit comments

Comments
 (0)