From 8fda70a0c162a427eea225613e60fa5edc56d3bc Mon Sep 17 00:00:00 2001 From: Sasha Sorokin <10401817+Brawaru@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:43:12 +0200 Subject: [PATCH 1/2] Remove homepage projects and tags build-time caching --- composables/homepage.ts | 27 ++++++++++++ composables/tag.js | 64 +--------------------------- nuxt.config.ts | 69 ------------------------------ pages/app.vue | 16 ++----- pages/index.vue | 34 +++++++-------- plugins/tags.ts | 93 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 163 deletions(-) create mode 100644 composables/homepage.ts create mode 100644 plugins/tags.ts diff --git a/composables/homepage.ts b/composables/homepage.ts new file mode 100644 index 0000000000..aff47490b1 --- /dev/null +++ b/composables/homepage.ts @@ -0,0 +1,27 @@ +import type { UseFetchOptions } from '#app' + +export async function useHomepageProjects(rowsCount: number) { + const runtimeConfig = useRuntimeConfig() + + const fetchOptions: UseFetchOptions = { + baseURL: runtimeConfig.public.apiBaseUrl, + key: `homepageProjects/${rowsCount}`, + transform(homepageProjects) { + const val = Math.ceil(homepageProjects.length / rowsCount) + const result: any[][] = [] + for (let i = 0; i < rowsCount; i++) { + result.push(homepageProjects.slice(val * i, val * (i + 1))) + } + return result + }, + } + + if (import.meta.server) { + fetchOptions.headers = { + 'User-Agent': 'Knossos Server (support@modrinth.com)', + 'X-Ratelimit-Key': runtimeConfig.rateLimitKey, + } + } + + return await useFetch('/projects_random?count=40', fetchOptions) +} diff --git a/composables/tag.js b/composables/tag.js index 91561a2374..2dfaa7e7a1 100644 --- a/composables/tag.js +++ b/composables/tag.js @@ -1,63 +1 @@ -import tags from '~/generated/state.json' - -export const useTags = () => - useState('tags', () => ({ - categories: tags.categories, - loaders: tags.loaders, - gameVersions: tags.gameVersions, - donationPlatforms: tags.donationPlatforms, - reportTypes: tags.reportTypes, - projectTypes: [ - { - actual: 'mod', - id: 'mod', - display: 'mod', - }, - { - actual: 'mod', - id: 'plugin', - display: 'plugin', - }, - { - actual: 'mod', - id: 'datapack', - display: 'data pack', - }, - { - actual: 'shader', - id: 'shader', - display: 'shader', - }, - { - actual: 'resourcepack', - id: 'resourcepack', - display: 'resource pack', - }, - { - actual: 'modpack', - id: 'modpack', - display: 'modpack', - }, - ], - loaderData: { - pluginLoaders: ['bukkit', 'spigot', 'paper', 'purpur', 'sponge', 'folia'], - pluginPlatformLoaders: ['bungeecord', 'waterfall', 'velocity'], - allPluginLoaders: [ - 'bukkit', - 'spigot', - 'paper', - 'purpur', - 'sponge', - 'bungeecord', - 'waterfall', - 'velocity', - 'folia', - ], - dataPackLoaders: ['datapack'], - modLoaders: ['forge', 'fabric', 'quilt', 'liteloader', 'modloader', 'rift', 'neoforge'], - }, - projectViewModes: ['list', 'grid', 'gallery'], - approvedStatuses: ['approved', 'archived', 'unlisted', 'private'], - rejectedStatuses: ['rejected', 'withheld'], - staffRoles: ['moderator', 'admin'], - })) +export const useTags = () => useNuxtApp().$tags diff --git a/nuxt.config.ts b/nuxt.config.ts index d1162404e3..0447808f97 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -3,7 +3,6 @@ import { pathToFileURL } from 'node:url' import svgLoader from 'vite-svg-loader' import { resolve, basename } from 'pathe' import { defineNuxtConfig } from 'nuxt/config' -import { $fetch } from 'ofetch' import { globIterate } from 'glob' import { match as matchLocale } from '@formatjs/intl-localematcher' @@ -93,74 +92,6 @@ export default defineNuxtConfig({ ], }, hooks: { - async 'build:before'() { - // 30 minutes - const TTL = 30 * 60 * 1000 - - let state: { - lastGenerated?: string - apiUrl?: string - categories?: any[] - loaders?: any[] - gameVersions?: any[] - donationPlatforms?: any[] - reportTypes?: any[] - } = {} - let homePageProjects: any[] = [] - - try { - state = JSON.parse(await fs.readFile('./generated/state.json', 'utf8')) - homePageProjects = JSON.parse(await fs.readFile('./generated/homepage.json', 'utf8')) - } catch { - // File doesn't exist, create folder - await fs.mkdir('./generated', { recursive: true }) - } - - const API_URL = getApiUrl() - - if ( - // Skip regeneration if within TTL... - state.lastGenerated && - new Date(state.lastGenerated).getTime() + TTL > new Date().getTime() && - // ...but only if the API URL is the same - state.apiUrl === API_URL && - homePageProjects.length !== 0 - ) { - return - } - - state.lastGenerated = new Date().toISOString() - - state.apiUrl = API_URL - - const headers = { - headers: { - 'user-agent': 'Knossos generator (support@modrinth.com)', - }, - } - - const [categories, loaders, gameVersions, donationPlatforms, reportTypes, projects] = - await Promise.all([ - $fetch(`${API_URL}tag/category`, headers), - $fetch(`${API_URL}tag/loader`, headers), - $fetch(`${API_URL}tag/game_version`, headers), - $fetch(`${API_URL}tag/donation_platform`, headers), - $fetch(`${API_URL}tag/report_type`, headers), - $fetch(`${API_URL}projects_random?count=40`, headers), - ]) - - state.categories = categories - state.loaders = loaders - state.gameVersions = gameVersions - state.donationPlatforms = donationPlatforms - state.reportTypes = reportTypes - homePageProjects = projects - - await fs.writeFile('./generated/state.json', JSON.stringify(state)) - await fs.writeFile('./generated/homepage.json', JSON.stringify(homePageProjects)) - - console.log('Tags generated!') - }, 'pages:extend'(routes) { routes.splice( routes.findIndex((x) => x.name === 'search-searchProjectType'), diff --git a/pages/app.vue b/pages/app.vue index 144c1fb754..3b3332cc55 100644 --- a/pages/app.vue +++ b/pages/app.vue @@ -1,7 +1,7 @@ diff --git a/plugins/tags.ts b/plugins/tags.ts new file mode 100644 index 0000000000..0d58443b6c --- /dev/null +++ b/plugins/tags.ts @@ -0,0 +1,93 @@ +import type { NitroFetchOptions } from 'nitropack' + +export default defineNuxtPlugin(async () => { + const runtimeConfig = useRuntimeConfig() + + const requestOptions: NitroFetchOptions = { + baseURL: runtimeConfig.public.apiBaseUrl, + } + + if (import.meta.server) { + requestOptions.headers = { + 'User-Agent': 'Knossos Server (support@modrinth.com)', + 'X-Ratelimit-Key': runtimeConfig.rateLimitKey, + } + } + + const { data: tags } = await useAsyncData('tags', async () => { + const [categories, loaders, gameVersions, donationPlatforms, reportTypes] = await Promise.all([ + $fetch(`tag/category`, requestOptions), + $fetch(`tag/loader`, requestOptions), + $fetch(`tag/game_version`, requestOptions), + $fetch(`tag/donation_platform`, requestOptions), + $fetch(`tag/report_type`, requestOptions), + ]) + + return { + categories, + loaders, + gameVersions, + donationPlatforms, + reportTypes, + projectTypes: [ + { + actual: 'mod', + id: 'mod', + display: 'mod', + }, + { + actual: 'mod', + id: 'plugin', + display: 'plugin', + }, + { + actual: 'mod', + id: 'datapack', + display: 'data pack', + }, + { + actual: 'shader', + id: 'shader', + display: 'shader', + }, + { + actual: 'resourcepack', + id: 'resourcepack', + display: 'resource pack', + }, + { + actual: 'modpack', + id: 'modpack', + display: 'modpack', + }, + ], + loaderData: { + pluginLoaders: ['bukkit', 'spigot', 'paper', 'purpur', 'sponge', 'folia'], + pluginPlatformLoaders: ['bungeecord', 'waterfall', 'velocity'], + allPluginLoaders: [ + 'bukkit', + 'spigot', + 'paper', + 'purpur', + 'sponge', + 'bungeecord', + 'waterfall', + 'velocity', + 'folia', + ], + dataPackLoaders: ['datapack'], + modLoaders: ['forge', 'fabric', 'quilt', 'liteloader', 'modloader', 'rift', 'neoforge'], + }, + projectViewModes: ['list', 'grid', 'gallery'], + approvedStatuses: ['approved', 'archived', 'unlisted', 'private'], + rejectedStatuses: ['rejected', 'withheld'], + staffRoles: ['moderator', 'admin'], + } + }) + + return { + provide: { + tags, + }, + } +}) From 4748502c18d74344e81911ce7db9b26e7b68cbde Mon Sep 17 00:00:00 2001 From: Sasha Sorokin <10401817+Brawaru@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:56:00 +0200 Subject: [PATCH 2/2] Remove regenerate workflow --- .github/workflows/regenerate.yml | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 .github/workflows/regenerate.yml diff --git a/.github/workflows/regenerate.yml b/.github/workflows/regenerate.yml deleted file mode 100644 index a6fbe7f61e..0000000000 --- a/.github/workflows/regenerate.yml +++ /dev/null @@ -1,15 +0,0 @@ -name: Regenerate tags - -on: - schedule: - - cron: '*/30 * * * *' - -jobs: - regenerate: - runs-on: ubuntu-latest - if: github.repository_owner == 'modrinth' - steps: - - name: Re-deploy site - run: | - curl -X POST \ - ${{ secrets.CLOUDFLARE_DEPLOY_URL }}