-
Notifications
You must be signed in to change notification settings - Fork 11
feat(): support app preview #4808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| resetReloadForError, | ||
| __secret_internals, | ||
| isNetworkError, | ||
| getHistory, | ||
| } from "@next-core/runtime"; | ||
| import { HttpRequestConfig, http } from "@next-core/http"; | ||
| import { i18n, initializeI18n } from "@next-core/i18n"; | ||
|
|
@@ -20,6 +21,7 @@ import { | |
| analytics, | ||
| } from "@next-core/easyops-runtime"; | ||
| import "@next-core/theme"; | ||
| import type { BootstrapData, Storyboard } from "@next-core/types"; | ||
| import "./XMLHttpRequest.js"; | ||
| import { loadCheckLogin } from "./loadCheckLogin.js"; | ||
| import { fulfilStoryboard, loadBootstrapData } from "./loadBootstrapData.js"; | ||
|
|
@@ -30,6 +32,10 @@ import { getMock } from "./mocks.js"; | |
| import { NS, K, locales } from "./i18n.js"; | ||
| import { DefaultError } from "./DefaultError.js"; | ||
|
|
||
| const isAppPreview = !!new URLSearchParams(window.location.search).get( | ||
| "_experimental_app_preview_" | ||
| ); | ||
|
|
||
| customElements.define("easyops-default-error", DefaultError); | ||
|
|
||
| analytics.initialize( | ||
|
|
@@ -115,61 +121,126 @@ const requestEnd = (): void => { | |
| window.addEventListener("request.start", requestStart); | ||
| window.addEventListener("request.end", requestEnd); | ||
|
|
||
| const runtime = createRuntime({ | ||
| hooks: { | ||
| auth, | ||
| fulfilStoryboard, | ||
| checkPermissions, | ||
| flowApi, | ||
| checkInstalledApps, | ||
| menu, | ||
| images: { imagesFactory, widgetImagesFactory }, | ||
| messageDispatcher, | ||
| pageView: analytics.pageView, | ||
| }, | ||
| }); | ||
| function doCreateRuntime() { | ||
| return createRuntime({ | ||
| hooks: { | ||
| auth, | ||
| checkPermissions, | ||
| flowApi, | ||
| checkInstalledApps, | ||
| menu, | ||
| images: { imagesFactory, widgetImagesFactory }, | ||
| messageDispatcher, | ||
| ...(isAppPreview | ||
| ? null | ||
| : { | ||
| fulfilStoryboard, | ||
| pageView: analytics.pageView, | ||
| }), | ||
| }, | ||
| }); | ||
|
Comment on lines
+124
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修复 当 - ...(isAppPreview
- ? null
- : {
- fulfilStoryboard,
- pageView: analytics.pageView,
- }),
+ ...(isAppPreview
+ ? {}
+ : {
+ fulfilStoryboard,
+ pageView: analytics.pageView,
+ }),🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| initializeI18n(NS, locales); | ||
|
|
||
| interface PreviewWindow extends Window { | ||
| _preview_only_appPreviewer?: AppPreviewer; | ||
| } | ||
|
|
||
| class AppPreviewer { | ||
| #setupCalled = false; | ||
|
|
||
| async setup(data: BootstrapData, url: string) { | ||
| try { | ||
| if (this.#setupCalled) { | ||
| throw new Error( | ||
| "_preview_only_setupAppPreview can only be called once" | ||
| ); | ||
| } | ||
| this.#setupCalled = true; | ||
| history.replaceState(null, "", url); | ||
| const runtime = doCreateRuntime(); | ||
| await loadCheckLogin(); | ||
| await runtime.bootstrap(data); | ||
| } catch (error) { | ||
| handleError(error); | ||
| } | ||
| requestEnd(); | ||
| } | ||
|
|
||
| async update(appId: string, storyboard: Partial<Storyboard>) { | ||
| __secret_internals.updateStoryboard(appId, storyboard); | ||
| } | ||
|
|
||
| reload() { | ||
| getHistory().reload(); | ||
| } | ||
|
|
||
| push(url: string) { | ||
| getHistory().push(url); | ||
| } | ||
|
|
||
| replace(url: string) { | ||
| getHistory().replace(url); | ||
| } | ||
|
|
||
| goBack() { | ||
| getHistory().goBack(); | ||
| } | ||
|
|
||
| goForward() { | ||
| getHistory().goForward(); | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| try { | ||
| const [, bootstrapData] = await Promise.all([ | ||
| loadCheckLogin(), | ||
| loadBootstrapData(), | ||
| ]); | ||
| await runtime.bootstrap(bootstrapData); | ||
| resetReloadForError(); | ||
| if (isAppPreview) { | ||
| requestStart(); | ||
| (window as PreviewWindow)._preview_only_appPreviewer = new AppPreviewer(); | ||
| return "ok"; | ||
| } else { | ||
| const runtime = doCreateRuntime(); | ||
| const [, bootstrapData] = await Promise.all([ | ||
| loadCheckLogin(), | ||
| loadBootstrapData(), | ||
| ]); | ||
| await runtime.bootstrap(bootstrapData); | ||
| resetReloadForError(); | ||
| } | ||
| return "ok"; | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error("bootstrap failed:", error); | ||
|
|
||
| if (shouldReloadForError(error)) { | ||
| location.reload(); | ||
| return "failed"; | ||
| } | ||
| handleError(error); | ||
| return "failed"; | ||
| } | ||
| } | ||
|
|
||
| // `.bootstrap-error` makes loading-bar invisible. | ||
| document.body.classList.add("bootstrap-error"); | ||
|
|
||
| const errorElement = document.createElement( | ||
| "easyops-default-error" | ||
| ) as DefaultError; | ||
| errorElement.errorTitle = isNetworkError(error) | ||
| ? i18n.t(`${NS}:${K.NETWORK_ERROR}`) | ||
| : i18n.t(`${NS}:${K.BOOTSTRAP_ERROR}`); | ||
| errorElement.textContent = httpErrorToString(error); | ||
| const linkElement = document.createElement("a"); | ||
| linkElement.slot = "link"; | ||
| linkElement.href = location.href; | ||
| linkElement.textContent = i18n.t(`${NS}:${K.RELOAD}`); | ||
| errorElement.appendChild(linkElement); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| document.querySelector("#main-mount-point")!.replaceChildren(errorElement); | ||
| function handleError(error: unknown) { | ||
| // eslint-disable-next-line no-console | ||
| console.error("bootstrap failed:", error); | ||
|
|
||
| if (shouldReloadForError(error)) { | ||
| location.reload(); | ||
| return "failed"; | ||
| } | ||
|
|
||
| // `.bootstrap-error` makes loading-bar invisible. | ||
| document.body.classList.add("bootstrap-error"); | ||
|
|
||
| const errorElement = document.createElement( | ||
| "easyops-default-error" | ||
| ) as DefaultError; | ||
| errorElement.errorTitle = isNetworkError(error) | ||
| ? i18n.t(`${NS}:${K.NETWORK_ERROR}`) | ||
| : i18n.t(`${NS}:${K.BOOTSTRAP_ERROR}`); | ||
| errorElement.textContent = httpErrorToString(error); | ||
| const linkElement = document.createElement("a"); | ||
| linkElement.slot = "link"; | ||
| linkElement.href = location.href; | ||
| linkElement.textContent = i18n.t(`${NS}:${K.RELOAD}`); | ||
| errorElement.appendChild(linkElement); | ||
|
|
||
| document.querySelector("#main-mount-point")!.replaceChildren(errorElement); | ||
| } | ||
|
|
||
| const bootstrapStatus = main(); | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.