diff --git a/lxl-web/.env.example b/lxl-web/.env.example index 43e0632fb..0455451c5 100644 --- a/lxl-web/.env.example +++ b/lxl-web/.env.example @@ -1,6 +1,7 @@ SVELTE_INSPECTOR_TOGGLE=shift-alt API_URL= ID_URL= +USE_LOCAL_DISPLAY_JSONLD=false USE_LOCALHOST_API=false LOCALHOST_API_PORT= -LOCALHOST_ID_PORT= \ No newline at end of file +LOCALHOST_ID_PORT= diff --git a/lxl-web/package-lock.json b/lxl-web/package-lock.json index de54f6d08..b45ce1b14 100644 --- a/lxl-web/package-lock.json +++ b/lxl-web/package-lock.json @@ -40,6 +40,7 @@ "dev": true, "license": "Apache-2.0", "dependencies": { + "@types/node": "18.18.2", "eslint": "^7.32.0", "eslint-config-airbnb-base": "^14.2.1", "eslint-plugin-import": "^2.25.2", diff --git a/lxl-web/src/hooks.server.ts b/lxl-web/src/hooks.server.ts index 19cc951c6..e48bd5a42 100644 --- a/lxl-web/src/hooks.server.ts +++ b/lxl-web/src/hooks.server.ts @@ -1,8 +1,18 @@ import type { Handle, HandleFetch } from '@sveltejs/kit'; import { defaultLocale, Locales } from '$lib/i18n/locales'; import { env } from '$env/dynamic/private'; +import { USE_LOCAL_DISPLAY_JSONLD } from '$env/static/private'; +import { env } from '$env/dynamic/private'; +import { DisplayUtil, VocabUtil } from '$lib/utils/xl'; +import fs from 'fs'; + +let utilCache; export const handle: Handle = async ({ event, resolve }) => { + const [vocabUtil, displayUtil] = await loadUtilCached(); + event.locals.vocab = vocabUtil; + event.locals.display = displayUtil; + // set HTML lang // https://github.com/sveltejs/kit/issues/3091#issuecomment-1112589090 const path = event.url.pathname; @@ -39,3 +49,35 @@ export const handleFetch: HandleFetch = async ({ request, fetch }) => { return fetch(request); }; + +async function loadUtilCached() { + if (!utilCache) { + utilCache = loadUtil(); + } + return utilCache; +} + +// TODO move +// TODO error handling +async function loadUtil() { + const [contextRes, vocabRes, displayRes] = await Promise.all([ + fetch(`${env.ID_URL}/context.jsonld`), + fetch(`${env.ID_URL}/vocab/data.jsonld`), + fetch(`${env.ID_URL}/vocab/display/data.jsonld`) + ]); + + const context = await contextRes.json(); + const vocab = await vocabRes.json(); + let display = await displayRes.json(); + + if (USE_LOCAL_DISPLAY_JSONLD === 'true') { + const path = '../../definitions/source/vocab/display.jsonld'; + const displayJson = fs.readFileSync(path, { encoding: 'utf8' }); + display = JSON.parse(displayJson); + console.warn(`USE_LOCAL_DISPLAY_JSONLD true. Using ${path}`); + } + + const vocabUtil = new VocabUtil(vocab, context); + const displayUtil = new DisplayUtil(display, vocabUtil); + return [vocabUtil, displayUtil]; +} diff --git a/lxl-web/src/lib/components/Search.svelte b/lxl-web/src/lib/components/Search.svelte index edcb42e52..fab5fd881 100644 --- a/lxl-web/src/lib/components/Search.svelte +++ b/lxl-web/src/lib/components/Search.svelte @@ -26,7 +26,7 @@ } -