-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
79 additions
and
50 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
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,54 @@ | ||
import fetch from "node-fetch"; | ||
import {CONTEXT, DISPLAY, translateAliasedUri, VOCAB} from "../plugins/env"; | ||
import * as VocabUtil from "lxljs/vocab"; | ||
import * as DisplayUtil from "lxljs/display"; | ||
|
||
const toJson = response => { | ||
if (response.ok) { | ||
return response.json() | ||
} | ||
else { | ||
throw { statusCode: response.status } | ||
} | ||
} | ||
|
||
async function fetchVocab() { | ||
const vocabCache = {}; | ||
|
||
await Promise.all([ | ||
fetch(translateAliasedUri(CONTEXT)).then(toJson), | ||
fetch(translateAliasedUri(VOCAB)).then(toJson), | ||
fetch(translateAliasedUri(DISPLAY)).then(toJson) | ||
]) | ||
.then(v => { | ||
let context, vocab, display; | ||
[context, vocab, display] = v; | ||
|
||
vocabCache.context = VocabUtil.preprocessContext(context)['@context']; | ||
vocabCache.vocab = vocab; | ||
vocabCache.display = DisplayUtil.expandInherited(display); | ||
}) | ||
.catch(err => { | ||
vocabCache.error = { statusCode: err.statusCode || 500, 'message': err.message || ''} | ||
}); | ||
|
||
return vocabCache; | ||
} | ||
|
||
export default function (_moduleOptions) { | ||
// shared by all requests | ||
let cache; | ||
|
||
async function getVocab() { | ||
if (!cache || cache.error) { | ||
cache = await fetchVocab(); | ||
} | ||
|
||
return cache; | ||
} | ||
|
||
// called before server-side rendering | ||
this.nuxt.hook("vue-renderer:ssr:prepareContext", async (ssrContext) => { | ||
ssrContext.$vocab = await getVocab(); | ||
}); | ||
} |
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