From 9e49555be33d196d6a660258b5fcc92300b29759 Mon Sep 17 00:00:00 2001 From: Misode Date: Fri, 6 Nov 2020 02:53:32 +0100 Subject: [PATCH] Cache mcdata registries --- .github/workflows/deploy.yml | 10 +++++++++- src/app/RegistryFetcher.ts | 33 ++++++++++++++++++++++++++++++--- webpack.config.js | 4 ++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 2e5732d5..c749040d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -12,10 +12,18 @@ jobs: - name: Checkout uses: actions/checkout@v1 + - name: Get latest mcdata commit hash + uses: octokit/request-action@v2.x + id: get_mcdata_hash + with: + route: GET /repos/Arcensoth/mcdata/branches/master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Build run: | npm install - npm run build + "npm run build -- --env.hash=${{ steps.get_mcdata_hash.outputs.data.commit.sha }}" - name: Deploy uses: JamesIves/github-pages-deploy-action@releases/v3 diff --git a/src/app/RegistryFetcher.ts b/src/app/RegistryFetcher.ts index 0876b861..a9ffd606 100644 --- a/src/app/RegistryFetcher.ts +++ b/src/app/RegistryFetcher.ts @@ -1,6 +1,9 @@ import { CollectionRegistry } from '@mcschema/core' import config from '../config.json' +const localStorageCache = (version: string) => `cache_${version}` +declare var __MCDATA_MASTER_HASH__: string; + const baseUrl = 'https://raw.githubusercontent.com/Arcensoth/mcdata' export const mcdata = (ref: string, registry: string) => { return `${baseUrl}/${ref}/processed/reports/registries/${registry}/data.min.json` @@ -9,18 +12,42 @@ export const mcdata = (ref: string, registry: string) => { export const RegistryFetcher = async (target: CollectionRegistry, versionId: string) => { const version = config.versions.find(v => v.id === versionId) if (!version) return + + const cache = JSON.parse(localStorage.getItem(localStorageCache(versionId)) ?? '{}') + const cacheValid = version.mcdata_ref !== 'master' || cache.mcdata_hash === __MCDATA_MASTER_HASH__ + let cacheDirty = false + await Promise.all(config.registries.map(async r => { const id = typeof r === 'string' ? r : r.id + + if (!cache.registries) { + cache.registries = {} + } + if (cacheValid && cache.registries?.[id]) { + target.register(id, cache.registries[id]) + return + } + const url = typeof r === 'string' - ? mcdata(version.mcdata_ref, r) - : `${baseUrl}/${version.mcdata_ref}/${r.path}` + ? mcdata(version.mcdata_ref, r) + : `${baseUrl}/${version.mcdata_ref}/${r.path}` + try { const res = await fetch(url) const data = await res.json() - console.log(r, url, data) + target.register(id, data.values) + cache.registries[id] = data.values + cacheDirty = true } catch (e) { console.error(`Error occurred while fetching registry "${id}":`, e) } })) + + if (cacheDirty) { + if (version.mcdata_ref === 'master') { + cache.mcdata_hash = __MCDATA_MASTER_HASH__ + } + localStorage.setItem(localStorageCache(versionId), JSON.stringify(cache)) + } } diff --git a/webpack.config.js b/webpack.config.js index a552c875..f69dafdb 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,7 @@ const CopyWebpackPlugin = require('copy-webpack-plugin') const HtmlWebpackPlugin = require('html-webpack-plugin') const MergeJsonWebpackPlugin = require("merge-jsons-webpack-plugin"); +const webpack = require('webpack'); const config = require('./src/config.json') module.exports = (env, argv) => ({ @@ -18,6 +19,9 @@ module.exports = (env, argv) => ({ ] }, plugins: [ + new webpack.DefinePlugin({ + __MCDATA_MASTER_HASH__: JSON.stringify(env ? env.hash : '') + }), new CopyWebpackPlugin({ patterns: [ { from: 'src/styles', to: 'styles' },