Cache mcdata registries

This commit is contained in:
Misode
2020-11-06 02:53:32 +01:00
parent c9165df29e
commit 9e49555be3
3 changed files with 43 additions and 4 deletions

View File

@@ -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

View File

@@ -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))
}
}

View File

@@ -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' },