Fix localStore set to a function that fetches pre-executed backend calls

This commit is contained in:
Alice Gaudon 2021-05-11 15:13:32 +02:00
parent 911e64a6ae
commit 076cda8008
2 changed files with 15 additions and 11 deletions

View File

@ -6,20 +6,19 @@
<style>%css%</style> <style>%css%</style>
<script type="module" defer> <script type="module" defer>
import View from '/js/views/%canonicalViewName%.js'; import View from '/js/views/%canonicalViewName%.js';
import {locals} from '/js/stores.js'; import * as stores from '/js/stores.js';
const localStore = stores[Object.keys(stores)[0]].locals;
locals.set(%locals%); const locals = %locals%;
localStore.set((key, args) => {
return locals[args ?
`'${key}', \`${args}\``
: `'${key}'`];
});
new View({ new View({
hydrate: true, hydrate: true,
target: document.body, target: document.body,
props: {
locals: (key, args) => {
return locals[args ?
`'${key}', \`${args}\``
: `'${key}'`];
},
},
}); });
</script> </script>
</head> </head>

View File

@ -221,7 +221,7 @@ export default class SvelteViewEngine extends ViewEngine {
logger.info(canonicalName + ' > ', 'Replacing backend calls'); logger.info(canonicalName + ' > ', 'Replacing backend calls');
// Skip replace if there is no swaf export // Skip replace if there is no swaf export
if (!code.match(/export[ \n]+let[ \n]+locals[ \n]*=[ \n]*{[ \n]*}/)) { if (!code.match(/import[ \n]+\{[ \n]*locals[ \n]*\}[ \n]+from[ \n]+["'](\.\.\/)+ts\/stores\.js["']/)) {
return { return {
backendCalls: [], backendCalls: [],
code: code, code: code,
@ -296,7 +296,12 @@ export default class SvelteViewEngine extends ViewEngine {
// Load locals into locals store // Load locals into locals store
const localsModulePath = "../../build/ts/stores.js"; const localsModulePath = "../../build/ts/stores.js";
const localsModule = await import(localsModulePath); const localsModule = await import(localsModulePath);
localsModule.locals.set(ViewEngine.getGlobals()); const locals = ViewEngine.getGlobals();
localsModule.locals.set((key: string, args: string) => {
return locals[args ?
`'${key}', \`${args}\``
: `'${key}'`];
});
// Load module and render // Load module and render
return requireFromString(svelteSsr.js.code, file + nanoid() + '.js').default.render(); return requireFromString(svelteSsr.js.code, file + nanoid() + '.js').default.render();