[APPS] Inject transport at build time instead of forking at runtime#323
Draft
sdkennedy2 wants to merge 1 commit intomasterfrom
Draft
[APPS] Inject transport at build time instead of forking at runtime#323sdkennedy2 wants to merge 1 commit intomasterfrom
sdkennedy2 wants to merge 1 commit intomasterfrom
Conversation
Collaborator
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Collaborator
Author
🎉 All green!❄️ No new flaky tests detected 🔗 Commit SHA: 8a8ebf4 | Docs | Datadog PR Page | Give us feedback! |
80f3364 to
22efe5c
Compare
Summary
Testing
|
22efe5c to
e072fb6
Compare
ed163fe to
203664f
Compare
e072fb6 to
8a8ebf4
Compare
203664f to
57382e6
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Motivation
The injected browser runtime (
apps-runtime.mjs) currently forks between two transports at runtime inexecuteBackendFunction:devServerTransport—POST /__dd/executeActionagainst the Vite dev server middlewarepostMessageTransport— iframe bridge to the parent Datadog appWhether we're in a dev-server context or an iframe sandbox is actually known at build time (
vitevsvite build), so shipping both transports to every bundle is dead weight — production apps always run in an iframe and will never hit/__dd/executeAction.This PR makes the decision at build time: each bundle now ships only the transport it needs.
Changes
Split the single
src/built/apps-runtime.tsinto two single-transport entries and emit both from the plugin'stoBuildpipeline:src/built/apps-runtime-dev.ts→apps-runtime-dev.mjs(fetch to/__dd/executeAction)src/built/apps-runtime-prod.ts→apps-runtime-prod.mjs(window.parent.postMessage)Each runtime is a trivial side-effect module that assigns
globalThis.DD_APPS_RUNTIME.executeBackendFunctiondirectly from its one transport — theexecuteBackendFunctiondispatcher (src/backend/client/execute-backend-function.ts) and its runtimeisInIframe()fork are removed.The top-level
context.inject(...)ingetPlugins()is moved into a new Viteconfig(_, { command })hook on the apps plugin's Vite sub-plugin. The injection plugin queuesinject()items to aMapthat's only consumed insideplugin.buildStart(see packages/plugins/injection/src/index.ts), so deferring to Vite'sconfig()hook still lands in time.InjectPosition.MIDDLEis preserved — same reasoning as before: Vite's dev server only injects MIDDLE content viatransformIndexHtml.The apps plugin is Vite-only in practice (build/upload and dev-server middleware both live under
src/vite/), so non-Vite bundlers simply stop getting an injection. No fallback is added — without Vite'scloseBundle/configureServerthe runtime global would dangle with nothing to call.The existing
execute-backend-function.test.tswas ported tosrc/backend/client/transports/dev-server-transport.test.ts(same 6 cases, now targeting the transport directly).src/index.test.tsnow runs atest.eachover bothserveandbuildcommands to assert the correct runtime file is injected for each.QA Instructions
Stacked on #320.
Local dev:
cd ~/dd/build-plugins && ~/.yarn/switch/bin/yarn dev(runsprepare-link+ auto-rebuild watcher).NODE_TLS_REJECT_UNAUTHORIZED=0 dd-auth --domain="dd.datad0g.com" --actions-api -- npx vite. Fetch/@id/__datadog-helper-fileand confirm the response containsfetch("/__dd/executeAction"...)and has nopostMessage/app-builder:run-queryreferences.NODE_TLS_REJECT_UNAUTHORIZED=0 dd-auth --domain="dd.datad0g.com" --actions-api -- npx vite build. Grep the builtdist/assets/index-*.js—postMessageshould appear,/__dd/executeActionshould not.window.DD_APPS_RUNTIME.executeBackendFunctionresolves in both modes and backend function calls succeed.Verified locally against
test-action-catalog-app:/@id/__datadog-helper-filecontained only fetch-based transport.dist/assets/index-Bo1i35ss.jscontainedpostMessage(1) andapp-builder:run-query(1) with 0 occurrences of/__dd/executeAction.Unit:
yarn test:unit packages/plugins/apps— 13 suites / 137 tests pass.Blast Radius
@dd/apps-pluginand the published@datadog/vite-plugin.globalThis.DD_APPS_RUNTIME.executeBackendFunctioncontract is unchanged.apps-runtime-{dev,prod}.mjsfiles in place ofapps-runtime.mjs. Consumers never referenced the file path directly; the build tooling emits it.Documentation