This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove homepage projects and tags build-time caching
- Loading branch information
Showing
6 changed files
with
140 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { UseFetchOptions } from '#app' | ||
|
||
export async function useHomepageProjects(rowsCount: number) { | ||
const runtimeConfig = useRuntimeConfig() | ||
|
||
const fetchOptions: UseFetchOptions<any[], any[][]> = { | ||
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 ([email protected])', | ||
'X-Ratelimit-Key': runtimeConfig.rateLimitKey, | ||
} | ||
} | ||
|
||
return await useFetch('/projects_random?count=40', fetchOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
||
|
@@ -115,74 +114,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 ([email protected])', | ||
}, | ||
} | ||
|
||
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'), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { NitroFetchOptions } from 'nitropack' | ||
|
||
export default defineNuxtPlugin(async () => { | ||
const runtimeConfig = useRuntimeConfig() | ||
|
||
const requestOptions: NitroFetchOptions<any, 'get'> = { | ||
baseURL: runtimeConfig.public.apiBaseUrl, | ||
} | ||
|
||
if (import.meta.server) { | ||
requestOptions.headers = { | ||
'User-Agent': 'Knossos Server ([email protected])', | ||
'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, | ||
}, | ||
} | ||
}) |