From 97dfc14007f14f8dfe525b1a635cc260ad8ae0fc Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 7 Jan 2021 14:53:56 +0000 Subject: [PATCH] Bundle app ID inside apps, rather than trying to find it dynamically --- .../userInterface/AppPreview/CurrentItemPreview.svelte | 1 + .../components/userInterface/AppPreview/iframeTemplate.html | 3 ++- packages/client/src/api/api.js | 4 +--- packages/client/src/index.js | 1 + packages/client/src/sdk.js | 2 -- packages/client/src/store/auth.js | 6 +++--- packages/client/src/store/builder.js | 1 + packages/client/src/store/screens.js | 5 ++--- packages/server/src/api/controllers/static/index.js | 1 + .../server/src/api/controllers/static/templates/app.hbs | 5 ++++- 10 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte index c5bcb2951a..bb152accc9 100644 --- a/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte +++ b/packages/builder/src/components/userInterface/AppPreview/CurrentItemPreview.svelte @@ -30,6 +30,7 @@ } $: selectedComponentId = $store.selectedComponentId ?? "" $: previewData = { + appId: $store.appId, layout, screen, selectedComponentId, diff --git a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.html b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.html index 2facc9ad70..cec0b789da 100644 --- a/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.html +++ b/packages/builder/src/components/userInterface/AppPreview/iframeTemplate.html @@ -22,10 +22,11 @@ } // Extract data from message - const { selectedComponentId, layout, screen, previewType } = JSON.parse(event.data) + const { selectedComponentId, layout, screen, previewType, appId } = JSON.parse(event.data) // Set some flags so the app knows we're in the builder window["##BUDIBASE_IN_BUILDER##"] = true + window["##BUDIBASE_APP_ID##"] = appId window["##BUDIBASE_PREVIEW_LAYOUT##"] = layout window["##BUDIBASE_PREVIEW_SCREEN##"] = screen window["##BUDIBASE_SELECTED_COMPONENT_ID##"] = selectedComponentId diff --git a/packages/client/src/api/api.js b/packages/client/src/api/api.js index c339bf8ac6..99b085b2fb 100644 --- a/packages/client/src/api/api.js +++ b/packages/client/src/api/api.js @@ -1,5 +1,3 @@ -import { getAppId } from "../utils/getAppId" - /** * API cache for cached request responses. */ @@ -30,7 +28,7 @@ const makeApiCall = async ({ method, url, body, json = true }) => { let headers = { Accept: "application/json", "Content-Type": "application/json", - "x-budibase-app-id": getAppId(), + "x-budibase-app-id": window["##BUDIBASE_APP_ID##"], } if (!window["##BUDIBASE_IN_BUILDER##"]) { headers["x-budibase-type"] = "client" diff --git a/packages/client/src/index.js b/packages/client/src/index.js index 19ac28d6fb..66bbce2770 100644 --- a/packages/client/src/index.js +++ b/packages/client/src/index.js @@ -7,6 +7,7 @@ const loadBudibase = () => { // Update builder store with any builder flags builderStore.set({ inBuilder: !!window["##BUDIBASE_IN_BUILDER##"], + appId: window["##BUDIBASE_APP_ID##"], layout: window["##BUDIBASE_PREVIEW_LAYOUT##"], screen: window["##BUDIBASE_PREVIEW_SCREEN##"], selectedComponentId: window["##BUDIBASE_SELECTED_COMPONENT_ID##"], diff --git a/packages/client/src/sdk.js b/packages/client/src/sdk.js index fefd659986..ba7a3d3a9e 100644 --- a/packages/client/src/sdk.js +++ b/packages/client/src/sdk.js @@ -2,7 +2,6 @@ import * as API from "./api" import { authStore, routeStore, screenStore, bindingStore } from "./store" import { styleable } from "./utils/styleable" import { linkable } from "./utils/linkable" -import { getAppId } from "./utils/getAppId" import DataProvider from "./components/DataProvider.svelte" export default { @@ -12,7 +11,6 @@ export default { screenStore, styleable, linkable, - getAppId, DataProvider, setBindableValue: bindingStore.actions.setBindableValue, } diff --git a/packages/client/src/store/auth.js b/packages/client/src/store/auth.js index a08510ac31..d2ce04b604 100644 --- a/packages/client/src/store/auth.js +++ b/packages/client/src/store/auth.js @@ -1,8 +1,8 @@ import * as API from "../api" -import { getAppId } from "../utils/getAppId" -import { writable } from "svelte/store" +import { writable, get } from "svelte/store" import { initialise } from "./initialise" import { routeStore } from "./routes" +import { builderStore } from "./builder" const createAuthStore = () => { const store = writable("") @@ -25,7 +25,7 @@ const createAuthStore = () => { } const logOut = async () => { store.set("") - const appId = getAppId() + const appId = get(builderStore).appId if (appId) { for (let environment of ["local", "cloud"]) { window.document.cookie = `budibase:${appId}:${environment}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;` diff --git a/packages/client/src/store/builder.js b/packages/client/src/store/builder.js index 7cbe49bde8..c7599e5254 100644 --- a/packages/client/src/store/builder.js +++ b/packages/client/src/store/builder.js @@ -3,6 +3,7 @@ import { writable } from "svelte/store" const createBuilderStore = () => { const initialState = { inBuilder: false, + appId: null, layout: null, screen: null, selectedComponentId: null, diff --git a/packages/client/src/store/screens.js b/packages/client/src/store/screens.js index 54c7dda109..49927db9d7 100644 --- a/packages/client/src/store/screens.js +++ b/packages/client/src/store/screens.js @@ -1,8 +1,7 @@ -import { writable, derived } from "svelte/store" +import { writable, derived, get } from "svelte/store" import { routeStore } from "./routes" import { builderStore } from "./builder" import * as API from "../api" -import { getAppId } from "../utils/getAppId" const createScreenStore = () => { const config = writable({ @@ -40,7 +39,7 @@ const createScreenStore = () => { ) const fetchScreens = async () => { - const appDefinition = await API.fetchAppDefinition(getAppId()) + const appDefinition = await API.fetchAppDefinition(get(builderStore).appId) config.set({ screens: appDefinition.screens, layouts: appDefinition.layouts, diff --git a/packages/server/src/api/controllers/static/index.js b/packages/server/src/api/controllers/static/index.js index 5c5294a7c7..a13ab7ac41 100644 --- a/packages/server/src/api/controllers/static/index.js +++ b/packages/server/src/api/controllers/static/index.js @@ -168,6 +168,7 @@ exports.serveApp = async function(ctx) { head, body: html, style: css.code, + appId: ctx.params.appId, }) } diff --git a/packages/server/src/api/controllers/static/templates/app.hbs b/packages/server/src/api/controllers/static/templates/app.hbs index cce07180ae..60497a05df 100644 --- a/packages/server/src/api/controllers/static/templates/app.hbs +++ b/packages/server/src/api/controllers/static/templates/app.hbs @@ -5,6 +5,9 @@ {{{head}}} - {{{body}}} + + {{{body}}} \ No newline at end of file