Repository navigation
fix(tanstack): serve /live/invoke as an alias of /deco/invoke, 400 on malformed JSON - #645
Merged
Merged
Conversation
… malformed JSON
deco-runtime registered `/live/invoke[/*]` and `/deco/invoke[/*]` for the
same handler (runtime/handler.tsx). Clients built against a Fresh site —
the mobile app, partner integrations — still call `/live/invoke/...`; on
`@decocms/tanstack` that path fell through to the CMS router and answered
a 404 HTML page.
`aliasLiveInvoke` rewrites the request at the top of the worker's
`handleRequest`, before admin routes, bypass paths or the site's
`/deco/invoke/$` route read the path. Method, headers, body, query and
`cf` are preserved.
`handleInvoke` (blocks-admin) now answers 400 `{ error: "Invalid JSON" }`
for a body that declares `application/json` and does not parse, instead
of running the handler with `{}` — mirroring deco-runtime's `json`
strategy, which throws on invalid JSON. An empty body is still `{}`, and
a body without a JSON content-type keeps the lenient `try-json` path.
Found while validating deco-sites/oficina-tanstack#479.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts: # packages/tanstack/src/sdk/workerEntry.test.ts
|
🎉 This PR is included in version 7.77.7 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
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.
Problem
deco-runtime (Fresh) served the invoke endpoint under both prefixes —
runtime/handler.tsx:paths: ["/live/invoke", "/deco/invoke"]and["/live/invoke/*", "/deco/invoke/*"]. Clients built against a Fresh site keep calling/live/invoke/...(the oficina mobile app: Apple sign-in exchange/userinfo, addresses, cards). On@decocms/tanstackonly/deco/invoke/$exists, so/live/invoke/...falls through to the CMS router and answers a 404 HTML page.Reproduction (measured on the site)
POST https://<staging>/live/invoke/vtex/loaders/user.ts→404 text/html(the not-found page); the same body on/deco/invoke/vtex/loaders/user.ts→200 application/json. The site works around it insrc/worker-entry.ts(liveInvokeAlias).Fix
aliasLiveInvoke(request)(exported,workerEntry.ts):/live/invoke→/deco/invoke,/live/invoke/*→/deco/invoke/*. Applied at the top ofhandleRequest, before admin routes, bypass paths and the site's route tree read the path. SameRequestreference for every other path (no allocation on the hot path). The rewritten request keeps method, headers, body, query,signalandcf(built field by field — thenew Request(url, request)form does not carrycfon every runtime, same reasoning asrevalidationRequest).handleInvoke(blocks-admin/src/admin/invoke.ts): a body that declaresapplication/jsonand does not parse now answers400 { "error": "Invalid JSON" }instead of running the handler with{}(an action executing without its input). This mirrors deco-runtime'sjsonparsing strategy (runtime/routes/invoke.ts,propsParsers.jsonthrows) and the site's Fresh middleware (routes/_middleware.ts:return new Response("Invalid JSON", { status: 400 })forPOST /live/invoke/*). Compatibility kept where it matters: an empty body is still{}(bodiless POST = no props), and a body without a JSON content-type keeps the lenienttry-jsonbehavior ({}on failure).Tests
packages/tanstack/src/sdk/workerEntry.test.ts—aliasLiveInvokeleaves other paths untouched (same reference); rewrites exact and splat paths keeping query/method/headers/body/cf; end-to-end throughcreateDecoWorkerEntrythe server entry receives the/deco/invoke/...URL.packages/blocks-admin/src/admin/invoke.test.ts— declared-JSON malformed body → 400 and the handler never runs; empty declared-JSON body → handler runs with{}; no content-type + malformed → lenient{}.vitest runon both files: new tests pass (invoke.test.ts11/11; the 3 pre-existing failures inworkerEntry.test.ts—draft preview (pull-based)×2,CDN-Cache-Control … defaults to no-store— are the same on a cleanmain).tsc --noEmitclean forpackages/tanstackandpackages/blocks-admin;biome checkclean on the touched files.Found while validating deco-sites/oficina-tanstack#479.
🤖 Generated with Claude Code
Summary by cubic
Makes
@decocms/tanstackworkers serve/live/invokeand/live/invoke/*as aliases of/deco/invoke, so Fresh-built clients keep working, and answers 400 on malformed JSON in invoke bodies instead of silently running the handler with{}.aliasLiveInvokerewrites the path at the top of the worker's request handling, preserving method, headers, body, query andcf; other paths keep the sameRequestreference with no allocation.handleInvokereturns400 { error: "Invalid JSON" }for bodies that declareapplication/jsonbut don't parse; an empty body still becomes{}, and a body without a JSON content-type keeps the lenienttry-jsonbehavior.Written for commit a12aa9d. Summary will update on new commits.