Skip to content

Commit

Permalink
fix : vercel edge config
Browse files Browse the repository at this point in the history
- edge configのスキーマを修正
- アイテムを強制更新できるように変更
  • Loading branch information
Liry24 committed Feb 21, 2025
1 parent c98cf9a commit dc93062
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion components/badge/header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const badge = ref<HeaderBadge | null>(null);
try {
const res = await $fetch('/api/edgeConfig/badge_main');
const res = await $fetch('/api/edgeConfig/badgeMain');
badge.value = res.value ? (res.value as unknown as HeaderBadge) : null;
} catch (error) {
console.error('Failed to fetch badge:', error);
Expand Down
28 changes: 19 additions & 9 deletions components/setups/item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const props = withDefaults(defineProps<Props>(), {
noAction: false,
});
const forceUpdateItem = (await $fetch('/api/edgeConfig/forceUpdateItem')).value;
const loading = ref(false);
const sourceInfo = {
Expand All @@ -35,34 +37,42 @@ onMounted(async () => {
new Date().getTime() - new Date(props.item.updated_at).getTime();
// 時間の差分が1日を超えている場合、処理継続する
if (timeDifference > 24 * 60 * 60 * 1000) {
// edge config の forceUpdateItem が true の場合は強制的に処理継続する
if (forceUpdateItem || timeDifference > 24 * 60 * 60 * 1000) {
loading.value = true;
const response = await $fetch('/api/item/booth', {
query: { id: props.item.id },
});
if (response.data) item.value = response.data;
if (!response.data) item.value.outdated = true;
}
loading.value = false;
loading.value = false;
}
});
</script>

<template>
<div
v-if="loading"
:data-size="props.size"
:class="
twMerge(
'flex items-center px-6 ring-1 ring-zinc-300 dark:ring-zinc-600 rounded-lg overflow-clip',
'group flex items-center px-6 ring-1 ring-zinc-300 dark:ring-zinc-600 rounded-lg overflow-clip',
props.class
)
"
>
<Icon
name="svg-spinners:ring-resize"
size="24"
:class="props.size === 'lg' ? 'h-20 sm:h-32' : 'h-20'"
/>
<div
:data-size="props.size"
class="flex items-center gap-3 h-24 data-[size=lg]:h-24 data-[size=lg]:sm:h-36"
>
<Icon name="svg-spinners:ring-resize" size="24" />
<p class="text-sm font-bold text-zinc-600 dark:text-zinc-400">
アイテムの最新情報を取得中...
</p>
</div>
</div>

<div
Expand Down
2 changes: 1 addition & 1 deletion components/ui/isMaintenance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const isMaintenance = ref<boolean>(false);
onMounted(async () => {
const res = await $fetch('/api/edgeConfig');
if (res.value) isMaintenance.value = res.value.is_maintenance as boolean;
if (res.value) isMaintenance.value = res.value.isMaintenance as boolean;
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion middleware/maintenance.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineNuxtRouteMiddleware(async (_to, _from) => {
if (env === 'development') return;

const isMaintenance = await $fetch('/api/edgeConfig');
if (isMaintenance.value?.is_maintenance)
if (isMaintenance.value?.isMaintenance)
return showError({
statusCode: 503,
message: 'メンテナンス中です',
Expand Down
54 changes: 29 additions & 25 deletions server/api/item/booth.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ const GetBoothItem = async (
event: H3Event,
id: number
): Promise<ApiResponse<Item>> => {
const edgeConfig = await event.$fetch('/api/edgeConfig');
if (!edgeConfig.value)
return {
data: null,
error: {
status: 500,
message: 'Error in vercel edge config.',
},
};

const config = edgeConfig.value;

const client = await serverSupabaseClient(event);

const startTime = Date.now(); // 処理開始時刻を記録
Expand Down Expand Up @@ -47,18 +59,21 @@ const GetBoothItem = async (
.maybeSingle();

// データが存在し、かつ最終更新が1日以内ならそのまま返す
if (itemData) {
const timeDifference =
new Date().getTime() - new Date(itemData.updated_at).getTime();
// edge config の forceUpdateItem が true の場合は強制的に更新
if (!config.forceUpdateItem) {
if (itemData) {
const timeDifference =
new Date().getTime() - new Date(itemData.updated_at).getTime();

if (timeDifference < 24 * 60 * 60 * 1000)
return {
data: itemData,
error: null,
};
if (timeDifference < 24 * 60 * 60 * 1000)
return {
data: itemData,
error: null,
};

console.log('Data is old, fetching from Booth', id);
}
console.log('Data is old, fetching from Booth', id);
}
} else console.log('Force update is enabled, fetching from Booth', id);

const locale = 'ja';
const urlBase = `https://booth.pm/${locale}/items/`;
Expand Down Expand Up @@ -147,21 +162,10 @@ const GetBoothItem = async (
// 134 - 素材(その他)

try {
const config = await event.$fetch(
'/api/edgeConfig/allowed_category_id'
);
if (config.status !== 200 || !config.value)
return {
data: null,
error: {
status: 500,
message: 'Error in vercel edge config.',
},
};

const allowed_category_id: number[] = config.value as number[];

if (!allowed_category_id.includes(Number(response.category.id)))
const allowedBoothCategoryId =
config.allowedBoothCategoryId as number[];

if (!allowedBoothCategoryId.includes(Number(response.category.id)))
if (!item.tags.map((t: string) => t).includes('VRChat'))
return {
data: null,
Expand Down

0 comments on commit dc93062

Please sign in to comment.